Perl

Table of Contents

Introduction

Philosophy

Perl is an interpreted language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). It combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh, so people familiar with those languages should have little difficulty with it.

(Perl 4 manual pages)

Perl 5 includes a number of features which make Perl a general-purpose programming language. Examples for this are support for Object Oriented Programming, Unicode, database integration, etc. (What is Perl? ).

Perl borrows syntax and concepts from many languages: awk, sed, C, Bourne Shell, Smalltalk, Lisp and even English.

(http://perldoc.perl.org/perlsyn.html)

  • There is more then one way to do it (TIMTOWTDI)
  • Do what I mean (DWIM)

Hello World

use strict;
use warnings;

print "Hello, world";

Basics

Data types

Scalars

my $string = 'text';
my $integer = 42;
my $float = 3.14;

say $string;
say $integer;
say $float;

Arrays

References

  • Basic example
    my %combination;
    
    $combination{25} = 0;
    $combination{10} = 0;
    $combination{ 5} = 0;
    $combination{ 1} = 0;
    $combination{25} += 3;
    
    my $cref = \%combination;
    $cref->{25} -= 1;
    say Dumper(%combination);
    
  • How to dereference a list?
    my @list = (25, 20, 5, 1);
    my $list_ref = \@list;
    for my $element (@{$list_ref}) 
        say $element;
    }
    
  • How to dereference a hash reference?

Constants

http://perldoc.perl.org/constant.html

use constant US_COINS => {
    QUARTERS => 25,
    DIMES    => 10,
    NICKELS  => 5,
    PENNIES  => 1
};

for my $name (keys US_COINS) {
    say $name;
}

say US_COINS->{'QUARTERS'};

Flow control

Conditionals

my $false_1 = 0;
my $false_2 = '0';
my $false_3 = '';
my $false_4 = ();
my $false_5 = undef;
my $true = 'rest';

if ($false_1) {
    say 'if block executed';
} elsif ($true) {
    say 'elsif block executed';
} else {
    say 'else block executed';
}

Loops

http://perldoc.perl.org/perlsyn.html#Loop-Control

  • while
    my @list = ('one', 'two', 'three');
    while (@list) {
        say shift @list;
    }
    
  • for

    http://perldoc.perl.org/perlsyn.html#For-Loops This is the Perl way for for-loops:

    for (1..10) {
        say 'Counter: ' . $_;
    }
    

    However, C-style loops could be used as well:

    for (my $i = 0; $i < 10; $i++) {
        say 'Counter: ' . ($i + 1);
    }
    
  • foreach

    For and foreach can be used as synonyms.

  • Labels and continue

    while, until and for loops support labels in order to control the loop execution.

Modules

How to export a function from a module?

Packages

package Local::Anagrams;

use strict;
use warnings;
use Exporter 'import';
our @EXPORT_OK = qw/ get_permutations /;

sub get_permutations 
    my $string = shift;
    # ...
    return @permutations;
}

1;

Intermediary

Commandline parameters

The simplest way to read command line arguments is via the special variable '@ARGV'.

use strict;
use warnings;

require 'anagrams.pm';

use feature 'say';

if (@ARGV != 1) {
    say 'Usage: get_anagrams.pl <word>';
    exit 1;
}

foreach my $word (get_permutations($ARGV[0])) {
    say $word;
}

Read more:

Unit tests

Test::Simple

https://metacpan.org/pod/Test::Simple

use strict;
use warnings;

use Test::Simple tests => 1;

sub add {
    my ($a, $b) = @_;
    return $a + $b;
}

ok(add(1,2) == 3, 'Simple addition calculated correctly.');

Test::More

https://metacpan.org/pod/Test::More

use strict;
use warnings;
use Test::More tests => 2;
use Local::List;
my @example_list = (6,1,34,3,12);
my $min_value = min(@example_list);
cmp_ok($min_value, '==', 1);
my $max_value = max(@example_list);
cmp_ok($max_value, '==', 34)

Advanced

Miscellaneous

Rounding

Math::Round

use strict;
use warnings;

use Math::Round; #http://search.cpan.org/~grommel/Math-Round-0.06/Round.pm

my $f = 3.1415926535;
my $r = printf("%.3f", 3.1415926535);
print "$r\n";

POSIX

use strict;
use warnings;

use feature 'say';
use POSIX qw (floor ceil);

my $float = 1.234;
say floor($float);
say ceil($float);
# https://metacpan.org/pod/distribution/perl/ext/POSIX/lib/POSIX.pod

Use English

http://perldoc.perl.org/English.html

With 'use English' it is possible to use English terms for special variables etc.

use English;

Data::Dumper

https://metacpan.org/pod/Data::Dumper

use strict;
use warnings;

use feature 'say';
use Data::Dumper;

my %example_hash;
$example_hash{key1} = 'value1';
$example_hash{key2} = 'value2';

say Dumper(%example_hash);

Custom quoting operators

GitHubImpressum