Programs are composed of statements often grouped together into blocks.
A statement ends with a semicolon (;), which is optional for the last statement in a block.
A block is one or more statements usually surrounded by curly braces; here's an example:
$thousand = 1000;
Blocks may stand by themselves but are often associated with such constructs as loops or if statements.
Arrays are ordered collections of zero or more scalar values, indexed by position. An array variable begins with the at sign @ followed by a legal variable name. For instance, here are two possible array variable names:
You can assign scalar values to an array by placing the scalar values in a list, separated by commas and surrounded by a pair of parentheses. For instance, you can assign an array the empty list:
@array = ( );
or one or more scalar values:
@dna_fragments = ('ACGT', $fragment2, 'GGCGGA');
Notice that it's okay to specify a scalar variable such as $fragment2 in a list. Its current value, not the variable name, is placed into the array.
The individual scalar values of an array (the elements) are indexed by their position in the array. The index numbers begin at 0. You can specify the individual elements of an array by preceding the array name by a $ and following it with the index number of the element within square brackets [ ], like so:
This equals the value of 'GGCGGA', given the values previously set for this array. Notice that the array has three scalar values, indexed by numbers 0, 1, and 2. The third and last element is indexed 2, one less than the total number of elements 3, because the first element is indexed number 0.
You can make a copy of an array using an assignment operator =, as in this example that makes a copy @output of an existing array @input:
@output = @input;
If you evaluate an array in scalar context, the value is the number of elements in the array. So if array @input has five elements, the following example assigns the value 5 to $count:
$count = @input;
Figure B-1 shows an array @myarray with three elements, which demonstrates the ordered nature of an array; by which each element appears, and can be found by its position in the array.
Figure B-1. Schematic of an array
A hash (also called an associative array) is a collection of zero or more pairs of scalar values, called keys and values. The values are indexed by the keys. An array variable begins with the percent sign % followed by a legal variable name. For instance, possible hash variable names are:
You can assign a value to a key with a simple assignment statement. For example, say you have a hash called %baseball_stadiums and a key Phillies to which you want to assign the value Veterans Stadium. This statement accomplishes the assignment:
Note that a single hash value is referenced by a $ instead of a % at the beginning of the hash name; this is similar to the way you reference individual array values by using a $ instead of a @.
You can assign several keys and values to a hash by placing their scalar values in a list, separated by commas and surrounded by a pair of parentheses. Each successive pair of scalars becomes a key and a value in the hash. For instance, you can assign a hash the empty list:
%hash = ( );
You can also assign one or more scalar key-value pairs:
Operators are functions that represent basic operations on values: addition, subtraction, etc. They are frequently used and are core parts of the Perl programming language. They are really just functions that take arguments. For instance, + is the operator that adds two numbers, like so:
3 + 4;
Operators typically have one, two, or three operands; in the example just given, there are two operands 3 and 4.
Operators can appear before, between, or after their operands. For example, the plus operator + appears between its operands.
B.9 Operator Precedence
Operator precedence determines the order in which the operations are applied. For instance, in Perl, the expression:
3 + 3 * 4
isn't evaluated left to right, which calculates 3 + 3 equals 6, and 6 times 4 results in a value of 24; the precedence rules cause the multiplication to be applied first, for a final result of 15. The precedence rules are available in the perlop manual page and in most Perl books. However, I recommend you use parentheses to make your code more readable and to avoid bugs. They make these expressions unambiguous; the first:
(3 + 3) * 4
evaluates to 24, and the second:
3 + (3 * 4)
evaluates to 15.
B.10 Basic Operators
For more information on how operators work, consult the perlop documentation bundled with Perl.
B.10.1 Arithmetic Operators
Perl has the basic five arithmetic operators:
These operators work on both integers and floating-point values (and if you're not careful, strings as well).
Perl also has a modulus operator, which computes the remainder of two integers:
For example, 17 % 3 is 2, because 2 is left over when you divide 3 into 17.
Perl also has autoincrement and autodecrement operators:
++ add one
-- subtract one
Unlike the previous six operators, these change a variable's value. $x++ adds one to $x, changing 4 to 5 (or a to b).
All scalars, whether numbers or strings, are represented as sequences of individual bits "under the hood." Every once in a while, you need to manipulate those bits, and Perl provides five operators to help:
B.10.3 String Operators
Two strings may be concatenated—joined together end to end—with the dot operator:
'This is a ' . 'joined string'
This results in the value 'This is a joined string'.
A string may be repeated with the x operator:
print "Hear ye! " x 3;
This prints out:
Hear ye! Hear ye! Hear ye!
B.10.4 File Test Operators
File test operators are unary operators that test files for certain characteristics, such as -e $file, which returns true if the file $file exists. Table B-2 lists some available file test operators.