1. Variables and their Related Operators

Perl provides three kinds of variables: scalars, arrays, and associative arrays. The discussion includes the designation of these three types and the basic operators provided by Perl for their manipulation.


1.1 Variables

The things Perl makes available to the programmer to work with are variables. Unlike many other programming languages, Perl does not require separate declaration of variables; they are defined implicitly within expressions, such as an assignment statement.

Perl provides three kinds of variables: scalars, arrays, and associative arrays. The initial character of the name identifies the particular type of variable and, hence, its functionality.

$name

scalar variable, either a number or string; Perl does not differentiate between the two, nor does it differentiate between integers and reals.
$aVar = 4;
$bVar = "a string of words";
$cVar = 4.5; # a decimal number
$dVar = 3.14e10; # a floatingpoint number

@name()

array ; a one-dimensional list of scalars. Perl uses the "at" symbol and parentheses with respect to the name of an array as a whole, whereas individual elements within an array are referred to as scalars and the index is placed in square brackets.
@aList = (2, 4, 6, 8);
@bList = @aList; # creates new array and gives it values of @aList
$aList[0] = 1; # changes the value of first item from 2 to 1

%name{}

associative array ; a special, 2-dimensional array, ideal for handling attribute/value pairs. The first element in each row is a key and the second element is a value associated with that key. Perl uses the "percent" symbol and curly braces with respect to the name of an associative array as a whole, whereas individual elements within an array are referred to as scalars, although the index is still placed in curly braces (unlike the shift in nomenclature used for arrays). Instead of using numbers to index an associative array, key values, such as $name{"QUERY_STRING"}, are used to reference the value associated with that particular key, i.e., QUERY_STRING. Since the associated value is a scalar, the variable has a $ prefix.
$aAA{"A"} = 1;  # creates first row of assoc. array
$aAA{"B"} = 2;  # creates second row of assoc. array
%bAA = %aAA;  # creates new assoc. array and gives it values of %aAA
$aAA{"A"} = 3;  # changes the value of first item from 1 to 3
%aAA = ("A", 1, "B", 2);  # same as first two stmts., above

1.2 Operators

If variables are the nouns Perl provides, operators are the verbs. Operators access and change the values of variables. Some, such as assignment, apply to all three kinds of variables, discussed above; however, most are specialized with respect to a particular type. Consequently, operators will be discussed with respect to the three basic types of variables.
1.2.1 Scalar Operators
assignment

see above

hex and octal assignment
$aVar1 = 0xff; # hex assign. for 255 decimal
$aVar2 = 0377; # octal assign. for same thing
single and double quote strings
$aVar1 = 0xff; # set $aVar1 = 255 decimal
$aVar2 = 'aVar2 = $aVar1'; # set $aVar2 = literal string
$aVar3 = "aVar3 = $aVar1"; # set $aVar3 = variable interpolated string, with $aVar1 replaced by 255
$aVar4 = 'only single quote interpolated characters are \' and \\'
double quote interpolated characters include:
\n newline \a bell \\ backslash \" double quote \l lowercase next letter \u uppercase next letter \L lowercase letters follow \U uppercase letters follow \E terminate \L or \E
operators for numbers
+ plus - minus * multiply / divide ** exponentiation % modulus # e.g., 7 % 3 = 1 == equal != not equal < less than > greater than <= less than or equal to >= greater than or equal to += binary assignment # e.g., $A += 1; -= same, subtraction *= same, multiplication ++ autoincrement # e.g., ++$A; also, $A++ -- autodecrement
operators for strings
. concatenate x n repetition # e.g., "A" x 3 => "AAA" eq equal ne not equal lt less than gt grater than le less than or equal to ge greater than or equal to chop() # remove last character in string index ($string, $substring) # position of substring in string, zero-based; -1 if not found index ($string, $substring, $skip) # skip number of chars substr($string, $start, $length) # substring substr($string, -$start, $length) # defined from end substr($string, $start) # rest of string
conversion between numbers and strings

Automatic, determined by the operator, if reasonable (e.g., "1.23" as string converts to 1.23 as number). If unreasonable, string converts to zero (0) as number (e.g., "not_a_number" converts to 0).

conversion between packed and unpacked forms

It is often necessary to convert from a character or scalar form to a packed binary representation, and back. A common example is building an IP address data structure. The two operators for doing this are pack and unpack. Pack takes a format specification and a list of values and packs them into a character string; conversely, unpack takes a format and a character string and breaks the string apart, according to the format, and assigns the parts to a list of variables.

Form:

pack("format", $value1, $value2, . . .); unpack ("format", character_string);

Example:

$IP = pack("CCCC", 152, 2, 128, 184); # create IP address ($var1, $var2, $var3, $var4) = unpack("CCCC", $IP); # inverse of the above

Format specifications can be given in context (in quotes) or they can be assigned to a string variable. There are a number of options available. See a standard text or the Perl man page for a complete list. In the example above, the "C" stands for an unsigned character value. One useful format to know is the following, which can be used to construct the address structure needed to bind a socket to a remote host:

$socket_addr_ptrn = 'S n a4 x8';

The "S" denotes a "short" unsigned integer. The "n" is a short integer in network order. The "a4" is an unpadded ASCII string, four bytes long. And, the "x8" is eight bytes of padding.

<STDIN> as scalar

Designates the next line of text from standard input.
1.2.2 Array Operators
assignment
@aList = (2, 4, 6, 8); # explicit values @aList = (1..4); # range of values @aList = (1, "two", 3, "four"); # mixed values @aList = (); # empty list @bList = @aList;

access

Individual items in array accessed as scalars.
$aList[0] # first item in @aList $aList[0,1] # slice, first two items in @aList $aList[$too_big] # access beyond array bounds returns undef, i.e., 0 or ''

additional operators

$#aList # index of last item push (@aList, $aNewItem); # @aList = @aList, $aNewItem $LastItem = pop (@aList); # inverse of push unshift (@aList, $aNewItem); # @aList = $aNewItem, @aList $FirstItem = shift (@aList); # inverse of unshift @aList = reverse (@aList); # reverse items @aList = sort (@aList); # sort items, alphabetically chop (@aList); # remove last character from each item @aList = <STDIN>; # one line of input per item
1.2.3 Associative Array Operators
assignment
$aAA{"A"} = 1; # creates first row of assoc. array $aAA{"B"} = 2; # creates second row of assoc. array %aAA = ("A", 1, "B", 2); # same as first two stmts., above %bAA = %aAA; # creates new assoc. array and gives it values of %aAA $aAA{"A"} = 3; # changes the value of first item from 1 to 3
additional operators
keys (%aAA) # list of keys for %aAA values (%aAA) # list of values for %aAA each (%aAA) # next key/value pair, as list delete $aAA{"A"}; # deletes key/value pair referenced