The University of Texas at Austin- What Starts Here Changes the World
Services Navigation


Perl Basics

Comments

Comments are notes to human readers of a script. The Perl interpreter ignores them. A Perl comment is indicated by a #, and anything which appears to the right of a # on a line of Perl code is a comment. In the following example, the only portion of the code, which isn't a comment, is the print statement.

# this whole line is a comment
print "not a comment"; # but the rest of the line is a comment

Variables

Programming languages use variables to store values. In Perl, variables can hold two kinds of data: numeric and string.

Numeric data stores numbers as their numeric values.

String data, or strings, are groups of characters. In Perl, string data is enclosed within single or double quotation marks. Most of the values in this tutorial are strings. In a string, a number is treated as characters, rather than as its numeric value. The following are considered strings in Perl: "marmoset", "1984", "llamas in pajamas", and "Class of 95".

In all of the tutorial examples and in most Perl scripts it is not necessary to specify if a number should be treated as numeric data or string data, since Perl generally treats the data correctly.

Perl has three kinds of variables: scalar, array, and hash.

Scalar

Scalar variables are used to hold a single numeric or string value. Scalar variable names begin with a $ followed by a letter, which can be followed by letters, numbers, or underscores. Values are assigned to scalar variables with the = operator.

$mammal = 'marmoset'; # the value of $mammal is "marmoset"
$book = '1984';
$phrase = 'llamas in pajamas';
$class = 'Class of 95';

Perl distinguishes between single and double-quoted strings. Single quoted strings contain text exactly as it appears. Double-quoted strings support variable interpolation and the backslash character as a special escape sequence to include codes within a string. The most common escape sequence is \n, which represents the newline character. In the example below, the value of $foo is interpolated inside the double quotes so $s3 equals the word test followed by the value of $foo.

$foo = 'fails'; # assigns fails to the $foo scalar variable
$s1 = "test\n"; # $s1 equals test followed by newline character
$s2 = 'test\n'; # $s2 equals test followed by \ followed by n
$s3 = "test $foo\n"; # $s3 equals "test fails"(the value of $foo) followed by newline character

In general, use single-quoted strings unless you need variable interpolation or specific escape sequences like newline (\n) or tab (\t).

Array

Arrays are lists of scalar values. Array variable names begin with an @ followed by a letter, which can be followed by letters, numbers, or underscores.

A comma-delimited list of values can be assigned to an array with the = operator.

@mammals = ("marmoset","walrus","alpaca","giraffe");
@examples = ($mammal, $book, $phrase, $class); # this could also be written as:
@examples = ("marmoset","1984","llamas in pajamas","Class of 95");

Note, that when defining a list of strings, the commas appear after the closing quote mark.

The number for their positions in the list can identify individual elements of an array. The first item in the list is in the 0 (zero) position, the second item in the list is in the 1 (one) position. Since individual items of an array are scalar values, the variable name for one element of an array begins with a $. The position in the array is indicated within square brackets [], following the name of the array.

$mammals[0]; # the value of this would be "marmoset"
$mammals[1]; # the value of this would be "walrus"
$mammals[2]; # the value of this would be "alpaca"
$mammals[3]; # the value of this would be "giraffe"

Hash

Hashes are a special kind of list in which the elements are grouped in pairs, and the first item in the pair is the "key" for the second item, which is the "value". Hashes begin with a % followed by a letter, which is followed by letters, numbers, or underscores.

Just like an array, a hash can be defined as a comma-delimited list

%animal_youngsters = ("goose","gosling","deer","fawn","pig","piglet");

The pairs of items in a hash are more obvious when an individual element of the hash is defined. The value of an element in a hash is a scalar value, so it starts with $. The value is defined by the name of the hash followed by the name of the key in {}.

$animal_youngsters{goose}; #this would be evaluated as "gosling"
$animal_youngsters{deer}; #this would be evaluated as "fawn"
$animal_youngsters{pig}; #this would be evaluated as "piglet"

Conditional

Conditional statements, also known as IF-THEN statements indicate that an operation is to be performed only if a certain condition is met. The condition is defined with (), the operation is indicated by {}.

if ( $sky eq "blue" ) { # if the sky is blue
     $rain = "no"; # the value of $rain is "no"
}

Modules

Modules are special Perl programs that make doing things in Perl easier. One example is CGI.pm. CGI.pm is Perl module that contains some shortcuts for gathering information from a Web form.

Loops

Loops are a way to tell Perl to perform the same operation more than once. There are many kinds of loops in Perl, the "foreach" and the "while/until" loops are the most popular.

Foreach

A foreach loop directs Perl to perform the commands within {} for each element of the list or hash specified within ().

foreach $i (%in) { # for each element of the hash %in
     print "$i=$in{i}"; # print the stuff in quotes
}

While/Until

While and Until loops are closely related to each other. In the while loop, the commands within {} are performed as long as the control expression within () is true. In an until loop, the commands within {} are performed until the control expression within () is true.

If the initial value of x is less than three, the following are equivalent. If the initial value of x is greater than three the commands in the while loop will not be performed, and the until structure will create an infinite loop, since the control expression cannot be met by adding one to x.

while (x < 3) { # while x is less than three
     x=x+1; # increase x by one
}
until (x = 3) { # until x equals three
     x=x+1; # increase x by one
}

Special Characters

There are some character combinations in Perl that have special meanings when used in double-quoted strings. They begin with a backslash (\). The following table contains some of the most common characters.

\n Newline
\r Return
\t Tab
\" Double quote

Strings

Strings are collections of characters grouped together. In Perl, there are two ways to indicate strings: with single or double quotes.

A string surrounded by single quotes contains the exact characters indicated by the string, with two exceptions: to use a single quote or a backslash within a single-quoted string, it must be preceded by a backslash.

A string surrounded by double quotes can be used to include the values of variables. It allows certain special characters, indicated by a backslash, to be incorporated in the string.

Filehandles

Filehandles are special variables used to reference files. A filehandle is defined by the open function:

open (FILEHANDLE, file);

The open function also indicates in what manner a file is to be opened: read-only, replace, or append. To open a file so that it can be appended to, use >> before the name of the file. To open a file so that it can be replaced, use > before the name of the file. To open a file as read-only use just the filename. Here are three ways to assign the filehandle FOO to the file stuff:

open (FOO, ">>stuff"); # open the file stuff to append
open (FOO, ">stuff"); # open the file stuff to replace
open (FOO, "stuff"); # open the file stuff as read-only

If the file you wish to open is not in the same directory as the script you are writing, you will need to specify a relative path to the file. If the script is in a user's cgi-bin, to write to a file in the public_html/logs/ directory, use the following command:

open (FOO, ">>../logs/stuff"); # open the file public_html/logs/stuff to append

 

 

 


  Updated 2006 August 14
  Comments to TeamWeb