The basics of Perl

This is a short introduction to some basics you will need to know before you should get started coding.

Download the code examples used on this page as a single working example file PerlBasics.pl.

Variable assignments

In Perl variables are typed when they are created. Variables can contain numbers or text and Perl will automatically figure out what type of data is kept inside the variable. Your job is to figure out whether you need a single variable or an array stack of variables, whether you need to use this variable globally or if you need to use it locally.

The most common assignment is a single equal sign. =

#!/usr/bin/perl -w
 
# This is a local variable that only contains one value, it happens to be a string of text.
my $JustALocalVariable = "Users name goes here";
 
# This is a local array and it contains many variables as strings of text
my @ListOfUsers = ("Kim", "Tom", "Ed", "Mark");
print "User 3 is ".$ListOfUsers[2]."\n";
# That line prints Ed to the command line. We print the name at index 2 because the first entry in the list is in position 0 not 1.
 
# This is a local hash variable. It works like an array, it is a stack of variables but you can use a name to access the variable rather then it's position in the list.
$NickNames->{'Kim'} = "Fast taker";
$NickNames->{'Tom'} = "The Doctor";
$NickNames->{'Ed'} = "Mister Giggles";
$NickNames->{'Mark'} = "Boss Man";
print "Mark likes to be called ".$NickNames->{'Mark'}."\n";

Here is an excellent site that talks more about hash variables in Perl.

Conditions

To test the value of a variable we use conditions. The most common condition test is with if

An equal condition test uses a double equal sign == not a single. Single equal sign is used for assignment and a double is used for a conditional test.

$A = 6;
 
if ( $A > 9 )
{
   # This condition evaluates to FALSE
   print "This line will never happen because A is not greater then 9\n";
}
if ( $A < 7 )
{
   # This condition evaluates to TRUE
   print "Look, A is less then 7\n";
}
if ( $A < 7 )
{
   # This condition evaluates to TRUE
   print "Look, A is less then 7\n";
}
if ( $A == 6 )
{
   print "A is equal to 6\n";
}
if ( $A == "6" )
{
   print "Perl sees \"6\" a string and 6 an integer as the same when using == to test\n";
}

When testing strings you cannot use the equal sign because text does not directly evaluate to a number so Perl has a special operator to test strings.

$Name = "Joe";
if ($Name eq "Tom" ) 
{
   print "Hello Tom\n";
}
if ($Name eq "Joe" ) 
{
   print "Hello Joe, you just missed Tom.\n";
}

Loops

Loops let us program things we want to happen again and again until a condition is met.

For loops

A for loop has three sections separated by a semicolon before the code block.

  1. Initialize a value
  2. Breakout condition
  3. Step iteration

This simple example will count to ten using three sections

  1. Initialize a value $A = 0 $A starts at zero
  2. Breakout condition $A < 10 keep going as long as $A is less then 10
  3. Step iteration $A++ add one to $A each time
for(my $A = 0; $A < 10; $A++)
{
    print "A = $A\n";
}

Foreach loops

The foreach is a special command in Perl that lets you iterate through an array. Typically the array is stack of string values. Each value in the array is automatically assigned to a local variable for you and the loop automatically exits when all the entries in the array have been evaluated.

@Staff = ("Kim", "Tom", "Danny", "Linda", "Roger");
foreach $Name (@Staff)
{
    print "Say hello to $Name\n";
}

While loops

A while loop only tests a condition before the code block. This example reads the arguments given to a program as options on the command line.

use Switch;
while ($#ARGV > -1)
{
        $Argument = shift @ARGV;
        switch($Argument)
        {
                case "-h"
                {
                        GoPrintHelpInfo();
                }
 
                case "-v"
                {
                        GoPrintVersionInfo();
                }
        }
}

A while loop will not execute the code if the condition evaluates to false or zero.

while ( 1 == 5 )
{
    print "This line will never be executed because 1 is never equal to 5.\n";
}
while ( 1 == 1 )
{
    print "This line will run for ever because 1 is always equal to 1.\n";
}

Regular Expressions

Perl has some cool commands that make it faster to find, replace and generally mangle text.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License