Exceptions to this practice include when the #
character is in a quoted string and when it is being used as the delimiter in a regular expression. Comments are useful to document your scripts, like this:
#!/usr/bin/perl
# a simple example to print a greeting
print 'hello there
';
A block of code, such as what might appear inside a loop or a branch of a conditional statement, is indicated with curly braces ({}
). For example, here is an infinite loop:
#!/usr/bin/perl
# a block of code to print a greeting forever
while (1) {
print 'hello there
';
};
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.
is the escape sequence for a newline character.
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 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
Perl Variable Types
There are three variable types in Perl:
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.
env
Hash#!/usr/bin/perl
foreach $key (keys %ENV) {
print '$key = $ENV{$key}
';
}
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.
';
}
for (@cattle) {
print '$_ says moo.
';