发信人: jerk (徐子陵), 信区: Unix
标 题: Unix Unleased -16
发信站: 饮水思源站 (Fri Nov 20 20:17:23 1998) , 站内信件
16
Perl
By David Till
Overview
What Is Perl?
Where Can I Get Perl?
A Simple Sample Program
Using Comments
Reading from Standard Input
Storing Values The Scalar Variable
Assigning a Value to a Scalar Variable
Scalar Variables Inside Character Strings
Writing to Standard Output
Working with Scalar Variables
Understanding Scalar Values
Using Octal and Hexadecimal Representation
Using Double- and Single-Quoted Strings
Using Floating-Point Values
Interchangeability of Strings and Numeric Values
Using Scalar Variable Operators
Performing Arithmetic
Performing Comparisons
Manipulating Bits
Using the Assignment Operators
Using Autoincrement and Autodecrement
Concatenating and Repeating Strings
Using Other C Operators
Matching Patterns
The Order of Operations
Using Lists and Array Variables
Introducing Lists
Scalar Variables and Lists
Using List Ranges
Storing Lists in Array Variables
Assigning to Array Variables
Using Array Variables in Lists
Assigning to Scalar Variables from Array Variables
Retrieving the Length of a List
Using Array Slices
Using Array Slices with Scalar Variables
Using the Array Slice Notation as a Shorthand
Other Array Operations
Sorting a List or Array Variable
Reversing a List or Array Variable
Using chop() on Array Variables
Creating a Single String from a List
Example: Sorting Words in a String
Using Command Line Arguments
Standard Input and Array Variables
Controlling Program Flow
Conditional Execution The if Statement
Two-Way Branching Using if and else
Multi-Way Branching Using elsif
Conditional Branching Using unless
Repeating Statements Using while and until
Using Single-Line Conditional Statements
Looping with the for Statement
Looping Through a List The foreach Statement
Exiting a Loop with the last Statement
Using next to Start the Next Iteration of a Loop
Using Labelled Blocks for Multi-Level Jumps
Terminating Execution Using die
Reading from and Writing to Files
Opening a File
Checking Whether the Open Succeeded
Reading from a File
Writing to a File
Closing a File
Determining the Status of a File
Reading from a Sequence of Files
Using Subroutines
Defining a Subroutine
Using a Subroutine
Returning a Value from a Subroutine
Using Local Variables
Passing Values to a Subroutine
Calling Subroutines from Other Subroutines
Associative Arrays
Defining Associative Arrays
Accessing Associative Arrays
Copying to and from Associative Arrays
Adding and Deleting Array Elements
Listing Array Indexes and Values
Looping with an Associative Array
Formatting Your Output
Defining a Print Format
Displaying a Print Format
Displaying Values in a Print Format
Choosing a Value Field Format
Writing to Other Output Files
Specifying a Page Header
Setting the Page Length
Formatting Long Character Strings
Using Built-In Functions
The $_ Variable
Summary
16
Perl
By David Till
Overview
The following sections tell you what Perl is and how you can get it, and provide
a short example of a working Perl program.
What Is Perl?
Perl is a simple yet useful programming language that provides the convenience
of shell scripts and the power and flexibility of high-level programming
languages. Perl programs are interpreted and executed directly, just as shell
scripts are; however, they also contain control structures and operators similar
to those found in the C programming language. This gives you the ability to
write useful programs in a very short time.
Where Can I Get Perl?
Perl is freeware: it can be obtained by file transfer (ftp) from the Free
Software Foundation at prep.ai.mit.edu (in the directory pub/gnu). Perl is also
available from several other sites on the Internet, including any site that
archives the newsgroup comp.sources.unix.
The Free Software Foundation gives you the right to obtain Perl and its source,
provided others have the right to obtain them from you. For more details on the
Free Software Foundation licensing policy, refer to the file
GETTING.GNU.SOFTWARE, also available from the foundation.
A Simple Sample Program
To show easy it is to use Perl, here is a simple program that echoes (writes
out) a line of input typed in at a terminal.
Listing 16.1. A Sample Perl Program.
#!/usr/bin/perl
$inputline = <STDIN>;
print ("$inputline");
To run this program, do the following:
Type in the program and save it in a file (say, foo).
Tell the system that this file contains executable statements. To do this,
enter the command chmod +x foo.
Run the program by entering the command foo.
If you receive the error message foo not found or some equivalent, either enter
the command ./foo or add the current directory . to your PATH environment
variable.
At this point, the program waits for you to type in an input line. Once you have
done so, it echoes your input line and exits.
The following sections describe each of the components of this simple program in
a little more detail.
Using Comments
The first line of this program is an example of a Perl comment. In Perl, anytime
a # character is recognized, the rest of the line is treated as a comment:
# this is a comment that takes up the whole line
$count = 0; # this part of the line is a comment
A comment appearing as the first line of a program is special. This header
comment indicates the location of the program interpreter to use. In this
example, the string !/usr/bin/perl indicates that this file is a Perl program.
The Perl interpreter should be located in /usr/bin/perl on your system. If it is
not, replace /usr/bin/perl in the header comment with the location of the Perl
interpreter on your system.
Reading from Standard Input
Like C, Perl recognizes the existence of the UNIX standard input file, standard
output file, and standard error file. In C, these files are called stdin, stdout
and stderr; in Perl, they are called STDIN, STDOUT and STDERR.
The Perl construct <STDIN> refers to a line of text read in from the standard
input file. This line of text includes the closing newline character.
Storing Values The Scalar Variable
The construct $inputline is an example of a scalar variable. A scalar variable
is a variable that holds exactly one value. This value can be a string, integer,
or floating point number.
All scalar variables start with a dollar sign, $. This distinguishes them from
other Perl variables. In a scalar variable, the character immediately following
the dollar sign must be a letter. Subsequent characters can be letters, digits,
or underscores. Scalar variable names can be as long as you like.
For more information on scalar variables and their values, see the section
"Working with Scalar Variables" later in this chapter.
Assigning a Value to a Scalar Variable
The statement $inputline = <STDIN>; contains the = character, which is the Perl
assignment operator. This statement tells Perl that the line of text read from
standard input, represented by <STDIN>, is to become the new value of the scalar
variable $inputline.
Perl provides a full set of useful arithmetic, logical, and string operators.
For details, refer to the sections "Working with Scalar Variables" and "Using
Lists and Array Variables" later in this chapter.
CAUTION: All scalar variables are given an initial value of the null string, "".
Therefore, a Perl program can be run even when a scalar variable is used before
a value has been assigned to it. Consider the statement
$b = $a;
This statement assigns the value of the variable $a to $b. If $a has not been
seen before, it is assumed to have the value "", and "" is assigned to $b. Since
this behavior is legal in Perl, you must check your programs for "undefined"
variables yourself.
Scalar Variables Inside Character Strings
The final statement of the program, print ("$inputline");, contains a character
string, which is a sequence of characters enclosed in double quotes. In this
case, the character string is "$inputline".
The string "$inputline" contains the name of a scalar variable, $inputline. When
Perl sees a variable inside a character string, it replaces the variable with
its value. In this example, the string "$inputline" is replaced with the line of
text read from the standard input file.
Writing to Standard Output
The built-in function print() writes its arguments (the items enclosed in
parentheses) to the standard output file. In this example, the statement print
("$inputline"); sends the contents of the scalar variable $inputline to the
standard output file.
The print() function can also be told to write to the standard error file or to
any other specified file. See the section "Reading from and Writing to Files"
later in this chapter for more details.
Working with Scalar Variables
Now that you know a little about Perl, it's time to describe the language in a
little more detail. This section starts you off by discussing scalar variables
and the values that can be stored in them.
Understanding Scalar Values
In Perl, a scalar value is any value that can be stored in a scalar variable.
The following are scalar values:
Integers
Double and single-quoted character strings
Floating-point values
The following assignments are all legal in Perl:
$variable = 1;
$variable = "this is a string";
$variable = 3.14159;
The following assignments are not legal:
$variable = 67M;
$variable = ^803;
$variable = $%$%!;
Using Octal and Hexadecimal Representation
Normally, integers are assumed to be in standard base 10 notation. Perl also
supports base 8 (octal) and base 16 (hexadecimal) notation.
To indicate that a number is in base 8, put a zero in front of the number:
$a = 0151; # 0151 octal is 105
To indicate base 16, put 0x (or 0X) in front of the number:
$a = 0x69; # 69 hex is also 105
The letters A through F (in either upper- or lowercase) represent the values 10
through 15:
$a = 0xFE; # equals 16 * 15 + 1 * 14, or 254
NOTE: Strings containing a leading 0 or 0x are not treated as base 8 or base 16:
$a = "0151";
$a = "0x69";
These strings are treated as character strings whose first character is "0."
Using Double- and Single-Quoted Strings
So far, all of the strings you have seen have been enclosed by the " (double
quotation mark) characters:
$a = "This is a string in double quotes";
Perl also allows you to enclose strings using the ' (single quotation mark)
character:
$a = 'This is a string in single quotes';
There are two differences between double-quoted strings and single-quoted
strings. The first difference is that variables are replaced by their values in
double-quoted strings, but not in single-quoted strings:
$x = "a string";
$y = "This is $x"; # becomes "This is a string"
$z = 'This is $x'; # remains 'This is $x'
Also, double-quoted strings recognize escape sequences for special characters.
These escape sequences consist of a backslash (\) followed by one or more
characters. The most common escape sequence is \n, representing the newline
character:
$a = "This is a string terminated by a newline\n";
Table 16.1 lists the escape sequences recognized in double-quoted strings.
Table 16.1. Escape Sequences in Double-Quoted Strings.
\abell (beep)
\bbackspace
\cnthe control-n character
\eescape
\fform feed
\lforce next letter into lowercase
\Lall following letters are lowercase
\nnewline
\rcarriage return
\ttab
\uforce next letter into uppercase
\Uall following letters are uppercase
\vvertical tab
\L and \U can be turned off by \E:
$a = "T\LHIS IS A \ESTRING"; # same as "This is a STRING"
To include a backslash or double quote in a double-quoted string, precede it
with another backslash:
$a = "A quote \" in a string";
$a = "A backslash \\ in a string";
You can specify the ASCII value for a character in base 8 or octal notation
using \nnn, where each n is an octal digit:
$a = "\377"; # this is the character 255, or EOF
You can also use hexadecimal to specify the ASCII value for a character. To do
this, use the sequence \xnn, where each n is a hexadecimal digit:
$a = "\xff"; # this is also 255
None of these escape sequences is supported in single-quoted strings, except for
\' and \\, which represent the single quote character and the backslash,
respectively:
$a = '\b is not a bell'
$a = 'a single quote \' in a string'
$a = 'a backslash \\ in a string'
NOTE: In Perl, strings are not terminated by a null character (ASCII 0), as they
are in C. In Perl, the null character can appear anywhere in a string:
$a = "This string \000 has a null character in it";
Using Floating-Point Values
Perl supports floating-point numbers in both conventional and scientific
notation. The letter E (or e) represents the power of ten to which a number in
scientific notation is to be raised.
$a = 11.3; # conventional notation
$a = 1.13E01; # 11.3 in scientific notation
$a = -1.13e-01; # the above divided by -10
CAUTION: Note that Perl uses your machine's floating point representation. This
means that only a certain number of digits (in mathematical terms, a certain
precision) are supported. For example, consider the following very short
program:
#!/usr/bin/perl
$pi = 3.14159265358979233;
print ("pi is $pi\n");
This program prints the following:
pi = 3.1415926535897922
This is because there just isn't room to keep track of all of the digits of pi
specified by the program.
This problem is made worse when arithmetic operations are performed on floating
point numbers; see "Performing Comparisons" for more information on this
problem.
Note that most programming languages, including C, have this problem.
Interchangeability of Strings and Numeric Values
In Perl, as you have seen, a scalar variable can be used to store a character
string, an integer, or a floating point value. In scalar variables, a value that
was assigned as a string can be used as an integer whenever it makes sense to do
so, and vice versa. For example, consider the program in file LIST 16_2 on the
CD-ROM, which converts distances from miles to kilometers and vice versa. In
this example, the scalar variable $originaldist contains the character string
read in from the standard input file. The contents of this string are then
treated as a number, multiplied by the miles-to-kilometers and
kilometers-to-miles conversion factors, and stored in $miles and $kilometers.
This program also contains a call to the function chop(). This function throws
away the last character in the specified string. In this case, chop() gets rid
of the newline character at the end of the input line.
If a string contains characters that are not digits, it is converted to 0:
# this assigns 0 to $a, since "hello" becomes 0
$a = "hello" * 5;
In cases like this, Perl does not tell you that anything has gone wrong, and
your results may not be what you expect.
Also, strings containing misprints yield unexpected results:
$a = "12O34"+1 # the letter O, not the number 0
When Perl sees a string in the middle of an expression, it converts the string
to an integer. To do this, it starts at the left of the string and continues
until it sees a letter that is not a digit. In this case, "12O34" is converted
to the integer 12, not 12034.
Using Scalar Variable Operators
The statement $miles = $originaldist * 0.6214; uses two scalar variable
operators: =, the assignment operator, which assigns a value to a variable, and
*, the multiplication operator, which multiplies two values.
Perl provides the complete set of operators found in C, plus a few others. These
operators are described in the following sections.
Performing Arithmetic
To do arithmetic in Perl, use the arithmetic operators.
Perl supports the following arithmetic operators:
$a = 15; # assignment: $a now has the value 15
$a = 4 + 5.1; # addition: $a is now 9.1
$a = 17 - 6.2; # subtraction: $a is now 10.8
$a = 2.1 * 6; # multiplication: $a is now 12.6
$a = 48 / 1.5; # division: $a is now 32
$a = 2 ** 3; # exponentiation: $a is now 8
$a = 21 % 5; # remainder (modulo): $a is now 1
$a = - $b; # arithmetic negation: $a is now $b * -1
Non-integral values are converted to integers before a remainder operation is
performed:
$a = 21.4 % 5.1; # identical to 21 % 5
Performing Comparisons
To compare two scalar values in Perl, use the logical operators.
Logical operators are divided into two classes: numeric and string. The
following numeric logical operators are defined:
11.0 < 16 # less than
16 > 11 # greater than
15 == 15 # equals
11.0 <= 16 # less than or equal to
16 >= 11 # greater than or equal to
15 != 14 # not equal to
$a || $b # logical OR: true if either is non-zero
$a && $b # logical AND: true only if both are non-zero
! $a # logical NOT: true if $a is zero
In each case, the result of the operation performed by a logical operator is
non-zero if true and zero if false, just like in C.
The expression on the left side of a || (logical or) operator is always tested
before the expression on the right side, and the expression on the right side is
only used when necessary. For example, consider the following expression:
$x == 0 || $y / $x > 5
Here, the expression on the left side of the ||, $x == 0, is tested first. If $x
is zero, the result is true regardless of the value of $y / $x > 5, so Perl
doesn't bother to compute this value. $y / $x > 5 is only evaluated if s is not
zero. This ensures that division by zero can never occur.
Similarly, the expression on the right side of a && operator is only tested if
the expression on the left side is true:
$x != 0 && $y / $x > 5
Once again, a division by zero error is impossible, because $y / $x > 5 is only
evaluated if $x is non-zero.
Perl also defines the <=> operator, which returns 0 if the two values are equal,
-1 if the left value is larger, and 1 if the right value is larger:
4 <=> 1 # returns -1
3 <=> 3.0 # returns 0
1 <=> 4.0 # returns 1
CAUTION: Be careful when you use floating point numbers in comparison
operations, because the result may not be what you expect. Consider the
following code fragment:
$val1 = 14.3;
$val2 = 100 + 14.3 - 100;
print "val1 is $val1, val2 is $val2\n";
On first examination, $val1 and $val2 appear to contain the same value, 14.3.
However, the print statement produces the following:
val1 is 14.300000000000001, val2 is 14.299999999999997
Adding and subtracting 100 affects the value stored in $val2 because of the way
floating point values are calculated and stored on the machine. As a result,
$val1 and $val2 are not the same, and $val1 == $val2 is not true.
This problem occurs in most programming languages (including C).
Besides the preceding numeric logical operators, Perl also provides logical
operators that work with strings:
"aaa" lt "bbb" # less than
"bbb" gt "aaa" # greater than
"aaa" eq "aaa" # equals
"aaa" le "bbb" # less than or equal to
"bbb" ge "aaa" # greater than or equal to
"aaa" ne "bbb" # not equal to
Perl also defines the cmp operator, which, like the numeric operator <=>,
returns -1, 0 or 1:
"aaa" cmp "bbb" # returns -1
"aaa" cmp "aaa" # returns 0
"bbb" cmp "aaa" # returns 1
This behavior is identical to that of the C function strcmp().
Note that the logical string operators perform string comparisons, not numeric
comparisons. For example, "40" lt "8" is true—if the two strings are sorted in
ascending order, "40" appears before "8".
Manipulating Bits
Any integer can always be represented in binary or base-2 notation, of course.
For example, the number 38 is equivalent to the binary value 100110: 32 plus 4
plus 2. Each 0 or 1 in this binary value is called a bit.
If a Perl scalar value happens to be an integer, Perl allows you to manipulate
the bits that make up that integer. To do this, use the Perl bitwise operators.
The following bitwise operators are supported in Perl:
The & (bitwise AND) operator
The | (bitwise OR) operator
The ^ (bitwise EXOR, or exclusive OR) operator
The ~ (bitwise NOT) operator
The << (left shift) and >> (right shift) operators
If a scalar value is not an integer, it is converted to an integer before a
bitwise operation is performed:
$a = 24.5 & 11.2 # identical to $a = 24 & 11
The & operator works as follows: first, it examines the values on either side of
the &. (These values are also known as the operands of the & operator.) These
values are examined in their binary representations. For example, consider the
following bitwise operation:
$a = 29 & 11;
In this case, the 29 is converted to 11101, and the 11 is converted to 01011. (A
binary representation can have as many leading zeroes as you like.)
Next, Perl compares each bit of the first operand with the corresponding bit in
the second operand:
11101
01011
In this case, only the second and fifth bits (from the left) of the two operands
are both 1; therefore, the binary representation of the result is 01001, or 9.
The | operator works in much the same way. The bits of the two operands are
compared one at a time; if a bit in the first operand is 1 or its corresponding
bit in the second operand is 1, the bit in the result is set to 1. Consider this
example:
$a = 25 | 11;
Here, the binary representations are 11001 and 01011. In this case, only the
third bits are both 0, and the result is 11011 or 27.
The ^ operator sets a result bit to 1 if exactly one of the corresponding bits
in an operand is 1. If both bits are 1 or both are 0, the result bit is set to
0. In the example $a = 25 ^ 11; the binary representations of the operands are
11001 and 01011, and the result is 10010, or 18.
The ~ operator works on one operand. Every 0 bit in the operand is changed to a
1 and vice versa. For example, consider the following:
$a = ~ 25;
Here, the binary representation of 25 is 11001. The result, therefore, is 00110,
or 6.
The << operator shifts the bits of the left operand the number of places
specified by the right operand, and fills the vacated places with zeroes:
$a = 29 << 2;
Here the value 29, whose binary representation is 11101, is shifted left two
positions. This produces the result 1110100, or 116.
Similarly, the >> operator shifts the bits rightward, with the rightmost bits
being lost:
$a = 29 >> 2;
In this case, 29, or 11101, is shifted right two places. The 01 on the end is
thrown away, and the result is 111, or 7.
Shifting left one bit is equivalent to multiplying by 2:
$a = 54 << 1; # this result is 108
$a = 54 * 2; # this result is also 108
Shifting right one bit is equivalent to dividing by 2:
$a = 54 >> 1; # this result is 27
$a = 54 / 2; # this result is also 27
Similarly, shifting left or right n bits is equivalent to multiplying or
dividing by 2**n.
Using the Assignment Operators
The most common assignment operator is the = operator, which you've already
seen:
$a = 9;
Here, the value 9 is assigned to the scalar variable $a.
Another common assignment operator is the += operator, which combines the
operations of addition and assignment:
$a = $a + 1; # this adds 1 to $a
$a += 1; # this also adds 1 to $a
Other assignment operators exist that correspond to the other arithmetic and
bitwise operators:
$a -= 1; # same as $a = $a - 1
$a *= 2; # same as $a = $a * 2
$a /= 2; # same as $a = $a / 2
$a %= 2; # same as $a = $a % 2
$a **= 2; # same as $a = $a ** 2
$a &= 2; # same as $a = $a & 2
$a |= 2; # same as $a = $a | 2
$a ^= 2; # same as $a = $a ^ 2
Using Autoincrement and Autodecrement
Another way to add 1 to a scalar variable is with the ++, or autoincrement,
operator:
++$a; # same as $a += 1 or $a = $a + 1
This operator can appear either before or after its operand:
$a++; # also equivalent to $a += 1 and $a = $a + 1
The ++ operator can also be part of a more complicated sequence of operations.
(A code fragment consisting of a sequence of operations and their values is
known as an expression.) Consider the following statements:
$b = ++$a;
$b = $a++;
In the first statement, the ++ operator appears before its operand. This tells
Perl to add 1 to $a before assigning its value to $b:
$a = 7;
$b = ++$a; # $a and $b are both 8
If the ++ operator appears after the operand, Perl adds 1 to $a after assigning
its value to $b:
$a = 7;
$b = $a++; # $a is now 8, and $b is now 7
Similarly, the —, or autodecrement, operator subtracts 1 from the value of a
scalar variable either before or after assigning the value:
$a = 7;
$b = —$a; # $a and $b are both 6
$a = 7;
$b = $a—; # $a is now 6, and $b is now 7
The ++ and — operators provide a great deal of flexibility, and are often used
in loops and other control structures.
Do not use the ++ and — operators on the same variable more than once in the
same expression:
$b = ++$a + $a++;
The value assigned to $b depends on which of the operands of the + operator is
evaluated first. On some systems, the first operand (++$a) is evaluated first.
On others, the second operand ($a++) is evaluated first.
You can ensure that you get the result you want by using multiple statements and
the appropriate assignment operator:
$b = ++$a;
$b += $a++;
Concatenating and Repeating Strings
Perl provides three operators that operate on strings: The . operator, which
joins two strings together; the x operator, which repeats a string; and the .=
operator, which joins and then assigns.
The . operator joins the second operand to the first operand:
$a = "be" . "witched"; # $a is now "bewitched"
This join operation is also known as string concatenation.
The x operator (the letter x) makes n copies of a string, where n is the value
of the right operand:
$a = "t" x 5; # $a is now "ttttt"
The .= operator combines the operations of string concatenation and assignment:
$a = "be";
$a .= "witched"; # $a is now "bewitched"
Using Other C Operators
Perl also supports the following operators, found in the C programming language:
The , (comma) operator, and the ? and : (conditional) operator combination.
The , operator ensures that one portion of an expression is evaluated first:
$x += 1, $y = $x;
The , operator breaks this expression into two parts:
$x += 1
$y = $x
The part before the comma is performed first. Thus, 1 is added to $x and then $x
is assigned to $y.
The ? and : combination allows you to test the value of a variable and then
perform one of two operations based on the result of the test. For example, in
the expression $y = $x == 0 ? 15 : 8 the variable $x is compared with zero. If
$x equals zero, $y is assigned 15; if $x is not zero, $y is assigned 8.
Matching Patterns
Perl allows you to examine scalar variables and test for the existence of a
particular pattern in a string. To do this, use the =~ (pattern matching)
operator:
$x =~ /jkl/
The character string enclosed by the / characters is the pattern to be matched,
and the scalar variable on the left of the =~ operator is the variable to be
examined. This example searches for the pattern jkl in the scalar variable $x.
If $x contains jkl, the expression is true; if not, the expression is false. In
the statement $y = $x =~ /jkl/;, $y is assigned a non-zero value if $x contains
jkl, and is assigned zero if $x does not contain jkl.
The !~ operator is the negation of =~:
$y = $x !~ /jkl/;
Here, $y is assigned zero if $x contains jkl, and a non-zero value otherwise.
Using Special Characters in Patterns
You can use several special characters in your patterns. The * character matches
zero or more of the character it follows:
/jk*l/
This matches jl, jkl, jkkl, jkkkl, and so on.
The + character matches one or more of the preceding character:
/jk+l/
This matches jkl, jkkl, jkkkl, and so on.
The ? character matches zero or one copies of the preceding character:
/jk?l/
This matches jl or jkl.
The character . matches any character except the newline character:
/j.l/
This matches any pattern consisting of a j, any character, and an l.
If a set of characters is enclosed in square brackets, any character in the set
is an acceptable match:
/j[kK]l/ # matches jkl or jKl
Consecutive alphanumeric characters in the set can be represented by a dash (-):
/j[k1-3K]l/ # matches jkl, j1l, j2l, j3l or jKl
You can specify that a match must be at the start or end of a line by using ^ or
$:
/^jkl/ # matches jkl at start of line
/jkl$/ # matches jkl at end of line
/^jkl$/ # matches line consisting of exactly jkl
You can specify that a match must be either on a word boundary or inside a word
by including \b or \B in the pattern:
/\bjkl/ # matches jkl, but not ijkl
/\Bjkl/ # matches ijkl, but not jkl
Some sets are so common that special characters exist to represent them:
\d matches any digit, and is equivalent to [0-9].
\w matches any character that can appear in a variable name; it is
equivalent to [A-Za-z_0-9].
\s matches any whitespace (any character not visible on the screen); it is
equivalent to [ \r\t\n\f]. (These backslash characters were explained in
"Using Double- and Single-Quoted Strings" earlier in this chapter.)
To match all but a specified set of characters, specify ^ at the start of your
set:
/j[^kK]l/
This matches any string containing j, any character but k or K, and l.
To use a special character as an ordinary character, precede it with a backslash
(\):
/j\*l/ # this matches j*l
This matches j*l.
In patterns, the * and + special characters match as many characters in a string
as possible. For example, consider the following:
$x = "abcde";
$y = $x =~ /a.*/;
The pattern /a.*/ can match a, ab, abc, abcd, or abcde. abcde is matched, since
it is the longest. This becomes meaningful when patterns are used in
substitution.
Substituting and Translating Using Patterns
You can use the =~ operator to substitute one string for another:
$val =~ s/abc/def/; # replace abc with def
$val =~ s/a+/xyz/; # replace a, aa, aaa, etc., with xyz
$val =~ s/a/b/g; # replace all a's with b's
Here, the s prefix indicates that the pattern between the first / and the second
is to be replaced by the string between the second / and the third.
You can also translate characters using the tr prefix:
$val =~ tr/a-z/A-Z/; # translate lower case to upper
Here, any character matched by the first pattern is replaced by the
corresponding character in the second pattern.
The Order of Operations
Consider the following statement:
$a = 21 * 2 + 3 << 1 << 2 ** 2;
The problem: Which operation should be performed first?
The following sections answer questions of this type.
Precedence
In standard grade-school arithmetic, certain operations are always performed
before others. For example, multiplication is always performed before addition:
4 + 5 * 3
Because multiplication is performed before addition, it has higher precedence
than addition.
Table 16.2 defines the precedence of the Perl operators described in these
sections. The items at the top of the table have the highest precedence, and the
items at the bottom have the lowest.
Table 16.2. Operator Precedence in Perl.
++, —Autoincrement and autodecrement
-, ~, !Operators with one operand
**Exponentiation
=~, !~Matching operators
*, /, %, xMultiplication, division, remainder, repetition
+, -, .Addition, subtraction, concatenation
<<, >>Shifting operators
-e, -r, etc.File status operators
<, <=, >, >=, lt, le, gt, geInequality comparison operators
==, !=, <=>, eq, ne, cmpEquality comparison operators
&Bitwise AND
|, ^Bitwise OR and exclusive OR
&&Logical AND
||Logical OR
..List range operator
? and :Conditional operator
=, +=, -=, *=, etc.Assignment operators
,Comma operator
For example, consider the following statement:
$x = 11 * 2 + 6 ** 2 << 2;
The operations in this statement are performed in the following order:
6 ** 2, yielding 36.
11 * 2, yielding 22.
36 + 22, yielding 58.
58 << 2, yielding 116.
Therefore, 116 is assigned to $x.
This operator precedence table contains some operators that are defined in later
sections. The .. (list range) operator is defined in "Using Lists and Array
Variables." The file status operators are described in "Reading from and Writing
to Files."
Associativity
Consider the following statement:
$x = 2 + 3 - 4;
In this case, it doesn't matter whether the addition (2 + 3) or the subtraction
(3 - 4) is performed first, because the result is the same either way. However,
for some operations, the order of evaluation makes a difference:
$x = 2 ** 3 ** 2;
Is $x assigned 64 (8 ** 2) or 512 (2 ** 9)?
To resolve these problems, Perl associates a specified associativity with each
operator. If an operator is right-associative, the rightmost operator is
performed first when two operators have the same precedence:
$x = 2 ** 3 ** 2; # the same as $x = 2 ** 9, or $x = 512
If an operator is left-associative, the leftmost operator is performed first
when two operators have the same precedence:
$x = 29 % 6 * 2; # the same as $x = 5 * 2, or $x = 10
The following operators in Perl are right-associative:
The assignment operators (=, +=, and so on)
The ? and : operator combination
The ** operator (exponentiation)
The operators that have only one operand (!, ~ and -)
All other operators are left-associative.
Forcing Precedence Using Parentheses
Perl allows you to force the order of evaluation of operations in expressions.
To do this, use parentheses:
$x = 4 * (5 + 3);
In this statement, 5 is added to 3 and then multiplied by 4, yielding 32.
You can use as many sets of parentheses as you like:
$x = 4 ** (5 % (8 - 6));
Here, the result is 4:
8 - 6 is performed, leaving 4 ** (5 % 2)
5 % 2 is performed, leaving 4 ** 1
4 ** 1 is 4
Using Lists and Array Variables
So far, the Perl programs you have seen have only used scalar data and scalar
variables. In other words, they have only dealt with one value at a time.
Perl also allows you to manipulate groups of values, known as lists or arrays.
These lists can be assigned to special variables known as array variables, which
can be processed in a variety of ways.
This section describes lists and array variables, and how to use them. It also
describes how to pass command-line arguments to your program using the
special-purpose array @ARGV.
Introducing Lists
A list is a collection of scalar values enclosed in parentheses. The following
is a simple example of a list:
(1, 5.3, "hello", 2)
This list contains four elements, each of which is a scalar value: the numbers 1
and 5.3, the string "hello", and the number 2. As always in Perl, numbers and
character strings are interchangeable: each element of a list can be either a
number or a string.
A list can contain as many elements as you like (or as many as your machine's
memory can store at one time). To indicate a list with no elements, just specify
the parentheses:
() # this list is empty
Scalar Variables and Lists
Lists can also contain scalar variables:
(17, $var, "a string")
Here, the second element of the list is the scalar variable $var. When Perl sees
a scalar variable in a list, it replaces the scalar variable with its current
value.
A list element can also be an expression:
(17, $var1 + $var2, 26 << 2)
Here, the expression $var1 + $var2 is evaluated to become the second element,
and the expression 26 << 2 is evaluated to become the third element.
Scalar variables can also be replaced in strings:
(17, "the answer is $var1")
In this case, the value of $var1 is placed into the string.
Using List Ranges
Suppose that you want to define a list consisting of the numbers 1 through 10,
inclusive. You can do this by typing in each of the numbers in turn:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
However, there is a simpler way to do it: use the list range operator, which is
.. (two consecutive periods). The following is a list created using the list
range operator:
(1..10)
This tells Perl to define a list whose first value is 1, whose second value is
2, and so on up to 10.
The list range operator can be used to define part of a list:
(2, 5..7, 11)
This list consists of five elements: the numbers 2, 5, 6, 7 and 11.
List range operators can also be used with floating-point values:
(2.1..5.3)
This list consists of four elements: 2.1, 3.1, 4.1, and 5.1. Each element of the
list is one greater than the previous element, and the last element of the list
is the largest possible number less than or equal to the number to the right of
the .. operator. (If the value to the left of the .. operator is greater than
the value to the right, an empty list is created.)
Elements that define the range of a list range operator can be expressions, and
these expressions can contain scalar variables:
($a..$b+5)
This list consists of all values between the current value of $a and the current
value of the expression $b+5.
Storing Lists in Array Variables
Perl allows you to store lists in special variables designed for that purpose.
These variables are called array variables.
The following is an example of a list being assigned to an array variable:
@array = (1, 2, 3);
Here, the list (1, 2, 3) is assigned to the array variable @array.
Note that the name of the array variable starts with the character @. This
allows Perl to distinguish array variables from other kinds of variables, such
as scalar variables, which start with the character $. As with scalar variables,
the second character of the variable name must be a letter, and subsequent
characters of the name can be letters, numbers, or underscores.
When an array variable is first created (seen for the first time), it is assumed
to contain the empty list () unless something is assigned to it.
Because Perl uses @ and $ to distinguish array variables from string variables,
the same name can be used in an array variable and in a string variable:
$var = 1;
@var = (11, 27.1, "a string");
Here, the name var is used in both the string variable $var and the array
variable @var. These are two completely separate variables.
Assigning to Array Variables
As you have already seen, lists can be assigned to array variables with the
assignment operator =:
@x = (11, "my string", 27.44);
You can also assign one array variable to another:
@y = @x;
A scalar value can be assigned to an array variable:
@x = 27.1;
@y = $x;
In this case, the scalar value (or value stored in a scalar variable) is
converted into a list containing one element.
Using Array Variables in Lists
As you have already seen, lists can contain scalar variables:
@x = (1, $y, 3);
Here, the value of the scalar variable $y becomes the second element of the list
assigned to @x.
You can also specify that the value of an array variable is to appear in a list:
@x = (2, 3, 4);
@y = (1, @x, 5);
Here, the list (2, 3, 4) is substituted for @x, and the resulting list (1, 2, 3,
4, 5) is assigned to @y.
Assigning to Scalar Variables from Array Variables
Consider the following assignment:
@x = ($a, $b);
Here, the values of the scalar variables $a and $b are used to form a
two-element list that is assigned to the array variable @x.
Perl also allows you to take the current value of an array variable and assign
its components to a group of scalar variables:
($a, $b) = @x;
Here, the first element of the list currently stored in @x is assigned to $a,
and the second element is assigned to $b. Additional elements in @x, if they
exist, are not assigned.
If there are more scalar variables than elements in an array variable, the
excess scalar variables are given the value "" (the null string), which is
equivalent to the numeric value 0:
@x = (1, 2);
($a, $b, $c) = @x; # $a is now 1, $b is now 2, $c is now ""
Retrieving the Length of a List
As you have already seen, when a scalar value is assigned to an array variable,
the value is assumed to be a list containing one element. For example, the
following statements are equivalent:
@x = $y;
@x = ($y);
However, the converse is not true. In the statement $y = @x;, the value assigned
to $y is the number of elements in the list currently stored in @x:
@x = ("string 1", "string 2", "string 3");
$y = @x; # $y is now 3
To assign the value of the first element of a list to a scalar variable, enclose
the scalar variable in a list:
@x = ("string 1", "string 2", "string 3");
($y) = @x; # $y is now "string 1"
Using Array Slices
Perl allows you to specify what part of an array to use in an expression. The
following example shows you how to do this:
@x = (1, 2, 3);
@y = @x[0,1];
Here, the list (1, 2, 3) is first assigned to the array variable @x. Then, the
array slice [0,1] is assigned to @y: in other words, the first two elements of
@x are assigned to @y. (Note that the first element of the array is specified by
0, not 1.)
You can assign to an array slice as well:
@x[0,1] = (11.5, "hello");
This statement assigns the value 11.5 to the first element of the array variable
@x, and assigns the string "hello" to the second.
Array variables automatically grow when necessary, with null strings assigned to
fill any gaps:
@x = (10, 20, 30);
@x[4,5] = (75, 85);
Here, the second assignment increases the size of the array variable @x from
three elements to six, and assigns 75 to the fifth element and 85 to the sixth.
The fourth element is set to be the null string.
Using Array Slices with Scalar Variables
An array slice can consist of a single element. In this case, the array slice is
treated as if it is a scalar variable:
@x = (10, 20, 30);
$y = $x[1]; # $y now has the value 20
Note that the array slice is now preceded by the character $, not the character
@. This tells Perl that the array slice is to be treated as a scalar variable.
Recall that array variables and scalar variables can have the same name:
$x = "Smith";
@x = (47, "hello");
Here, the scalar variable $x and the array variable @x are both defined, and are
completely independent of one another. This can cause problems if you want to
include a scalar variable inside a string:
$y = "Refer to $x[1] for more information.";
In this case, Perl assumes that you want to substitute the value of the array
slice $x[1] into the string. This produces the following:
$y = "Refer to hello for more information.";
To specify the scalar variable and not the array slice, enclose the variable
name in braces:
$y = "Refer to ${x}[1] for more information.";
This tells Perl to replace $x, not $x[1], and produces the following:
$y = "Refer to Smith[1] for more information.";
Using the Array Slice Notation as a Shorthand
So far, we have been using the array slice notation @x[0,1] to refer to a
portion of an array variable. As it happens, in Perl the above is exactly
equivalent to a list of single-element array slices:
@y = @x[0,1];
@y = ($x[0], $x[1]); # these two statements are identical
This allows you to use the array slice notation whenever you want to refer to
more than one element in an array:
@y = @x[4,1,5];
In this statement, the array variable @y is assigned the values of the fifth,
second, and sixth elements of the array variable @x.
@y[0,1,2] = @x[1,1,1];
Here, the second element of @x is copied to the first three elements of @y.
In Perl, assignments in which the operands overlap are handled without
difficulty. Consider this example:
@x[4,3] = @x[3,4];
Perl performs this assignment by creating a temporary array variable, copying
@x[3,4] to it, and then copying it to @x[4,3]. Thus, this statement swaps the
values in the fourth and fifth elements of @x.
Other Array Operations
Perl provides a number of built-in functions that work on lists and array
variables. For example, you can sort array elements in alphabetic order, reverse
the elements of an array, remove the last character from all elements of an
array, and merge the elements of an array into a single string.
Sorting a List or Array Variable
The built-in function sort() sorts the elements of an array in alphabetic order
and returns the sorted list:
@x = ("this", "is", "a", "test");
@x = sort (@x); # @x is now ("a", "is", "test", "this")
Note that the sort is in alphabetic, not numeric, order:
@x = (70, 100, 8);
@x = sort (@x); # @x is now ("100", "70", "8")
The number 100 appears first because the string "100" is alphabetically ahead of
"70" (since "1" appears before "7").
Reversing a List or Array Variable
The function reverse() reverses the order of the elements in a list or array
variable and returns the reversed list:
@x = ("backwards", "is", "array", "this");
@x = reverse(@x); # @x is now ("this", "array", "is", "backwards")
You can sort and reverse the same list:
@x = reverse(sort(@x));
This produces a sort in reverse alphabetical order.
Using chop() on Array Variables
The chop() function can be used on array variables as well as scalar variables:
$a[0] = <STDIN>;
$a[1] = <STDIN>;
$a[2] = <STDIN>;
chop(@a);
Here, three input lines are read into the array variable @a—one in each of the
first three elements. chop() then removes the last character (in this case, the
terminating newline character) from all three elements.
Creating a Single String from a List
To create a single string from a list or array variable, use the function
join():
$x = join(" ", "this", "is", "a", "sentence");
The first element of the list supplied to join() contains the characters that
are to be used to glue the parts of the created string together. In this
example, $x becomes "this is a sentence".
join() can specify other join strings besides " ":
@x = ("words","separated","by");
$y = join("::",@x,"colons");
Here, $y becomes "words::separated::by::colons".
To undo the effects of join(), call the function split():
$y = "words::separated::by::colons";
@x = split(/::/, $y);
The first element of the list supplied to split() is a pattern to be matched.
When the pattern is matched, a new array element is started and the pattern is
thrown away. In this case, the pattern to be matched is ::, which means that @x
becomes ("words", "separated", "by", "colons").
Note that the syntax for the pattern is the same as that used in the =~
operator; see "Matching Patterns" for more information on possible patterns to
match.
Example: Sorting Words in a String
The example in LIST 16_2 on the CD-ROM uses split(), join(), and sort() to sort
the words in a string.
Using Command Line Arguments
The special array variable @ARGV is automatically defined to contain the strings
entered on the command line when a Perl program is invoked. For example, if the
program
#!/usr/bin/perl
print("The first argument is $ARGV[0]\n");
is called printfirstarg, entering the command
printfirstarg 1 2 3
produces the following output:
The first argument is 1
You can use join() to turn @ARGV into a single string, if you like:
#!/usr/bin/perl
$commandline = join(" ", @ARGV);
print("The command line arguments: $commandline\n");
If this program is called printallargs, entering
printallargs 1 2 3
produces
The command line arguments: 1 2 3
Note that $ARGV[0], the first element of the @ARGV array variable, does not
contain the name of the program. For example, in the invocation
printallargs 1 2 3
$ARGV[0] is "1", not "printallargs". This is a difference between Perl and C: in
C, argv[0] is "printallargs" and argv[1] is "1".
Standard Input and Array Variables
Since an array variable can contain as many elements as you like, you can assign
an entire input file to a single array variable:
@infile = <STDIN>;
This works as long as you have enough memory to store the entire file.
Controlling Program Flow
Like all programming languages, Perl allows you to include statements that are
only executed when specified conditions are true; these statements are called
conditional statements.
The following is a simple example of a conditional statement:
if ($x == 14) {
print("\$x is 14\n");
}
Here, the line if ($x == 14) { tells Perl that the following statements—those
between the { and }—are only to be executed if $x is equal to 14.
Perl provides a full range of conditional statements; these statements are
described in the following sections.
Conditional Execution The if Statement
The if conditional statement has the following structure:
if (expr) {
...
}
When Perl sees the if, it evaluates the expression expr to be either true or
false. If the value of the expression is the integer 0, the null string "", or
the string "0", the value of the expression is false; otherwise, the value of
the expression is true.
CAUTION: The only string values that evaluate to false are "" and "0". Strings
such as "00" and "0.0" return true, not false.
Two-Way Branching Using if and else
The else statement can be combined with the if statement to allow for a choice
between two alternatives:
if ($x == 14) {
print("\$x is 14\n");
} else {
print("\$x is not 14\n");
}
Here, the expression following the if is evaluated. If it is true, the
statements between the if and the else are executed. Otherwise, the statements
between the else and the final } are executed. In either case, execution then
proceeds to the statement after the final }.
Note that the else statement cannot appear by itself: it must follow an if
statement.
Multi-Way Branching Using elsif
The elsif statement allows you to write a program that chooses between more than
two alternatives:
if ($x == 14) {
print("\$x is 14\n");
} elsif ($x == 15) {
print("\$x is 15\n");
} elsif ($x == 16) {
print("\$x is 16\n");
} else {
print("\$x is not 14, 15 or 16\n");
}
Here, the expression $x == 14 is evaluated. If it evaluates to true (if $x is
equal to 14), the first print() statement is executed. Otherwise, the expression
$x == 15 is evaluated. If $x == 15 is true, the second print() is executed;
otherwise, the expression $x == 16 is evaluated, and so on.
You can have as many elsif statements as you like; however, the first elsif
statement of the group must be preceded by an if statement.
The else statement can be omitted:
if ($x == 14) {
print("\$x is 14\n");
} elsif ($x == 15) {
print("\$x is 15\n");
} elsif ($x == 16) {
print("\$x is 16\n");
} # do nothing if $x is not 14, 15 or 16
If the else statement is included, it must follow the last elsif.
Conditional Branching Using unless
The unless statement is the opposite of the if statement:
unless ($x == 14) {
print("\$x is not 14\n");
}
Here, the statements between the braces are executed unless the value of the
expression evaluates to true.
You can use elsif and else with unless, if you like; however, an if-elsif-else
structure is usually easier to follow than an unless-elsif-else one.
Repeating Statements Using while and until
In the examples above, each statement between braces is executed once, at most.
To indicate that a group of statements between braces is to be executed until a
certain condition is met, use the while statement:
#!/usr/bin/perl
$x = 1;
while ($x <= 5) {
print("\$x is now $x\n");
++$x;
}
Here, the scalar variable $x is first assigned the value 1. The statements
between the braces are then executed until the expression $x <= 5 is false.
When you run the program shown above, you get the following output:
$x is now 1
$x is now 2
$x is now 3
$x is now 4
$x is now 5
As you can see, the statements between the braces have been executed five times.
The until statement is the opposite of while:
#!/usr/bin/perl
$x = 1;
until ($x <= 5) {
print("\$x is now $x\n");
++$x;
}
Here, the statements between the braces are executed until the expression $x <=
5 is true. In this case, the expression is true the first time it is evaluated,
which means that the print() statement is never executed. To fix this, reverse
the direction of the arithmetic comparison:
#!/usr/bin/perl
$x = 1;
until ($x > 5) {
print("\$x is now $x\n");
++$x;
}
This now produces the same output as the program containing the while statement
above.
CAUTION: If you use while, until, or any other statement that repeats, you must
make sure that the statement does not repeat forever:
$x = 1;
while ($x == 1) {
print("\$x is still $x\n");
}
Here, $x is always 1, $x == 1 is always true, and the print() statement is
repeated an infinite number of times.
Perl does not check for infinite loops such as this one above. It is your
responsibility to make sure that infinite loops don't happen!
Using Single-Line Conditional Statements
If only one statement is to be executed when a particular condition is true, you
can write your conditional statement using a single-line conditional statement.
For example, instead of writing
if ($x == 14) {
print("\$x is 14\n");
}
you can use the following single-line conditional statement:
print("\$x is 14\n") if ($x == 14);
In both cases, the print() statement is executed if $x is equal to 14.
You can also use unless, while, or until in a single-line conditional statement:
print("\$x is not 14\n") unless ($x == 14);
print("\$x is less than 14\n") while ($x++ < 14);
print("\$x is less than 14\n") until ($x++ > 14);
Note how useful the autoincrement operator ++ is in the last two statements: it
allows you to compare $x and add one to it all at once. This ensures that the
single-line conditional statement does not execute forever.
Looping with the for Statement
Most loops—segments of code that are executed more than once—use a counter to
control and eventually terminate the execution of the loop. Here is an example
similar to the ones you've seen so far:
$count = 1; # initialize the counter
while ($count <= 10) { # terminate after ten repetitions
print("the counter is now $count\n");
$count += 1; # increment the counter
}
As you can see, the looping process consists of three components:
The initialization of the counter variable
A test to determine whether to terminate the loop
The updating of the counter variable after the execution of the statements
in the loop
Because a loop so often contains these three components, Perl provides a quick
way to do them all at once by using the for statement. The following example
uses the for statement and behaves the same as the example you just saw:
for ($count = 1; $count <= 10; $count += 1) {
print("the counter is now $count\n");
}
Here the three components of the loop all appear in the same line, separated by
semicolons. Because the components are all together, it is easier to remember to
supply all of them, which makes it more difficult to write code that goes into
an infinite loop.
Looping Through a List The foreach Statement
So far, all of the examples of loops that you've seen use a scalar variable as
the counter. You can also use a list as a counter by using the foreach
statement:
#!/usr/bin/perl
@list = ("This", "is", "a", "list", "of", "words");
print("Here are the words in the list: \n");
foreach $temp (@list) {
print("$temp ");
}
print("\n");
Here, the loop defined by the foreach statement executes once for each element
in the list @list. The resulting output is
Here are the words in the list:
This is a list of words
The current element of the list being used as the counter is stored in a special
scalar variable, which in this case is $temp. This variable is special because
it is only defined for the statements inside the foreach loop:
#!/usr/bin/perl
$temp = 1;
@list = ("This", "is", "a", "list", "of", "words");
print("Here are the words in the list: \n");
foreach $temp (@list) {
print("$temp ");
}
print("\n");
print("The value of temp is now $temp\n");
The output from this program is the following:
Here are the words in the list:
This is a list of words
The value of temp is now 1
The original value of $temp is restored after the foreach statement is finished.
Variables that only exist inside a certain structure, such as $temp in the
foreach statement in the preceding example, are called local variables.
Variables that are defined throughout a Perl program are known as global
variables. Most variables you use in Perl are global variables. To see other
examples of local variables, refer to "Using Subroutines."
CAUTION: Changing the value of the local variable inside a foreach statement
also changes the value of the corresponding element of the list:
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
if ($temp == 2) {
$temp = 20;
}
}
In this loop, when $temp is equal to 2, $temp is reset to 20. Therefore, the
contents of the array variable @list become (1, 20, 3, 4, 5).
Exiting a Loop with the last Statement
Normally, you exit a loop by testing the condition at the top of the loop and
then jumping to the statement after it. However, you can also exit a loop in the
middle. To do this, use the last statement.
File LIST 16_5 on the CD-ROM totals a set of receipts entered one at a time;
execution is terminated when a null line is entered. If a value entered is less
than zero, the program detects this and exits the loop.
Using next to Start the Next Iteration of a Loop
In Perl, the last statement terminates the execution of a loop. To terminate a
particular pass through a loop (also known as an iteration of the loop), use the
next statement.
File LIST 16_4 on the CD-ROM sums up the numbers from 1 to a user-specified
upper limit, and also produces a separate sum of the numbers divisible by two.
Be careful when you use next in a while or until loop. The following example
goes into an infinite loop:
$count = 0;
while ($count <= 10) {
if ($count == 5) {
next;
}
$count++;
}
When $count is 5, the program tells Perl to start the next iteration of the
loop. However, the value of $count is not changed, which means that the
expression $count == 5 is still true.
To get rid of this problem, you need to increment $count before using next, as
in:
$count = 0;
while ($count <= 10) {
if ($count == 5) {
$count++;
next;
}
$count++;
}
This, by the way, is why many programming purists dislike statements such as
next and last: it's too easy to lose track of where you are and what needs to be
updated.
Perl automatically assumes that variables are initialized to be the null string,
which evaluates to 0 in arithmetic expressions. This means that in code
fragments such as
$count = 0;
while ($count <= 10) {
...
$count++;}
you don't really need the $count = 0; statement. However, it is a good idea to
explicitly initialize everything, even when you don't need to. This makes it
easier to spot misprints:
$count = $tot = 0;
while ($count <= 10) {
$total += $count; # misprint: you meant to type "$tot"
$count += 1;
}
print ("the total is $tot\n");
If you've gotten into the habit of initializing everything, it's easy to spot
that $total is a misprint. If you use variables without initializing them, you
first have to determine whether $total is really a different variable than $tot.
This may be difficult if your program is a large and complicated one.
Using Labelled Blocks for Multi-Level Jumps
In Perl, loops can be inside other loops: such loops are said to be nested. To
get out of an outer loop from within an inner loop, label the outer loop and
specify its label when using last or next:
$total = 0;
$firstcounter = 1;
DONE: while ($firstcounter <= 10) {
$secondcounter = 1;
while ($secondcounter <= 10) {
$total += 1;
if ($firstcounter == 4 && $secondcounter == 7) {
last DONE;
}
$secondcounter += 1;
}
$firstcounter += 1;
}
The statement
last DONE;
tells Perl to jump out of the loop labelled DONE and continue execution with the
first statement after the outer loop. (By the way, this code fragment is just a
rather complicated way of assigning 37 to $total.)
Loop labels must start with a letter, and can consist of as many letters,
digits, and underscores as you like. The only restriction is that you can't use
a label name that corresponds to a word that has a special meaning in Perl:
if: while ($x == 0) { # this is an error in perl
...
}
When Perl sees the if, it doesn't know whether you mean the label if or the
start of an if statement.
Words such as if that have special meanings in Perl are known as reserved words
or keywords.
Terminating Execution Using die
As you have seen, the last statement terminates a loop. To terminate program
execution entirely, use the die() function.
To illustrate the use of die(), see File LIST 16_6 on the CD-ROM, a simple
program that divides two numbers supplied on a single line. die() writes its
argument to the standard error file, STDERR, and then exits immediately. In this
example, die() is called when there are not exactly two numbers in the input
line, or if the second number is zero.
If you like, you can tell die() to print the name of the Perl program and the
line number being executed when the program was terminated. To do this, leave
the closing newline character off the message:
die("This prints the filename and line number");
If the closing newline character is included, the filename and line number are
not included:
die("This does not print the filename and line number\n");
Reading from and Writing to Files
So far, all of the examples have read from the standard input file, STDIN, and
have written to the standard output file, STDOUT, and the standard error file,
STDERR. You can also read from and write to as many other files as you like.
To access a file on your UNIX file system from within your Perl program, you
must perform the following steps:
First, your program must open the file. This tells the system that your Perl
program wants to access the file.
Then, the program can either read from or write to the file, depending on
how you have opened the file.
Finally, the program can close the file. This tells the system that your
program no longer needs access to the file.
The following sections describe these operations, tell you how you can read from
files specified in the command line, and describe the built-in file test
operations.
Opening a File
To open a file, call the built-in function open():
open(MYFILE, "/u/jqpublic/myfile");
The second argument is the name of the file you want to open. You can supply
either the full UNIX pathname, as in /u/jqpublic/myfile, or just the filename,
as in myfile. If only the filename is supplied, the file is assumed to be in the
current working directory.
The first argument is an example of a file variable. Once the file has been
opened, your Perl program accesses the file by referring to this variable. Your
file variable name must start with a letter, and can then contain as many
letters and digits as you like. (You must ensure, however, that your file
variable name is not the same as a reserved word, such as if. See the note in
"Using Labelled Blocks for Multi-Level Jumps" for more information on reserved
words.)
By default, Perl assumes that you want to read any file that you open. To open a
file for writing, put a > (greater than) character in front of your filename:
open(MYFILE, ">/u/jqpublic/myfile");
When you open a file for writing, any existing contents are destroyed. You
cannot read from and write to the same file at the same time.
To append to an existing file, put two > characters in front of the filename:
open(MYFILE, ">>/u/jqpublic/myfile");
You still cannot read from a file you are appending to, but the existing
contents are not destroyed.
Checking Whether the Open Succeeded
The open() function returns one of two values:
open() returns true (a non-zero value) if the open succeeds
open() returns false (zero) if an error occurs (that is, the file does not
exist or you don't have permission to access the file)
You can use the return value from open() to test whether the file is actually
available, and call die() if it is not:
unless (open(MYFILE, "/u/jqpublic/myfile")) {
die("unable to open /u/jqpublic/myfile for reading\n");
}
This ensures that your program does not try to read from a nonexistent file.
You can also use the || (logical or) operator in place of unless:
open(MYFILE, "/u/jqpublic/myfile") ||
die("unable to open /u/jqpublic/myfile for reading\n");
This works because the right side of the || operator is only executed if the
left side is false. See "Performing Comparisons" for more information on the ||
operator.
Reading from a File
To read from a file, enclose the name of the file in angle brackets:
$line = <MYFILE>;
This statement reads a line of input from the file specified by the file
variable MYFILE, and stores the line of input in the scalar variable $line. As
you can see, you read from files in exactly the same way you read from the
standard input file, STDIN.
Writing to a File
To write to a file, specify the file variable when you call the function
print():
print MYFILE ("This is a line of text to write \n",
"This is another line to write\n");
The file variable must appear before the first line of text to be written to the
file.
This method works both when you are writing a new file and when you are
appending to an existing one.
Closing a File
When you are finished reading from or writing to a file, you can tell the system
that you are finished by calling close():
close(MYFILE);
Note that close() is not required: Perl automatically closes the file when the
program terminates or when you open another file using a previously defined file
variable.
Determining the Status of a File
As you have seen, when you open a file for writing, the existing contents of the
file are destroyed. If you only want to open the file for writing if the file
does not already exist, you can first test to see if a file exists. To do this,
use the -e operator:
if (-e "/u/jqpublic/filename") {
die ("file /u/jqpublic/filename already exists");
}
open (MYFILE, "/u/jqpublic/filename");
The -e operator assumes that its operand—a scalar value—is the name of a file.
It checks to see if a file with that name already exists. If the file exists,
the -e operator returns true; otherwise, it returns false.
Similar tests exist to test other file conditions. The most commonly used file
status operators are listed in Table 16.3.
Table 16.3. File Status Operators.
-dIs this file really a directory?
-eDoes this file exist?
-fIs this actually a file?
-lIs this file really a symbolic link?
-oIs this file owned by the person running the program?
-rIs this file readable by the person running the program?
-sIs this a non-empty file?
-wIs this file writeable by the person running the program?
-xIs this file executable by the person running the program?
-zIs this file empty?
-BIs this a binary file?
-TIs this a text file?
Reading from a Sequence of Files
Many UNIX commands have the form
command file1 file2 file3 ...
These commands operate on all of the files specified on the command line,
starting with file1 and continuing on from there.
You can simulate this behavior in Perl. To do this, use the <> operator.
File LIST 16_7 on the CD-ROM counts all the times the word "the" appears in a
set of files.
Suppose that this example is stored in a file named thecount. If the command
thecount myfile1 myfile2 myfile3 is entered from the command line, the program
starts by reading a line of input from the file myfile1 into the scalar variable
$inputline. This input line is then split into words, and each word is tested to
see if it is "the." Once this line is processed, the program reads another line
from myfile1.
When myfile1 is exhausted, the program then begins reading lines from myfile2,
and then from myfile3. When myfile3 is exhausted, the program prints the total
number of occurrences of "the" in the three files.
Using Subroutines
Some programs perform the same task repeatedly. If you are writing such a
program, you may soon get tired of writing the same lines of code over and over
again. Perl provides a way around this problem: frequently used segments of code
can be stored in separate sections, known as subroutines.
The following sections describe how subroutines work, how to pass values to
subroutines and receive values from them, and how to define variables that only
exist inside subroutines.
Defining a Subroutine
A common Perl task is to read a line of input from a file and break it into
words. Here is an example of a subroutine that performs this task. Note that it
uses the <> operator described in "Reading from a Sequence of Files."
sub getwords {
$inputline = <>;
@words = split(/\s+/, $inputline);
}
All subroutines follow this simple format: The reserved word sub, followed by
the name of the subroutine (in this case, getwords), a { (open brace) character,
one or more Perl statements (also known as the body of the subroutine), and a
closing } (close brace) character.
The subroutine name must start with a letter, and can then consist of any number
of letters, digits, and underscores. (As always, you must ensure that your
variable name is not a reserved word. See the note in "Using Labelled Blocks for
Multi-Level Jumps" for more information on reserved words.)
A subroutine can appear anywhere in a Perl program—even right in the middle, if
you like. However, programs are usually easier to understand if the subroutines
are all placed at the end.
Using a Subroutine
Once you have written your subroutine, you can use it by specifying its name.
Here is a simple example that uses the subroutine getwords to count the number
of occurrences of the word "the":
#!/usr/bin/perl
$thecount = 0;
&getwords;
while ($words[0] ne "") { # stop when line is empty
for ($index = 0; $words[$index] ne ""; $index += 1) {
$thecount += 1 if $words[$index] eq "the";
}
&getwords;
}
print ("Total number of occurrences of the: $thecount\n");
The statement &getwords; tells Perl to call the subroutine getwords. When Perl
calls the subroutine getwords, it executes the statements contained in the
subroutine, namely
$inputline = <>;
@words = split(/\s+/, $inputline);
Once these statements have been executed, Perl then executes the statement
immediately following the &getwords statement.
Returning a Value from a Subroutine
The getwords subroutine defined above is useful, but it suffers from one serious
limitation: it assumes that the words from the input line are always going to be
stored in the array variable @words. This may lead to problems:
@words = ("These", "are", "some", "words");
&getwords;
Here, calling getwords destroys the existing contents of @words.
To solve this problem, consider the subroutine getwords you saw earlier:
sub getwords {
$inputline = <>;
@words = split(/\s+/, $inputline);
}
In Perl subroutines, the last value seen by the subroutine becomes the
subroutine's return value. In this example, the last value seen is the list of
words assigned to @words. In the call to getwords, this value can be assigned to
an array variable:
@words2 = &getwords;
Note that this hasn't yet solved the problem, since @words is still overwritten
by the getwords subroutine. However, now you don't need to use @words in
getwords, because you are assigning the list of words by using the return value.
You can now change getwords to use a different array variable:
sub getwords {
$inputline = <>;
@subwords = split(/s+/, $inputline);
}
Now, the statements
@words = ("These", "are", "some", "words");
@words2 = &getwords;
work properly: @words is not destroyed when getwords is called. (For a better
solution to this problem, see the following section, "Using Local Variables.")
Since the return value of a subroutine is the last value seen, the return value
may not always be what you expect.
Consider the following simple program that adds numbers supplied on an input
line:
#!/usr/bin/perl
$total = &get_total;
print("The total is $total\n");
sub get_total {
$value = 0;
$inputline = <STDIN>;
@subwords = split(/\s+/, $inputline);
$index = 0;
while ($subwords[$index] ne "") {
$value += $subwords[$index++];
}
}
At first glance, you might think that the return value of the subroutine
get_total is the value stored in $value. However, this is not the last value
seen in the subroutine!
Note that the loop exits when $subwords[index] is the null string. Since no
statements are processed after the loop exits, the last value seen in the
subroutine is, in fact, the null string. Thus, the null string is the return
value of get_total, and is assigned to $total.
To get around this problem, always have the last statement of the subroutine
refer to the value you want to use as the return value:
sub get_total {
$value = 0;
$inputline = <STDIN>;
@subwords = split(/\s+/, $inputline);
$index = 0;
while ($subwords[$index] ne "") {
$value += $subwords[$index++];
}
$value; # $value is now the return value
}
Now, get_total actually returns what you want it to.
Using Local Variables
As you saw in "Returning a Value from a Subroutine," defining variables that
appear only in a subroutine ensures that the subroutine doesn't accidentally
overwrite anything:
sub getwords {
$inputline = <>;
@subwords = split(/s+/, $inputline);
}
Note, however, that the variables $inputline and @subwords could conceivably be
added to your program at a later time. Then, a call to getwords would once again
accidentally destroy values that your program needs to keep.
You can ensure that the variables used in a subroutine are known only inside
that subroutine by defining them as local variables. Here is the subroutine
getwords with $inputline and @subwords defined as local variables:
sub getwords {
local($inputline, @subwords);
$inputline = <>;
@subwords = split(/s+/, $inputline);
}
The local() statement tells Perl that versions of the variables $inputline and
@subwords are to be defined for use inside the subroutine. Once a variable has
been defined with local(), it cannot accidentally destroy values in your
program:
@subwords = ("Some", "more", "words");
@words = getwords;
Here, @subwords is not destroyed, because the @subwords used in getwords is
known only inside the subroutine.
Passing Values to a Subroutine
You can make your subroutines more flexible by allowing them to accept values.
As an example, here is the getwords subroutine modified to split the input line
using a pattern that is passed to it:
sub getwords {
local($pattern) = @_;
local($inputline, @subwords);
$inputline = <>;
@subwords = split($pattern, $inputline);
}
The array variable @_ is a special system variable that contains a copy of the
values passed to the subroutine. The statement local($pattern) = @_; creates a
local scalar variable named $pattern and assigns the first value of the array,
@_, to it.
Now, to call getwords you must supply the pattern you want it to use when
splitting words. To split on white space, as before, call getwords as follows:
@words = getwords(/\s+/);
If your input line consists of words separated by colons, you can split it using
getwords by calling it as follows:
@words = getwords(/:/);
If you like, you can even break your line into single characters:
@words = getwords(//);
For more information on patterns you can use, see "Matching Patterns."
The array variable @_ behaves like any other array variable. In particular, its
components can be used as scalar values:
$x = $_[0];
Here, the first element of @_—the first value passed to the subroutine—is
assigned to $x.
Usually, assigning @_ to local variables is the best approach, because your
subroutine becomes easier to understand.
Calling Subroutines from Other Subroutines
If you like, you can have a subroutine call another subroutine you have written.
For example, here is a subroutine that counts the number of words in an input
line:
sub countline {
local(@words, $count);
$count = 0;
@words = getwords(/\s+/);
foreach $word (@words) {
$count += 1;
}
$count; # make sure the count is the return value
}
The subroutine countline first calls the subroutine getwords to split the input
line into words. Then it counts the number of words in the array returned by
getwords and returns that value.
Once you have written countline, it is easy to write a program called wordcount
that counts the number of words in one or more files:
#!/usr/bin/perl
$totalwordcount = 0;
while (($wordcount = &countline) != 0) {
$totalwordcount += $wordcount;
}
print("The total word count is $totalwordcount\n");
# include the subroutines getwords and countline here
This program reads lines until an empty line—a line with zero words—is read in.
(It assumes that the files contain no blank lines. You can get around this
problem by having getwords test whether $inputline is empty before breaking it
into words, returning a special "end of file" value in this case. This value
could then be passed from getwords to countline, and then to the main program.)
Because getwords uses the <> operator to read input, the files whose words are
counted are those listed on the command line:
wordcount file1 file2 file3
This counts the words in the files file1, file2, and file3.
The variable @_ is a local variable whose value is only defined in the
subroutine in which it appears. This allows subroutines to pass values to other
subroutines: each subroutine has its own copy of @_ and none of the copies can
destroy each other's values.
Associative Arrays
A common programming task is to keep counts of several different things at once.
You can, of course, use scalar variables or array variables to solve this
problem, but this requires a rather messy if-elsif structure:
if ($fruit eq "apple") {
$apple += 1;
} elsif ($letter eq "banana") {
$banana += 1;
} elsif ($letter eq "cherry") {
$cherry += 1;
...
This takes up a lot of space, and is rather boring to write.
Fortunately, Perl provides an easier way to solve problems like
these—associative arrays. The following sections describe associative arrays and
how to manipulate them.
Defining Associative Arrays
In ordinary arrays, you access an array element by specifying an integer as the
index:
@fruits = (9, 23, 11);
$count = $fruits[0]; # $count is now 9
In associative arrays, you do not have to use numbers such as 0, 1, and 2 to
access array elements. When you define an associative array, you specify the
scalar values you want to use to access the elements of the array. For example,
here is a definition of a simple associative array:
%fruits = ("apple", 9,
"banana", 23,
"cherry", 11);
$count = $fruits{"apple"}; # $count is now 9
Here, the scalar value "apple" accesses the first element of the array %fruits,
"banana" accesses the second element, and "cherry" accesses the third. You can
use any scalar value you like as an array index, or any scalar value as the
value of the array element:
%myarray = ("first index", 0,
98.6, "second value",
76, "last value");
$value = $myarray{98.6}; # $value is now "second value"
Associative arrays eliminate the need for messy if-elsif structures. To add 1 to
an element of the %fruits array, for example, you just need to do the following:
$fruits{$fruit} += 1;
Better still, if you decide to add other fruits to the list, you do not need to
add more code, because the preceding statement also works on the new elements.
The character % tells Perl that a variable is an associative array. As with
scalar variables and array variables, the remaining characters of the
associative array variable name must consist of a letter followed by one or more
letters, digits, or underscores.
Accessing Associative Arrays
Since an associative array value is a scalar value, it can be used wherever a
scalar value can be used:
$redfruits = $fruits{"apple"} + $fruits{"cherry"};
print("yes, we have no bananas\n") if ($fruits{"banana"} == 0);
Note that Perl uses braces (the { and } characters) to enclose the index of an
associative array element. This makes it possible for Perl to distinguish
between ordinary array elements and associative array elements.
Copying to and from Associative Arrays
Consider the following assignment, which initializes an associative array:
%fruits = ("apple", 9,
"banana", 23,
"cherry", 11);
The value on the right of this assignment is actually just the ordinary list,
("apple", 9, "banana", 23, "cherry", 11), grouped into pairs for readability.
You can assign any list, including the contents of an array variable, to an
associative array:
@numlist[0,1] = ("one", 1);
@numlist[2,3] = ("two", 2);
%numbers = @numlist;
$first = $numbers{"one"}; # $first is now 1
Whenever a list or an array variable is assigned to an associative array, the
odd-numbered elements (the first, third, fifth, and so on) become the array
indexes, and the even-numbered elements (the second, fourth, sixth, etc.) become
the array values.
You can also assign an associative array to an array variable:
%numbers = ("one", 1,
"two", 2);
@numlist = %numbers;
$first = $numlist[3]; # first is now 2
Here, the array indexes and array values both become elements of the array.
Adding and Deleting Array Elements
To add a new element to an associative array, just create a new array index and
assign a value to its element. For example, to create a fourth element for the
%fruits array, use the following:
$fruits{"orange"} = 1;
This statement creates a fourth element with index "orange" and gives it the
value 1.
To delete an element, use the delete() function:
delete($fruits{"orange"});
This deletes the element indexed by "orange" from the array %fruits.
Listing Array Indexes and Values
The keys() function retrieves a list of the array indexes used in an associative
array:
%fruits = ("apple", 9,
"banana", 23,
"cherry", 11);
@fruitindexes = keys(%fruits);
Here, @fruitindexes is assigned the list consisting of the elements "apple",
"banana", and "cherry". Note that this list is in no particular order. To
retrieve the list in alphabetic order, use sort() on the list:
@fruitindexes = sort(keys(%fruits));
This produces the list ("apple", "banana", "cherry").
To retrieve a list of the values stored in an associative array, use the
function values():
%fruits = ("apple", 9,
"banana", 23,
"cherry", 11);
@fruitvalues = values(%fruits);
@fruitvalues now contains a list consisting of the elements 9, 23, and 11
(again, in no particular order).
Looping with an Associative Array
Perl provides a convenient way to use an associative array in a loop:
%fruits = ("apple", 9,
"banana", 23,
"cherry", 11);
while (($fruitname, $fruitvalue) == each(%fruitnames) {
...
}
The each() function returns each element of the array in turn. Each element is
returned as a two-element list (array index, then array value). Again, the
elements are returned in no particular order.
Formatting Your Output
So far, the only output produced has been raw, unformatted output produced using
the print() function. However, you can control how your output appears on the
screen or on the printed page. To do this, define print formats and use the
write() function to print output using these formats.
The following sections describe print formats and how to use them.
Defining a Print Format
Here is an example of a simple print format:
format MYFORMAT =
===================================
Here is the text I want to display.
===================================
.
Here, MYFORMAT is the name of the print format. This name must start with a
letter, and can consist of any sequence of letters, digits, or underscores.
The subsequent lines define what is to appear on the screen. Here, the lines to
be displayed are a line of = characters followed by a line of text and ending
with another line of = characters. A line consisting of a period indicates the
end of the print format definition.
Like subroutines, print formats can appear anywhere in a Perl program.
Displaying a Print Format
To print using a print format, use the write() function. For example, to print
the text in MYFORMAT, use
$~ = "MYFORMAT";
write();
This sends
===================================
Here is the text I want to display.
===================================
to the standard output file.
$~ is a special scalar variable used by Perl; it tells Perl which print format
to use.
Displaying Values in a Print Format
To specify a value to be printed in your print format, add a value field to your
print format. Here is an example of a print format that uses value fields:
format VOWELFORMAT =
==========================================================
Number of vowels found in text file:
a: @<<<<< e: @<<<<< i: @<<<<< o: @<<<<< u: @<<<<<
$letter{"a"}, $letter{"e"}, $letter{"i"}, $letter{"o"}, $letter{"u"}
==========================================================
.
The line
a: @<<<<< e: @<<<<< i: @<<<<< o: @<<<<< u: @<<<<<
contains five value fields. Each value field contains special characters that
provide information on how the value is to be displayed. (These special
characters are described in the following section, "Choosing a Value Field
Format.")
Any line that contains value fields must be followed by a line listing the
scalar values (or variables containing scalar values) to be displayed in these
value fields:
$letter{"a"}, $letter{"e"}, $letter{"i"}, $letter{"o"}, $letter{"u"}
The number of value fields must equal the number of scalar values.
Choosing a Value Field Format
The following value field formats are supported:
@<<<<Left-justified output: width equals the number of characters
supplied
@>>>>Right-justified output: width equals the number of characters
supplied
@||||Centered output: width equals the number of characters supplied
@##.##Fixed-precision numeric: . indicates location of decimal point
@*Multi-line text
In all cases, the @ character is included when the number of characters in the
field are counted. For example, the field @>>>> is five characters wide.
Similarly, the field @###.## is seven characters wide: four before the decimal
point, two after the decimal point, and the decimal point itself.
Writing to Other Output Files
You can also write to other files by using print formats and write(). For
example, to write to the file represented by file variable MYFILE using print
format MYFORMAT, use the following statements:
select(MYFILE);
$~ = "MYFORMAT";
write(MYFILE);
The select() statement indicates which file is to be written to, and the $~ =
"MYFORMAT"; statement selects the print format to use.
Once an output file has been selected using select(), it stays selected until
another select() is seen. This means that if you select an output file other
than the standard output file, as in select(MYFILE);, output from write() won't
go to the standard output file until Perl sees the statement select (MYFILE);.
There are two ways of making sure you don't get tripped up by this:
Always use STDOUT as the default output file. If you change the output file,
change it back when you're done:
select(MYFILE);
$~ = "MYFORMAT";
write(MYFILE);
select(STDOUT);
Always specify the output file with select() before calling write():
select(STDOUT);
$~ = "MYFORMAT";
write(); # STDOUT is assumed
It doesn't really matter which solution you use, as long as you're consistent.
If you are writing a subroutine that writes to a particular output file, you can
save the current selected output file in a temporary variable and restore it
later:
$temp = select(MYFILE); # select the output file
$~ = "MYFORMAT";
write(MYFILE);
select($temp); # restore the original selected output file
This method is also useful if you're in the middle of a large program and you
don't remember which output file is currently selected.
Specifying a Page Header
You can specify a header to print when you start a new page. To do this, define
a print format with the name filename_TOP, where filename is the name of the
file variable corresponding to the file you are writing to. For example, to
define a header for writing to standard output, define a print format named
STDOUT_TOP:
format STDOUT_TOP =
page @<
$%
The system variable $% contains the current page number (starting with 1).
Setting the Page Length
If a page header is defined for a particular output file, write() automatically
paginates the output to that file. When the number of lines printed is greater
than the length of a page, it starts a new page.
By default, the page length is 60 lines. To specify a different page length,
change the value stored in the system variable $=:
$= = 66; # set the page length to 66 lines
This assignment must appear before the first write() statement.
Formatting Long Character Strings
A scalar variable containing a long character string can be printed out using
multiple value fields:
format QUOTATION =
Quotation for the day:
—————————————————————————————
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$quotation
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$quotation
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$quotation
.
Here the value of $quotation is written on three lines. The @ character in the
value fields is replaced by ^: this tells Perl to fill the lines as full as
possible (cutting the string on a space or tab). Any of the value fields defined
in "Choosing a Value Field Format" can be used.
If the quotation is too short to require all of the lines, the last line or
lines are left blank. To define a line that is only used when necessary, put a ~
character in the first column:
~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
To repeat a line as many times as necessary, put two ~ characters at the front:
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
CAUTION: The contents of the scalar variable are destroyed by this write
operation. To preserve the contents, make a copy before calling write().
Using Built-In Functions
The examples you have seen so far use some of the many built-in functions
provided with Perl. The following table provides a more complete list.
For more details on these functions and others, see the online manual page for
Perl.
Table 16.4. Built-In Functions.
alarm($scalar)Deliver SIGALRM in $scalar seconds
atan2($v1, $v2)Return arctangent of $v1/$v2
caller($scalar)Return context of current subroutine
chdir($scalar)Change working directory to $scalar
chmod(@array)Change permissions of file list
chop($scalar)Remove the last character of a string
chown(@array)Change owner and group of file list
close(FILE)Close a file
cos($scalar)Return cosine of $scalar in radians
crypt($v1, $v2)Encrypt a string
defined($scalar)Determine whether $scalar is defined
delete($array{$val})Delete value from associative array
die(@array)Print @array to STDERR and exit
each(%array)Iterate through an associative array
eof(FILE)Check whether FILE is at end of file
eval($scalar)Treat $scalar as a subprogram
exec(@array)Send @array to system as command
exit($scalar)Exit program with status $scalar
exp($scalar)Compute e ** $scalar
fileno(FILE)Return file descriptor for FILE
fork()Create parent and child processes
getc(FILE)Get next character from FILE
getlogin()Get current login from /etc/utmp
gmtime($scalar)Convert time to GMT array
grep($scalar, @array)Find $scalar in @array
hex($scalar)Convert value to hexadecimal
index($v1, $v2, $v3)Find $v2 in $v1 after position $v3
int($scalar)Return integer portion of $scalar
join($scalar, @array)Join array into single string
keys(%array)Retrieve indexes of associative array
length($scalar)Return length of $scalar
link(FILE1, FILE2)Hard link FILE1 to FILE2
localtime($scalar)Convert time to local array
log($scalar)Get natural logarithm of $scalar
mkdir(DIR, $scalar)Create directory
oct($string)Convert value to octal
open(FILE, $scalar)Open file
ord($scalar)Return ASCII value of character
pack($scalar, @array)Pack array into binary structure
pipe(FILE1, FILE2)Open pair of pipes
pop(@array)Pop last value of array
print(FILE, @array)Print string, list or array
push(@array, @array2)Push @array2 onto @array
rand($scalar)Return random value
readlink($scalar)Return value of symbolic link
require($scalar)include library file $scalar
reverse(@list)Reverse order of @list
rindex($v1, $v2)Return last occurrence of $v2 in $v1
scalar($val)Interpret $val as scalar
shift(@array)Shift off first value of @array
sin($scalar)Return sine of $scalar in radians
sleep($scalar)Sleep for $scalar seconds
sort(@array)Sort @array in alphabetical order
splice(@a1, $v1, $v2, @a2)Replace elements in array
split($v1, $v2)Split scalar into array
sprintf($scalar, @array)Create formatted string
sqrt($expr)Return square root of $expr
srand($expr)Set random number seed
stat(FILE)Retrieve file statistics
substr($v1, $v2)Retrieve substring
symlink(FILE1, FILE2)Create symbolic link
system(@array)Execute system command
time()Get current time
undef($scalar)Mark $scalar as undefined
unlink(@array)Unlink a list of files
unpack($v1, $v2)Unpack array from binary structure
unshift(@a1, @a2)Add @a2 to the front of @a1
utime(@array)Change date stamp on files
values(%array)Return values of associative array
vec($v1, $v2, $v3)Treat string as vector array
wait()Wait for child process to terminate
write(FILE)Write formatted output
The $_ Variable
By default, any function that accepts a scalar variable can have its argument
omitted. In this case, Perl uses $_, which is the default scalar variable.
$_ is also the default variable when reading from a file. So, for example,
instead of writing
$var = <STDIN>;
chop($var);
you can write
chop(<STDIN>);
Summary
Perl is a programming language that allows you to write programs that manipulate
files, strings, integers, and arrays quickly and easily. The features of Perl
include:
String and integer interchangeability
Arithmetic, logical, bitwise, and string operators
List, array, and associative array manipulation
Control structures for handling program flow
File input and output capability
Subroutines
Formatted output
A wide range of built-in functions
--
隋末风云起,双龙走天下。
尽数天下英豪,唯我独尊!
※ 来源:·饮水思源站 bbs.sjtu.edu.cn·[FROM: 202.120.5.209]
--
※ 修改:.fzx 于 Aug 1 12:22:49 修改本文.[FROM: heart.hit.edu.cn]
※ 转寄:.紫 丁 香 bbs.hit.edu.cn.[FROM: chen.hit.edu.cn]
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: jmm.bbs@bbs.hit.edu.]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1,024.416毫秒