Perl Tutorial - Practical Extraction and Reporting Language (Perl)

Please leave a remark at the bottom of each page with your useful suggestion.

Table of Contents

  1. Perl Introduction
  2. Perl Program Startup
  3. Perl Regular Expressions
  4. Perl Array Program
  5. Perl Basic Program
  6. Perl Subroutine / Function Program
  7. Perl XML Program
  8. Perl String Program
  9. Perl Statement Program
  10. Perl Network Program
  11. Perl Hash Program
  12. Perl File Handling Program
  13. Perl Data Type Program
  14. Perl Database Program
  15. Perl Class Program
  16. Perl CGI Program
  17. Perl GUI Program
  18. Perl Report Program

Perl Program Startup

Your First Perl Program

Take the following text and put it into a file called

     print "Hi there!\n";

Now, run it with your Perl interpreter. From a command line, go to the directory with this file and type perl You should see:

Hi there!

The \n indicates the ``newline” character; without it, Perl doesn’t skip to a new line of text on its own.

Perl Variables

A place where we can store data. A variable needs a name, put new data in it, retrieve the data stored in it.

Variable Names

  • Contain alphanumeric characters and underscores
  • User variable names may not start with numbers
  • Variable names are preceded by a punctuation mark indicating the type of data

Types of Perl Variable

Different types of variables start with a different symbol

  • Scalar variables start with $
  • Array variables start with @
  • Hash variables start with %

Declaring Variables

You don't need to declare variables in Perl. But it's a very good idea typos scoping. Using the strict pragma.

use strict;
my $var;

Scalar Variables

Store a single item of data

my $name = "Mahendran";
my $myrole = 'Perl Developer';
my $num = 32;
my $decimal = 0.000001;
my $very_large_number = 3.27e17; 

Type Conversions

Perl converts between strings and numbers, Add int to a floating point number

my $sum = $num + $decimal;
Putting a number into a string
print "$name says, 'The meaning of life is $sum.'\n";

Mahendran says, 'The meaning of life is 32.000001'

Quoting Strings

Single quotes don't expand variables or escape sequences. Use a backslash to escape special characters in double quoted strings.

my $price = '$9.95';
my $invline = "24 widgets @ $price each\n";
print "He said \"The price is  \$300\"";

24 widgets @ $price each
He said "The price is $300"

Better Quotes

This can look ugly
	print "He said \"The price is \$300\"";
This is a tidier alternative
	print qq(He said "The price is \$300");
Also works for single quotes
	print q(He said "That's too expensive");

Undefined Values

A scalar variable that hasn't had data put into it will contain the special value “undef”

Test for it with defined() function
	if (defined($my_var)) { ... }

Array Variables

Arrays contain an ordered list of scalar values.

my @fruit = ('apples', 'oranges','guavas', 'passionfruit', 'grapes');
my @magic_numbers = (23, 42, 69);
my @random_scalars = ('mumble', 123.45, 'dave cross', -300, $name);

Array Elements

Accessing individual elements of an array

print $fruits[0]; 
	# prints "apples"  
Note: Indexes start from zero
print $random_scalars[2];   
	# prints "dave cross"
Note: use of $ as individual element of an array is a scalar

Array Slices

Returns a list of elements from an array

print @fruits[0,2,4];
	# prints "apples", "guavas", "grapes"

print @fruits[1 .. 3];
	# prints "oranges", "guavas", "passionfruit"
Note use of @ as we are accessing more than one element of the array

Setting Array Values

$array[4] = 'something';
$array[400] = 'something else';

Also with slices
@array[4, 7 .. 9] = ('four', 'seven', 'eight','nine');
@array[1, 2] = @array[2, 1];

Doesn't need to be an array
($x, $y) = ($y, $x);

Array Size

$#array is the index of the last element in @array. Therefore $#array + 1 is the number of elements

$count = @array; 

Does the same thing and is easier to understand

Hash Variables

Hashes implement “look-up tables” or “dictionaries”

Initialised with a list

%french = ('one', 'un',
           'two', 'deux',
           'three', 'trois');
“fat comma” (=>) is easier to understand

%german = (one   => 'ein',
           two   => 'zwei', 
           three => 'drei');

Accessing Hash Values

As with arrays, notice the use of $ to indicate that we're accessing a single value.

$three = $french{three};
	print $german{two};

Hash Slices

Just like array slices. Returns a list of elements from a hash

print @french{'one','two','three'};
	# prints "un", "deux" & "trois"

Again, note use of @ as we are accessing more than one value from the hash.

Setting Hash Values

$hash{foo} = 'something';
$hash{bar} = 'something else';

Also with slices
  @hash{'foo', 'bar'} = ('something', 'else');
  @hash{'foo', 'bar'} = @hash{'bar', 'foo'};

The Default Variable

Many Perl operations either set $_ or use its value if no other is given

print; # prints the value of $_

If a piece of Perl code seems to be missing a variable, then it's probably using $_

Think of “it” or “that” in English

A Special Array

@ARGV - Contains your programs command line arguments

my $num = @ARGV;
print "$num arguments: @ARGV\n";

c:\perl\bin\perl '' 'foo' 'bar' 'baz'

Input and Output

Programs become more useful with input and output

print 'What is your name: ';

$name = <STDIN>;
print “Hello $name”;


The easiest way to get data into a Perl program is to read from STDIN

$input = <STDIN>;

< ... > is the “read from filehandle” operator

STDIN is the standard input filehandle


The easiest way to get data out of a Perl program is to use print

print “Hello world\n”;


Arithmetic Operators

Standard arithmetic operations

add (+), subtract (-), multiply (*), divide (/)

Less standard operations modulus (%), exponentiation (**)

$speed = $distance / $time;
$vol = $length * $breadth * $height;
$area = $pi * ($radius ** 2);
$odd = $number % 2;

Shortcut Operators

Often need to do things like

$total = $total + $amount;

Can be abbreviated to

$total += $amount;

Even shorter

$x++; # same as $x += 1 or $x = $x + 1
$y--; # same as $y -= 1 or $y = $y - 1

Subtle difference between $x++ and ++$x

String Operators

Concaternation (.)

$name = $firstname . ' ' .  $surname;

Repetition (x)

$line = '-' x 80;
$police = 'hello ' x 3;

Shortcut versions available

$page .= $line; # $page = $page . $line
$thing x= $i;   # $thing = $thing x $i

File Test Operators

Check various attributes of a file

-e $file does the file exist
-r $file is the file readable
-w $file is the file writeable
-d $file is the file a directory
-f $file is the file a normal file
-T $file is a text file
-B $file is a binary file


Function Return Values

Functions can return scalars or lists (or nothing)

$age = 29.75;
$years = int($age);
@list = ('a', 'random',
         'collection', 'of',
@sorted = sort(@list);
# a collection of random words

String Functions

length returns the length of a string

$len = length $a_string;

uc and lc return upper and lower case versions of a string

$string = 'MiXeD CaSe';
print "$string\n", uc $string,
      "\n", lc $string;

chop removes the last character from a string and returns it

$word = 'word';
$letter = chop $word;

chomp removes the last character only if it is a newline and returns true or false appropriately


substr returns substrings from a string

$string = 'Hello world';
print substr($string, 0, 5); # prints 'Hello'

Unlike many other languages you can assign to a substring

substr($string, 0, 5) = 'Greetings';
print $string;
# prints 'Greetings world'

Numeric Functions

  • cos, sin, tan standard trigonometric functions
  • exp exponentiation using e
  • log logarithm to base e
  • rand returns a random number
  • sqrt returns the square root

Array Manipulation

push adds a new element to the end of an array

push @array, $value;

pop removes and returns the last element in an array

$value = pop @array;

shift and unshift do the same for the start of an array

sort returns a sorted list (it does not sort the list in place)

@sorted = sort @array;

sort does a lot more besides, see the docs (perldoc -f sort)

reverse returns a reversed list
  @reverse = reverse @array;

Arrays and Strings

join takes an array and returns a string

@array = (1 .. 5);
$string = join ', ', @array; 
# $string is '1, 2, 3, 4, 5'

split takes a string and converts it into an array

$string = '1~2~3~4~5';
@array = split(/~/, $string);
# @array is (1, 2, 3, 4, 5)

Hash Functions

  • delete removes a key/value pair from a hash
  • exists tells you if an element exists in a hash
  • keys returns a list of all the keys in a hash
  • values returns a list of all the values in a hash

Time Functions

time returns the number of seconds since midnight Jan 1st 1970

$now = time;

localtime converts that into more usable values

($sec, $min, $hour, $mday, $mon, 
 $year, $wday, $yday, $isdst)
  = localtime($now);

It's common to use localtime on the current time

@time_bits = localtime(time);

Call to time can be omitted

@time_bits = localtime;

Use array slices

($d, $m, $y) =
   (localtime)[3 .. 5];

You can get formatted dates by fiddling the return values from localtime

Easier to use strftime (from

print strftime('%Y-%m-%d', localtime);

Format followed by list of date/time values. Format is POSIX standard Like UNIX date command

print strftime ('%d %B %y', localtime);

File Operations

open opens a file and associates it with a filehandle

open(my $file, '<', 'in.txt');

You can then read the file with <$file>

	$line  = <$file>; # one line
	@lines = <$file>; # all lines

Finally, close the file with close

read to read a fixed number of bytes into a buffer

$bytes = read(FILE, $buffer, 1024);
seek to move to a random position in a file
seek(FILE, 0, 0);

tell to get current file position

$where = tell FILE;
truncate to truncate file to given size
truncate FILE, $where;

Writing to Files

Open file in write mode
	open my $file, '>', 'out.txt';
# overwrite
	open my $file, '>>', 'out.txt';
# append
Write to file using print
	print $file “some data\n”;
Note lack of comma

Conditional Constructs

Comparison Operators

Compare two values in some way

Are they equal
	$x == $y or $x eq $y
	$x != $y or $x ne $y
Is one greater than another
	$x > $y or $x gt $y
	$x >= $y or $x ge $y
Also < (lt) and <= (le)

62 > 42             # true
'0' == (3 * 2) - 6  # true
'apple' gt 'banana' # false
'apple' == 'banana' # true(!)
1 + 2 == '3 bears'  # true
1 + 3 == 'three'    # false

Boolean Operators

Combine two or more conditional expressions into one

EXPR_1 and EXPR_2
	true if both EXPR_1 and EXPR_2 are true

EXPR_1 or EXPR_2
	true if either EXPR_1 or _EXPR_2 are true
Alternative syntax && for and and || for or
Different precedence though

if function

if - our first conditional

if ($name eq 'Developer') {

if … else …

if … else … - an extended if

if ($name eq 'Developer') {
	} else {
		die "Game over!\n";

if … elsif … else …

if … elsif … else … - even more control

if ($name eq 'Developer') {
	} elsif ($tardis_location
			eq $here) {
	} else {
	die "Game over!\n";


while - repeat the same code

while ($dalek_prisoners) {
  print "Mahendran\n";

next – jump to next iteration of loop
last – jump out of loop
redo – jump to start of same iteration of loop


until - the opposite of while

until ($regenerations == 12) {
  print "Regenerating\n";


for - more complex loops

for ($i = 1; $i <= 10; $i++) {
  print "$i squared is ",
        $i * $i, "\n";


foreach - simpler looping over lists

foreach $i (1 .. 10) {
  print "$i squared is ", 
        $i * $i, "\n";

my %months = (Jan => 31, Feb => 28,
              Mar => 31, Apr => 30,
              May => 31, Jun => 30,
              … );
foreach (keys %months) {
  print "$_ has $months{$_} days\n";


Self-contained "mini-programs" within your program

Make it easy to repeat code

Subroutines have a name and a block of code

sub NAME {

sub example {
  print "Mahendran!!\n";

Calling a Subroutine

example; # last one only works if function has been predeclared

Subroutine Arguments

Arguments end up in the @_ array within the function

example('Perl Developer');
sub example {
  my ($name) = @_;
	print "Mahendran $name\n";

Multiple Arguments

As @_ is an array it can contain multiple arguments

sub example {
  foreach (@_) {
    print "Mahendran $_\n";

Calling Subroutines

A subtle difference between &my_sub and my_sub()

&my_sub passes on the contents of @_ to the called subroutine

sub first { &second };
	sub second { print @_ };
	first('some', 'random', 'data');

Returning Values

Use return to return a value from a subroutine

sub example {
  if (rand > .25) {
    print "Mahendran $_[0]\n";
    return 1;
  } else {

Subroutines can return lists

sub example {
  my @exampled;
  foreach (@_) {
    if (rand > .25) {
      print "Mahendran $_\n";
      push @exampled, $_;
  return @exampled;

Perl Overview Cheat


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

Data Types

$s='Hallo';   # scalar string
$nr=5;        # scalar decimal
$nr=1.456;    # scalar float point

@arr=(1,2,3);   # array of numbers
$#arr;          # (length-1) -> 2
$arr[0];        # gives '1'
$arr[1];        # gives '2'
$arr[2];        # gives '3'
$arr[-1];       # gives '3';

$h{1};          # gives 'joe'
$h{3}='dog';    # adding {3 => dog}

$ref=\$nr;    # scalar ref
$$ref;        # resolve scalar ref
$ref=\%h;     # hash ref
%$ref;        # resolve hash ref

Variable Scopes

my $i;        # local private var
local $s;     # dynamic variable
local FILE;   # local file handle

Control Flow

if($a == 1) {     unless($i >= 55) {
   doThis();         doThat();
} else {          } else {
   doThat();         doThis();
}                 }

exit if($o ne 'false');
exit unless($o eq 'false');

while($i > 5 && $i &lt; 25) 
for($i=0;$i<100;$i++) {
   print $i**;

Working with Arrays

@arr = split /,/, $text;
$text = join ',', @a;

@arr = sort @arr;
@arr = reverse @arr;

push @arr, $i; $i = pop @arr;
unshift @arr, $i; $i = shift @arr;

foreach my $i (@arr) {
   print $i;

Working with Hashes

foreach my $s (keys %h) {
   print $s.„=>“.$h{$s}.„\n“;

@arr = values(%h);

Using Modules

use XML::Parser;
my $p = new XML::Parser();

require Time::Local;
print localtime();

Working with Files

# reading a file in one step
open FILE, '/etc/hosts';
@hosts = <FILE>;
close FILE

# reading and writing per line
open(FILE1, '<$filename1');
open(FILE2, '>$filename2');
print FILE2, $_ while(<FILE1>);

Write Your Comments or Suggestion...