Выбрать главу

#!/usr/bin/perl

# a block of code to print a greeting forever

while (1) {

 print "hello there\n";

};

Perl statements are terminated with a semicolon. A Perl statement can extend over several actual screen lines because Perl is not concerned about whitespace.

The second line of the simple program prints the text enclosed in quotation marks. \n is the escape sequence for a newline character.

TIP

Using the perldoc and man commands is an easy way to get more information about the version of Perl installed on your system. To learn how to use the perldoc command, enter the following:

$ perldoc perldoc

To get introductory information on Perl, you can use either of these commands:

$ perldoc perl

$ man perl

For an overview or table of contents of Perl's documentation, use the perldoc command like this:

$ perldoc perltoc

The documentation is extensive and well organized. Perl includes a number of standard Linux manual pages as brief guides to its capabilities, but perhaps the best way to learn more about Perl is to read its perlfunc document, which lists all the available Perl functions and their usage. You can view this document by using the perldoc script and typing perldoc perlfunc at the command line. You can also find this document online athttp://www.cpan.org/doc/manual/html/pod/perlfunc.html.

Perl Variables and Data Structures

Perl is a weakly typed language, meaning that it does not require that you declare a data type, such as a type of value (data) to be stored in a particular variable. C, for example, makes you declare that a particular variable is an integer, a character, a structure, or what ever the case may be. Perl variables are whatever type they need to be, and can change type when you need them to.

Perl Variable Types

There are three variable types in Perclass="underline" scalars, arrays, and hashes. A different character is used to signify each variable type.

Scalar variables are indicated with the $ character, as in $penguin. Scalars can be numbers or strings, and they can change type from one to the other as needed. If you treat a number like a string, it becomes a string. If you treat a string like a number, it is translated into a number if it makes sense to do so; otherwise, it usually evaluates to 0. For example, the string "76trombones" evaluates as the number 76 if used in a numerical calculation, but the string "polar bear" will evaluate to 0.

Perl arrays are indicated with the @ character, as in @fish. An array is a list of values that are referenced by index number, starting with the first element numbered 0, just as in C and awk. Each element in the array is a scalar value. Because scalar values are indicated with the $ character, a single element in an array is also indicated with a $ character.

For example, $fish[2] refers to the third element in the @fish array. This tends to throw some people off, but is similar to arrays in C in which the first array element is 0.

Hashes are indicated with the % character, as in %employee. A hash is a list of name and value pairs. Individual elements in the hash are referenced by name rather than by index (unlike an array). Again, because the values are scalars, the $ character is used for individual elements.

For example, $employee{name} gives you one value from the hash. Two rather useful functions for dealing with hashes are keys and values. The keys function returns an array containing all the keys of the hash, and values returns an array of the values of the hash. Using this approach, the Perl program in Listing 25.2 displays all the values in your environment, much like typing the bash shell's env command.

LISTING 25.2 Displaying the Contents of the env Hash

#!/usr/bin/perl

foreach $key (keys %ENV) {

 print "$key = $ENV{$key}\n";

}

Special Variables

Perl has a wide variety of special variables, which usually look like punctuation — $_, $!, and $] — and are all extremely useful for shorthand code. $_ is the default variable, $! is the error message returned by the operating system, and $] is the Perl version number.

$_ is perhaps the most useful of these, and you will see that variable used often in this chapter. $_ is the Perl default variable, which is used when no argument is specified. For example, the following two statements are equivalent:

chomp;

chomp($_);

The following loops are equivalent:

for $cow (@cattle) {

 print "$cow says moo.\n";

}

for (@cattle)             {

 print "$_ says moo.\n";

}

For a complete listing of the special variables, you should see the perlvar document that comes with your Perl distribution (such as in the perlvar manual page), or you can go online to http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlvar.html.

Operators

Perl supports a number of operators to perform various operations. There are comparison operators (used to compare values, as the name implies), compound operators (used to combine operations or multiple comparisons), arithmetic operators (to perform math), and special string constants.

Comparison Operators

The comparison operators used by Perl are similar to those used by C, awk, and the csh shells, and are used to specify and compare values (including strings). Most frequently, a comparison operator is used within an if statement or loop. Perl has comparison opera tors for numbers and strings. Table 25.1 shows the numeric comparison operators and their behavior.

TABLE 25.1 Numeric Comparison Operators in Perl

Operator Meaning
== Is equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
.. Range of >= first operand to <= second operand
<=> Returns -1 if less than, 0 if equal, and 1 if greater than