Rob’s PERL Cookbook

June 17th, 2008

I occasionally write some PERL scripts. Sometimes it can be many months between writing PERL scripts. Whenever I start writing them again, I forget some basic things. The last time I started writing some PERL again, I decided to write down the things that I was looking up. Here is my PERL cookbook of common tasks.

PERL Documentation

http://perldoc.perl.org/

Passing Parameters to Functions

Parameters are passed to functions in the @_ list.

Typically, the following is done in a function:

sub function {
  my $parm1=shift @_;
  my $parm2=shift @_;

  ...function body...
}

Using “my” makes the scope of the variable only inside the function. If there is no “my”, it is globally accessible.

Constants/#defines

See “constant” on perldoc.

use constant {
  CONST1 => 3,
  CONST2 => 4,
};

Use “CONST1” (without $ or @ preceeding it) in the code to refer to the constant “3”.

Quotes

To summarize the main difference between single and double quotation marks: Single quotation marks do not interpret, and double quotation marks do. That is, if you put something in single quotation marks, Perl assumes that you want the exact characters you place between the marks — except for the slash-single quote (\’) combination and double-slash (\\) combination. If you place text inside double quotation marks, however, Perl interprets variable names. Perl also interprets special characters inside double-quoted literal strings.

Quotes for dummies

Perl documentation on quote operators.

File test operators

Reference

To test if a file is a directory, do:

if -d $file

To test if a file exists

if -e $file

Chomping newlines

The correct thing to do is:

chomp $var

Reference

This modifies the value of $var itself

DO NOT DO $var=chomp($var): that will put the return value of chomp() into $var, which is not the chomped $var, it is the number of characters

Search/replace regular expression

$var =~ s/(hi)there/$1/

The above example replaces “hithere” with just “hi”.

It takes $var as input, and also places the output into $var.

Notes:

  1. No quotes areound the regexp
  2. $1, $2, etc can be used to substitute the grouped expressions

Initialize a list

@list = ("item1","item2","item3");

Get the size of a list

$listsize = scalar(@list)

The scalar() function on a list returns the size of the list

Find the length of a string in a variable

$size=length($string)

foreach loop

perldoc for foreach

Break out of a loop

The “last” keyword is the equivalent of “break” in C. It will exit out of a loop.

It is also more powerful than “break”, as it can exit out of an outermost loop. For example:

LOOP_OUTER: for ($i=0;$i<10;++i)
{
  for ($j=0;$j<10;++i)
  {
    if (some condition)
      last LOOP_OUTER;
  }
}

Use last without a label and it breaks out of the innermost loop.

Here is the documentation for last.

last doesn’t work with do/while. You need to use a label for it. See the end of the statement modifiers section of perldoc. Here is the example they give:

 LOOP: { 
  do {
    last if $x = $y**2;
    # do something here
	 } while $x++ <= $z;
  }

Skip to the next loop iteration

The “next” keyword is the equivalent of “continue” in C.

documentation for next

Forking and waiting on multiple processes, and the INT signal

fork() will split the execution into child and parent.

It returns the child PID to the parent, and returns 0 to the child.

Use waitpid to wait for the child to finish. It can be non-blocking by using “WNOHANG” as the 2nd argument, or it can be blocking by using “0” as the 2nd argument.

The following example will wait for each child to finish in a blocking way (waitpid will block until at least one child is finished):

do {
  $kid = waitpid(-1, 0);
} while($kid>0);

And with nonblocking:

do {
  $kid = waitpid(-1, WNOHANG);
} while($kid>0);

It seems that when Ctrl-C is pressed on the parent it is passed to the children. This is good.

Look at this example code: parent fork_test_parent.pl invoking fork_test_child.pl.

Handling Control-C

See here.

“INT” is like a #define that can be used for the signal number for Ctrl-C.

These examples have a use of it: fork_test_parent.pl invoking fork_test_child.pl

Signalling Ctrl-C to a process

kill @pids

Index 0 of @pids must be the signal number. “INT” can be used for Ctrl-C.

“kill” is used in the “cleanup” function of fork_test_parent.pl.

Expect.pm

Expect.pm documentation.

Command line parameters

The @ARGV list has the command line parameters. $ARGV[0] has the first parameter, it does not contain the executable name (unlike C). Using “shift @ARGV” will move $ARGV[1] into $ARGV[0].

Reading an input file

$ret=open(FH,"$fname");
if (!$ret)
{ # File open failure.
 print STDERR "$fname was not opened successfully!!!\n";
 exit(1);
}

# File opened successfully
while ($inputline=<FH>)
{
 chomp($inputline);   # Remove trailing newline

 # Process $inputline

} # while ($inputline=<FH>)
close (FH);

Writing to an output file

$ret=open(FH,">$fname");
if(!$ret)
{ # File open is unsuccessful
  print STDERR "Can't open $fname for output!!!\\n";
  exit(1);
}
print FH "output line #1\\n";
print FH "output line #2\\n";
close (FH);

Using split with whitespace between values

($first_token,$second_token)=split(/\\s+/,$linein);

Installing a module from CPAN

May need admin privileges to do this. If using cygwin, open a cygwin prompt as Administrator. Below is an example of how I installed ExifTool

perl -MCPAN -e 'install Image::ExifTool'

2 Comments

  1. Davidon 18 Aug 2009 at 2:35 am

    I miss perl – I loved perl – Poetry in code…
    preg_match – p’sha

  2. Robon 08 Jul 2010 at 7:31 pm

    Needed:
    cookbook for eval
    cookbook for opening files/reading lines.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.