Main index

Introducing UNIX and Linux


Perl

Overview
Introduction
      Why yet another utility?
      Beginning Perl
      Invoking Perl
      Documentation on perl
      Perl Scripts
Variables
Input and output
      Files and redirection
      Pipes
      The DATA filehandle
Fields
Control structures
Predefined Perl
      Functions
      Modules
Regular expressions
      Single character translation
      String editing
Perl and the Kernel
Quality code
When do I use Perl?
Summary
Exercises

Variables

In the shell (and Awk also), there is only one type of variable, which is a string of characters. If those characters are digits, and the variable is used in a context where a number is expected, then the variable will be understood to be a number. In Awk, arrays are also provided for.

In the shell, the value of a variable is accessed by placing a dollar symbol in front of the name of the variable, but its value is set by just using the name. In Awk, the dollar symbol is not needed at all.

In Perl, the situation is more complex. First of all we consider scalar variables. These are similar to variables you have already met, and can contain strings of characters, which are interpreted appropriately. The difference between Perl and shell/Awk variables is that the name of a scalar variable is always prepended with a dollar. For example, to set the value of $i to 0 and the value of $i to $i+1:

$i=0;
$x=$i+1;

Arrays are indicated by the @ symbol before the name, and square brackets are used to access individual elements of an array, which must be indexed by integers commencing with 0. Individual elements of an array, since they are scalars, are prepended with a $. A predefined array you will often use is @ARGV, which is the list of arguments to the Perl program. Its first element is $ARGV[0].

In Awk, an array could be indexed by any string, and we referred to such an array as an associative array. In Perl it is called a hash. It is distinguished from an array in three ways. First, the @ symbol is replaced by % to refer to the whole hash. Second, square brackets are replaced by braces, and finally the indices are strings, not integers.

Simple use of Perl seldom requires the @ and % symbols, since most of the time we access the individual elements of the arrays directly. As an example, suppose array @month is an array of 12 strings containing the names of the months, and %age is a hash containing the ages of some people. The following fragments of code set the first and last elements of @month and the ages for Chris and Sam.

$month[0]='January';
$month[11]='December';
$age{'Chris'}=22;
$age{'Sam'}=19;

Either single or double quotes can be used to denote strings, but like the shell, dollars and escape characters will be interpreted within double quotes but not within single quotes.

Strings can be concatenated - that is, joined together - by use of the operator . ("dot"). For instance, the concatenation of strings

"Hello " . "world!"

is equivalent to the single string

"Hello world!"


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck