First Perl Program

A perl script ends with .pl extension. The first line in the script gives the full path to where the perl interpreter is stored on that machine. On the CS machines that path is /lusr/bin/perl. Here is a program that prints Hello World.

#!/lusr/bin/perl

print "Hello World! \n";

The program is stored in a file called hello.pl. To run the program it has to be made into an executable file first.

chmod +x hello.pl

perl hello.pl

OR

./hello.pl

Variables

In Perl a variable could be a scalar, array, or a hash. Each variable type is preceded by a unique symbol. $ for scalar, @ for array, and % for hash. Variables names begin with letters followed by letters or digits. The variable $_ has a special meaning in Perl.

Scalar variables represent numbers or strings. There is no integer representation. All numbers are represented as double precision. 0 starts an octal number and 0x a hexadecimal number.

$a = 2.5;
$b = 056;
$c = 0xAF;

Strings can be expressed with single quotes or double quotes. Expressions and variables are interpreted if they are within double quotes, whereas they are printed literally if they are in single quotes.

List is ordered scalar data. An array is a variable that holds a list. An array can have any number of elements. The elements could be both numbers or strings. The size of the array does not have to be declared. To define an array do:

@a = ("abc", "def", "ghi");
@b = (1.2, 3.4, 5.6);
Arrays start with zero index. Elements in the array are treated as scalar quantities.
$a[1] = "pqr";
This assignment replaces the string def with the string pqr. To obtain the length of the array you can assign the array to a scalar variable.
$length = @a;
The variable $length now has the value 3 which is the length of the array @a.

There are several functions to manipulate arrays.

Operators

The arithmetic operators for numbers are:

The String operators are:

The comparison or relational operators are:
OperationNumerical Operator String Operator
Less than < lt
Less than equal to <= le
Greater than > gt
Greater than or equal to >= ge
Equal to == eq
Not equal to != ne

The boolean operators are:
Operation Symbol
NOT !
AND &&
OR ||

Input and Output

To output the value of a variable do:

print $var, "\n";
To produce a formatted output use the same style of formatting as in C:
print ("%20s  %10.5f \n", $str, $num);

To read input from the terminal do:

$a = ;
chomp ($a);
The function chomp() removes new line characters. The function chop() removes just the last character.

Conditionals

There are no boolean variables in Perl. If the expression has a numerical value then 0 evaluates to false and any other number is true. If the expression is a string, then a blank string is false and any other string is true.

The if-else-then statement has the following form:

if (condition1)
{
  statement1;
}
elsif (condition2)
{
  statement2;
}
else
{
  statement3;
}
There are no switch statements in Perl. But there is unless-else statement. The block of code associated with the unless will get executed if the condition is false.
unless (condition)
{
  statement1;
}
else
{
  statement2;
}

Loops

The basic loop is the while loop.

while (condition)
{
  statement;
}
Sometimes it is easier to say until something is true rather than while not this is true. Perl has an until loop that executes as long as the condition is false.
until (condition)
{
  statement;
}
There is also the do-while loop that gets executed at least once even if the condition is false to start with.
do
{
  statement;
} while (condition);
There is also the for loop that works the same way as it does in Java.
for (intial_exp; condition; increment)
{
  statement;
}
There is also another loop, the foreach loop, that can be used to iterate through an array.
foreach $i (@a)
{
  statement;
}

Subroutines

File Input / Output

Perl Builtin Functions