Try perl -ne 'print if /pattern/' input.txt
. For find in
a range and print, try
perl -ne 'print if (m/PatternA/..m/PatternB/)'
.
To replace the original string with the new in a specific range,
try perl -ne 's/this/that/g if (m/PatternA/..m/PatternB/)'
.
I never try other cases without specificing a range.
Please use
my $newstring = $oldstring =~ s/foo/bar/r;
/r
option is necessary to keep the original one. See answer and
answer.
Plain >
only catch STDOUT
. Redirect STDERR
to STDOUT
needs 2>&1
.
An example
system("ls -lst > example.txt 2>&1");
This is a dangerous way. Safer ways are discussed in answer More info in answer,
[2021-11-16 Tue 15:49]Make use of system
command. See the link.
Make use of return
at the end of the subroutine. See the link and
link.
I checked this link. I choose the way using hash. You may want to check others.
I quote the method here. The script is
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
sub fields {
say 'this is fields';
}
sub another {
say 'this is another subroutine';
}
my %functions = (
fields => \&fields,
another => \&another,
);
my $function = shift;
if (exists n$functions{$function}) {
$functions{$function}->();
} else {
die "There is no function called $function available\n";
}
The usage is
$ ./dispatch_tab fields this is fields $ ./dispatch_tab another this is another subroutine $ ./dispatch_tab xxx There is no function called xxx available[2021-11-17 Wed 23:33]
-f
function is useful to check if a file is a plain file. See the
answer and link. A usage can be checked in the doc for readdir.
opendir(my $dh, $some_dir) || die "Can't opendir $some_dir: $!";
my @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
closedir $dh;
See the link.
push(@array, element)
, add element or elements into the end of the array$popped = pop(@array)
, delete and return the last element of the array$shifted = shift(@array)
, delete and return the first element of the arrayunshift(@array)
, add element or elements into the beginning of the array
You could use last
statement to break the innermost loop. Other usage
can be found in the link. And here is a related question on
Stackoverflow
.
See this link. The first argument is $_[0]
and the second is
$_[1]
. The array need to be passed as reference. See this question
and answer.
Please check the link.
[2021-11-18 Thu 16:27]We need the operator s. An example:
my $myvar = "abc";
print ("$myvar\n");
$myvar =~ s/a/A/; # this will modify the original string
print( $myvar =~ s/b/B/ ); # $myvar won't be printed.
print( $myvar =~ s/c/C/r ); # r option guarantee original one unchanged
$mynewvar = $myvar =~ s/c/C/r; # a new string saved in $mynewvar
Please note the difference between the return value with and without
option r
. Also see this answer.
You can use i
mdifier, an example /G[a-b].*/i
. Check this answer.
I quote the answer here. The following are false and others are true.
0 '0' undef '' # Empty scalar () # Empty list ('')[2021-11-18 Thu 17:08]
Make use of sort.
[2021-11-18 Thu 17:09]Make use of open. It needs file handle, mode, and filename.
open(filehandle,mode,filename)
You have three modes:
mode | operand |
---|---|
read | < |
write | > |
append | >> |
Also check this link.
[2021-11-18 Thu 17:12]Check this link.
[2021-11-18 Thu 21:03]You need to redefine the variable $/
to undef
, which is dangerous.
Then <>
will read the file in one chunk. See the discussions.
See this link.
[2021-11-18 Thu 21:07]Use the modifier /s
. Check this link.
You can make use of defined.
[2021-11-18 Thu 21:11]Make use of opendir
and readdir
. See this link.
Please check this answer, using (?!)
.