Skip to content

Latest commit

 

History

History
263 lines (231 loc) · 9.31 KB

perl.org

File metadata and controls

263 lines (231 loc) · 9.31 KB

Perl tips

One-line phraser

grep and sed print

Try perl -ne 'print if /pattern/' input.txt. For find in a range and print, try perl -ne 'print if (m/PatternA/..m/PatternB/)'.

sed replacement

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.

General scripts

String substitution

[2021-11-16 Tue 14:29]

Please use

my $newstring = $oldstring =~ s/foo/bar/r;

/r option is necessary to keep the original one. See answer and answer.

Redirect output from system commands in perl

[2021-11-16 Tue 14:35]

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,

Run system comands

[2021-11-16 Tue 15:49]

Make use of system command. See the link.

Return a value in subroutine

[2021-11-16 Tue 15:55]

Make use of return at the end of the subroutine. See the link and link.

How to call subroutine in command line

[2021-11-17 Wed 09:51]

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

How to check if a file is plain file

[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;

Push, pop, shift, unshift arrays

[2021-11-18 Thu 00:23]

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 array
  • unshift(@array), add element or elements into the beginning of the array

Break loop

[2021-11-18 Thu 01:09]

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.

Pass arguments to subroutine

[2021-11-18 Thu 01:21]

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.

system

[2021-11-18 Thu 16:19]

Please check the link.

Replace string

[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.

Ignore case sensitivity

[2021-11-18 Thu 16:52]

You can use i mdifier, an example /G[a-b].*/i. Check this answer.

Boolean in perl

[2021-11-18 Thu 16:54]

I quote the answer here. The following are false and others are true.

0
'0'
undef
''  # Empty scalar
()  # Empty list
('')

Sort an array

[2021-11-18 Thu 17:08]

Make use of sort.

Open a file

[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:

modeoperand
read<
write>
append>>

Also check this link.

Read files

[2021-11-18 Thu 17:12]

Check this link.

Read multi-line

[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.

undef

[2021-11-18 Thu 21:06]

See this link.

Replace characters in multiple lines

[2021-11-18 Thu 21:07]

Use the modifier /s. Check this link.

Check if a variable is defined

[2021-11-18 Thu 21:08]

You can make use of defined.

Read a directory

[2021-11-18 Thu 21:11]

Make use of opendir and readdir. See this link.

how to select lines without containing a pattern

[2021-11-25 Thu 16:45]

Please check this answer, using (?!).