Split last field but one using delimeter / :

    ($start,$finish) = split(/\//,$fields[-2]);

Chop off last character:

    $finishx = substr($finish,0,-1);

Chop off first character:

    $startx = substr($start,1);

Accepting command line arguments:

    These are stored in array @ARGV , $ARGV[0] is the first argument, $ARGV
    [1] the second, etc.

To find out the number of command line arguments:

    $#ARGV + 1 - $#ARGV is the number of the last argument. e.g. if it's
    $ARGV[7], $#ARGV will equal 7 so adding one gives the number of arguments
    (remembering that the first argument will be $ARGV[0})

To find the number of elements in an array

    scaler @array

To find the last element of an array

    $array[-1]

To check if an array is empty

    if (!@array)

To check if $ variable has a value

    if (defined $value)

String sort

    @result = sort {$a cmp $b } @somelist;

Numeric sort

    @result = sort {$a <=> $b } @somelist;

Numeric sort, reverse order

    @result = sort {$b <=> $a } @somelist;

Reverse numeric sort of a hash

    @result = sort { $hash{$b} <=> $hash{$a} } keys(%somehash);

Break out of a loop

    last;

Exit that iteration of a loop and start next iteration

    next;

Go back to the start of a loop iteration

    redo;

comparision operators

numeric string
equal == eq
not equal != ne
less than < lt
more than > gt
less than or equal <= le
greater than or equal => eq

Perl Modules

To check which perl modules are installed

    instmodsh -l

For easy cpan module install cpan App::cpanminus . You can then install modules as follows:

    cpanm Module::Name

This will work out th dependencies, do the build, etc. To upgrade do cpanm –self-upgrade

For Windows

  • perl Makefile.PL
  • dmake
  • dmake test
  • dmake install

More useful Perl tips here: http://wiki.preshweb.co.uk/doku.php

Recent Changes