Learn Perl in 1 Hour
This is a basic Perl tutorial. The goal is to get a quick working understanding of the language. Examples on this page are based on Perl 5.14.2. (released on )
Perl variable prefix: sigil
in Perl, every variable name must start with one of {$ @ %}.
$means the VALUE of the variable is a “scalar”. i.e. string, number.@means the VALUE of the variable is a array.%means the VALUE of the variable is a hash table (aka dictionary, associative list.).
# -*- coding: utf-8 -*- # perl use Data::Dumper; # for printing array and hash $Data::Dumper::Indent = 0; # set to print compact $aa = 4; # scalar @aa = (1, 2, 3); # array %aa = ('e' => 4, 'f' => 5, 'g' => 6); # hash print $aa, "\n"; # 4 print Dumper(\@aa), "\n"; # $VAR1 = [1,2,3]; print Dumper(\%aa), "\n"; # $VAR1 = {'e' => 4,'g' => 6,'f' => 5};
Strings
quoting string
Use single quote for literal string.
# -*- coding: utf-8 -*- # perl # use single quote for literal string $a = 'this and that'; print $a; # prints 2 lines
To have characters \n for newline and \t for tab, use double quote.
# -*- coding: utf-8 -*- # perl $a = "this\nand that"; print $a; # prints 2 lines
When double quote is used, variables inside the string will be evaluated.
# -*- coding: utf-8 -*- # perl $a = 4; $b = "this is $a"; print $b; # this is 4
Summary: 2 major ways to quote strings strings: single quote and double quote.
'single quote'→ everything is literal."double quote"→ ① Backslash is char escape. ② Variables will be evaluated.
Quoting String q[] qq[]
You can also use the syntax q(this n that), which is equivalent to 'this n that'.
The parenthesis can be curly brackets {} or square brackets []. It can also be / \ | @ and most others ASCII symbols.
# -*- coding: utf-8 -*- # perl # the following are all same $a = q(this 'n' that); $b = q[this 'n' that]; $c = q{this 'n' that}; $d = q|this 'n' that|; $e = "this 'n' that"; $f = 'this \'n\' that';
Similarly, "…" is same as qq(…).
Substring
substr(string, offset, number of chars to extract).
# -*- coding: utf-8 -*- # perl # get substring print substr('012345', 2, 2); # prints 23
String Length
Length of string is length(string).
print length('abc');
String Join and Repetition
use dot to join string.
# -*- coding: utf-8 -*- # perl $s = "a" . "b"; print $s; # ab
String repetition is done with the operator x.
# -*- coding: utf-8 -*- # perl print 'abc' x 2; # abcabc
True and False
Perl does not have a boolean type. Basically, anything that seems should be false, is false. (of course, it can be tricky). The following are false:
- 0
undef""empty string- empty array
- empty hash
Everything else is true.
Perl does automatic conversion between number and string, so '0' is false in some contexts because it converts to 0. But '0.0' is true, because it remains a string, and is not empty string.
The value of Perl's {array, list, hash}, depends on context (what's adjacent to it), and is not very intuitive.
The best thing is to test what you need exactly. For example, check if the length of a list is 0, or whether a var has value 0, or whether it is undef.
# -*- coding: utf-8 -*- # perl use strict; if (0) { print "yes"} else { print "no"} # ⇒ no if (0.0) { print "yes"} else { print "no"} # ⇒ no if ("0") { print "yes"} else { print "no"} # ⇒ no if ("") { print "yes"} else { print "no"} # ⇒ no if (undef) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*- # perl use strict; # empty array is false my @myArray = (); if (@myArray) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*- # perl use strict; # empty hash is false my %myHash = (); if (%myHash) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*- # perl use strict; if (1) { print "yes"} else { print "no"} # ⇒ yes if ("0.0") { print "yes"} else { print "no"} # ⇒ yes if (".0") { print "yes"} else { print "no"} # ⇒ yes
Explicit Testing
# -*- coding: utf-8 -*- # perl use strict; # examples of explicit testing my $x = 5; my $y; if (defined($x)) { print "yes"} else { print "no"} # ⇒ yes if (defined($y)) { print "yes"} else { print "no"} # ⇒ no if ($x == 0) { print "yes"} else { print "no"} # ⇒ no
# -*- coding: utf-8 -*- # perl use strict; # testing array length my @myArray = (); my $myArrayLength = scalar @myArray; if ($myArrayLength == 0) { print "yes"} else { print "no"} # ⇒ yes
Conditional: if then else
#-*- coding: utf-8 -*- # perl # Examples of if $x = 1; if ($x == 1) { print "x yes\n"; } $y = 2; if ($y == 1) { print "y yes\n"; } else { print "y no\n"; } $z = 2; if ($z < 0) { print 'z neg'; } elsif ($z == 0) { print 'z zero'; } elsif ($z == 1) { print 'z one'; } else { print 'z other'; }
Loop, Iteration
#-*- coding: utf-8 -*- # perl @aa = (1..9); # creates a array 1 to 9 for $xx (@aa) { print $xx } # ⇒ 123456789
Note: Perl also supports loop controls “next”, “last”, “goto” and few others.
#-*- coding: utf-8 -*- # perl for $xx (1..9) { print $xx; if ($xx == 4) { last; # break } } # ⇒ 1234
#-*- coding: utf-8 -*- # perl $x = 1; while ($x <= 9) { print $x, "\n"; $x++; }
List
Creating a List
- List is enclosed by parenthesis “()”.
- To assign a list to a variable, the variable must have a “@” sign in front.
- To print a list, use the “Dumper” function in the package “Data::Dumper”.
# -*- coding: utf-8 -*- # perl @a = (0, 1, 2, 'three', 4, 5, 6, 7); # assigns a list to @a. use Data::Dumper; # loads the list-printing module print '@a is:', Dumper(\@a);
The backslash in front of @a is necessary. It returns the “reference” of the array @a, and the argument to Dumper must be a reference. Once a list is assigned to a variable, it's called array.
Perl's concept of “list” and “array” is a bit complex. Basically, when a list is assigned to a variable, it's a array. For detail, see: Perl: Difference Between List and Array
Counting Elements
To find the number of elements in a list, use “scalar”.
# -*- coding: utf-8 -*-
# perl
@a = (4, 5, 6); # a list
print scalar(@a); # prints 3. The length.
print @a + 0; # prints 3. The + forces a scalar context.
Perl has a “list context” and “scalar context”. How things are evaluated depends on whether the thing is in list or scalar context. When a list/array is in a scalar context, it returns its length. The function scalar forces things in a scalar context.
Adding Elements
To add a element, or join two lists, use push(array, new_item).
# -*- coding: utf-8 -*- # perl use Data::Dumper; @b = (1, 9); push(@b, 3); # add a element to @b, at the end print Dumper(\@b); # [1, 9, 3]
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a = (1, 9); @b = (3, 4); @c = (); push(@c, @a, @b); # @c is the joined list of @a and @b print Dumper(\@c); # [1, 9, 3, 4]
Nested List
Perl automatically flatten array/lists, even if the new item added to list is a list. To force creating a nested list, you have to use square brackets, like this:
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a = (1, 9); @b = (5, 6); # create nested list push(@a, [@b]); print Dumper(\@a); # [1, 9, [3, 4]]
Square brackets actually creates a reference to a array.
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a = (1, 8); # array $b = [1, 8]; # reference to array print Dumper(\@a); # [1, 8] print Dumper($b); # [1, 8]
Getting Elements
To extract list element, append with [index]. The index can be several integers separated by comma, for getting multiple elements.
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a = (0, 1, 2, 'three', 4, 5, 6, 7); @b = @a[3, 1, 5]; # ['three', 1, 5] $c = @a[2]; # gets 2 print Dumper \@b; # ['three', 1, 5] print $c, "\n"; # 2
Here a example of extracting sublist (aka slice).
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a = (0, 1, 2, 'three', 4, 5, 6, 7); @b = @a[1..4]; # the 1..4 creates a range print Dumper \@b; # [1, 2, 'three', 4]
Changing Elements
To replace parts, just assign them. For example, $myArray[3] = "heart";.
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a = (0, 1, 2, 'three', 4); $a[3] = 'new'; print Dumper(\@a); # [ 0, 1, 2, 'new', 4 ]
Note the dollar sign $ above. This tells Perl that this data is a “scalar” as opposed to a “multiple”. In Perl, a variable of “scalar” type (such as numbers and strings) starts with a $. A variable for array (aka list) starts with the sign @. A variable for harshes/dictionaries starts with %. All Perl variables must start with one of {$ @ %}. (this is a simplified story)
Nested List
To create a nested list, use square brackets for the inner list.
# -*- coding: utf-8 -*- # perl use Data::Dumper; @b = (4, 5, [1, 2, [9, 8]], 7); # nested list print Dumper \@b; # [ 4, 5, [ 1, 2, [ 9, 8 ] ], 7 ]
You can embed a array as a nested list into another array. For example, @b = (4, 5, \@myarray, 7).
# -*- coding: utf-8 -*- # perl use Data::Dumper; @a=(1, 2, 3); @b = (4, 5, \@a, 7); # embed @a as sublist. print '@b', Dumper \@b; # [ 4, 5, [ 1, 2, 3 ], 7 ]
To extract element from nested list, use this form: $array_name[first_level_index]->[2nd_level_index]->[3rd_level_index]….
# -*- coding: utf-8 -*- # perl use Data::Dumper; @b = (1, 2, ['x', 'y'], 3); $c = $b[2]->[1]; print $c; # 'y' @b = (1, 2, ['x', [4, 5], 7], 3); $c = $b[2]->[1]->[1]; print $c; # 5
Keyed List (dictionary; hash table; associative list)
In Perl, keyed-list is called hash table, or just hash. It is done like this:
# -*- coding: utf-8 -*- # perl use Data::Dumper qw(Dumper); # load the Dumper function for printing array/hash $Data::Dumper::Indent = 0; # make it print in compact style # hash table %hh = ('john'=>3, 'mary'=> 4, 'jane'=> 5, 'vicky'=>7); print Dumper \%hh; # {'jane' => 5,'john' => 3,'vicky' => 7,'mary' => 4}
The line use Data::Dumper qw(Dumper); loads the function “Dumper” from the package “Data::Dumper”.
The purpose of Dumper is to print array and hash.
Variable of hash datatype must begin with % in their name.
# -*- coding: utf-8 -*- # perl use Data::Dumper qw(Dumper); # for printing list or hash %hh = ('john' =>3, 'mary' => 4, 'jane' => 5, 'vicky' => 7); print Dumper \%hh; # get value from a key print $hh{'mary'}; # 4 # delete a entry delete $hh{'vicky'}; print Dumper \%hh; # { 'jane' => 5, 'john' => 3, 'mary' => 4 } # get all keys print Dumper [keys %hh]; # [ 'jane', 'john', 'mary' ] # get all values (Perl 5.12. released in 2010) print Dumper [values %hh]; # [ 5, 3, 4] # check if a key exists print exists $hh{'mary'}; # returns 1, meaning true.
If you are going to get values of a hash, you use $ in front of the hash variable. For example, $b{'mary'}.
The Dumper function's argument needs to be a “reference” to
the hash.
So, you can use it like this:
Dumper(\%b)
or
Dumper([%b]).
(parenthesis is usually optional)
Looping Thru a List
Removing Elements in a List
Use “grep” to remove elements in a list. The form is one of:
grep {true/false function name $_} arraygrep {expression on $_ ;} array
Example:
# -*- coding: utf-8 -*- # perl use Data::Dumper; sub ff {return $_[0] % 2 == 0}; # return true if divisible by 2 print Dumper[ grep {ff $_} (0..10)]; # ⇒ [ 0, 2, 4, 6, 8, 10 ]
@_→ a builtin variable that's all the arguments passed to a subroutine, as array. So,$_[0]is the first argument passed.$_→ a builtin variable that's the default input for regex to match, and in general represents a default argument.
The (0..10) generate a list from 0 to 10.
The % above is the operator for computing remainder of a division.
The Data::Dumper module is to import the “Dumper” function for printing list.
Applying a Function to a List
Use “map” to apply a function to a list. The basic form is map {function_name($_)} list. It returns a list.
# -*- coding: utf-8 -*- # perl use Data::Dumper; $Data::Dumper::Indent=0; sub ff {return ($_[0])**2;}; # square a number print Dumper [ map { ff($_)} (0..10)]; # ⇒ $VAR1 = ['0','1','4',9,'16',25,36,49,'64',81,100];
The ** is the exponential operator.
Using Library
In Perl, a library is called a module. The standard filename suffix is “.pm”.
For a script, the filename suffix is “.pl”.
List Available Modules
To get a list of standard module that are bundled with Perl (but not necessarily installed). perldoc perlmodlib..
To load a package, call use package_name;. It will import all functions in that package. Example:
# -*- coding: utf-8 -*- # perl # loading some commonly used packages use Data::Dumper; # for printing list and hash use File::Find; # for traversing directories
To find out what functions are available in a module, read its documentation, for example perldoc Data::Dumper.
Module Load Path
Here is a example showing module paths and loaded modules:
# -*- coding: utf-8 -*- # perl use Data::Dumper; print Dumper \@INC; # prints all module searching paths print Dumper \%INC; # prints all loaded modules __END__ sample output: $VAR1 = [ '/etc/perl', '/usr/local/lib/perl/5.12.4', '/usr/local/share/perl/5.12.4', '/usr/lib/perl5', '/usr/share/perl5', '/usr/lib/perl/5.12', '/usr/share/perl/5.12', '/usr/local/lib/site_perl', '.' ]; $VAR1 = { 'warnings/register.pm' => '/usr/share/perl/5.12/warnings/register.pm', 'bytes.pm' => '/usr/share/perl/5.12/bytes.pm', 'XSLoader.pm' => '/usr/share/perl/5.12/XSLoader.pm', 'Carp.pm' => '/usr/share/perl/5.12/Carp.pm', 'Exporter.pm' => '/usr/share/perl/5.12/Exporter.pm', 'strict.pm' => '/usr/share/perl/5.12/strict.pm', 'warnings.pm' => '/usr/share/perl/5.12/warnings.pm', 'overload.pm' => '/usr/share/perl/5.12/overload.pm', 'Data/Dumper.pm' => '/usr/lib/perl/5.12/Data/Dumper.pm' };
For more info about the predefined variables @INC and %INC.
perldoc perlvar
Perl: List Available Modules, List Module Search Paths
Defining a Function
Here is a example of defining a function.
# -*- coding: utf-8 -*- # perl use Data::Dumper; $Data::Dumper::Indent = 0; # print in compact style # define a function sub ff { $a = $_[0]; # get first arg $b = $_[1]; # get second arg # arguments are automatically assigned to array @_ print Dumper(\@_); # prints the array @_ # use “return” to return value and exit the function return $a + $b; } ff(3, 4, "rabbit"); # $VAR1 = [3,4,'rabbit'];
Note: Unlike most other languages, subroutine's parameters are USUALLY not declared.
Arguments are automatically assigned to the array @_. So, $_[0] is the first element of the array @_. The @_ a builtin variable.
Optional Parameters
To define a function with optional parameters, just use defined($_[n]) to check if the argument is given.
# -*- coding: utf-8 -*- # perl # myFun(x,y) returns x+y. y is optional and default to 1. sub myFun { $x = $_[0]; if (defined $_[1]) { $y = $_[1]; } else { $y = 1; } return $x+$y; } print myFun(3); # 4
[see Perl: Function Optional Parameter]
Classes and Objects
Writing a Module
In the following, i show you how to write a library in Perl by a example.
Save the following 3 lines in a file and name it mymodule.pm.
# -*- coding: utf-8 -*- # perl package mymodule; # declaring the module sub f1($){$_[0]+1} # module body 1 # module must return a true value
Then, call it like the following way:
# -*- coding: utf-8 -*- # perl use mymodule; # import the module print mymodule::f1(5); # call the function
This is the simplest illustration of writing a package in Perl and calling its function.
In Perl, there are 2 different concepts for a set of code that resides in a file: “module” and “package”.
A “module” is simply a file of Perl code. To load a module, use require file_name. It is similar to “include” in C and PHP. A module file normally has suffix “.pl”.
A “package” in Perl is more modern meaning of a library. It is a mechanism of importing functions defined in a external file, using name spaces. Package files has “.pm” as suffix. A package file needs to start with package filename; and must return a value true (any number, string, as the last line will do).
Perl's module was there before it had a real library system. You should just write packages.
If you have a question, put $5 at patreon and message me.