Perl (Practical Extraction and Report Language) is a powerful scripting tool you can use to manage files, create reports, edit text, and perform many other tasks when using Linux. Perl is included with Fedora and could be considered an integral part of the distribution because Fedora depends on Perl for many types of software services, logging activities, and software tools. If you do a full install from this book’s DVD, you will find nearly 150 software tools written in Perl installed under the /usr/bin
and /usr/sbin
directories.
Perl is not the easiest of programming languages to learn because it is designed for flexibility. This chapter shows how to create and use Perl scripts on your system. You will see what a Perl program looks like, how the language is structured, where you can find modules of prewritten code to help you write your own Perl scripts, and several examples of Perl used to perform a few common functions on a computer system.
Although originally designed as a data extraction and report generation language, Perl appeals to many Linux system administrators because it can be used to create utilities that fill a gap between the capabilities of shell scripts and compiled C programs. Another advantage of Perl over other UNIX tools is that it can process and extract data from binary files, whereas sed
and awk
cannot.
In Perl, “there is more than one way to do it.” This is the unofficial motto of Perl, and it comes up so often that it is usually abbreviated as TIMTOWTDI.
You can use Perl at your shell’s command line to execute one-line Perl programs, but most often the programs (usually ending in .pl
) are run as a command. These programs will generally work on any computer platform because Perl has been ported to just about every operating system. Perl is available by default when you install Fedora, and you will find its RPM files on the DVD included with this book.
Perl programs are used to support a number of Fedora services, such as system logging. For example, the logwatch.pl
program is run every morning at 4:20 a.m. by the crond
(scheduling) daemon on your system. Other Fedora services supported by Perl include
Amanda for local and network backups
Fax spooling with the faxrunqd
program
Printing supported by Perl document filtering programs
Hardware sensor monitoring setup using the sensors-detect
Perl program
As of this writing, the current production version of Perl is 5.8.8 (which is Perl version 5 point 8, patch level 8). You can download the code from http://www.perl.com/ and build it yourself from source. You will occasionally find updated versions in RPM format for Fedora, which can be installed by updating your system. Refer to Chapter 7, “Managing Software,” to see how to quickly get a list of available updates for Fedora.
You can determine what version of Perl you installed by typing perl -v
at a shell prompt. If you are installing the latest Fedora distribution, you should have the latest version of Perl.
This section introduces a very simple sample Perl program to get you started using Perl. Although trivial for experienced Perl hackers, a short example is necessary for new users who want to learn more about Perl.
To introduce you to the absolute basics of Perl programming, Listing 30.1 illustrates a simple Perl program that prints a short message.
Type that in and save it to a file called trivial.pl
. Then make the file executable using the chmod
command (see the following sidebar) and run it at the command prompt.
The sample program in the listing is a two-line Perl program. Typing in the program and running it (using Perl or making the program executable) shows how to create your first Perl program, a process duplicated by Linux users around the world every day!
#!
is often pronounced she-bang, which is short for sharp (the musicians’ name for the #
character), and bang, which is another name for the exclamation point. This notation is also used in shell scripts. Refer to Chapter 15, “Automating Tasks,” for more information about writing shell scripts.
The #!
line is technically not part of the Perl code at all. The #
character indicates that the rest of the screen line is a comment. The comment is a message to the shell, telling it where it should go to find the executable to run this program. The interpreter ignores the comment line.
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 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 at http://www.cpan.org/doc/manual/html/pod/perlfunc.html.
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 whatever the case may be. Perl variables are whatever type they need to be, and can change type when you need them to.
There are three variable types in Perl: 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"
will evaluate 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 30.2 displays all the values in your environment, much like typing the bash
shell’s env
command.
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. "; }
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.
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.
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 operators for numbers and strings. Table 30.1 shows the numeric comparison operators and their behavior.
Table 30.2 shows the string comparison operators and their behaviors.
Perl uses compound operators, similar to those used by C or awk
, which can be used to combine other operations (such as comparisons or arithmetic) into more complex forms of logic. Table 30.3 shows the compound pattern operators and their behavior.
Perl supports a wide variety of math operations. Table 30.4 summarizes these operators.
Table 30.4. Perl Arithmetic Operators
Operator | Purpose |
---|---|
| Raises |
| Calculates the remainder of |
| Adds |
| Subtracts |
| Multiplies |
| Divides |
| Negates |
| Increments |
| Uses value of |
| Decrements |
| Uses value of |
| Assigns value of |
You can also use comparison operators (such as ==
or <
) and compound pattern operators (&&
, ||
, and !
) in arithmetic statements. They evaluate to the value 0
for false and 1
for true.
Perl supports a number of operators that don’t fit any of the prior categories. Table 30.5 summarizes these operators.
Table 30.5. Other Perl Operators
Purpose | |
---|---|
| Bitwise |
| Bitwise |
| Bitwise |
| Bitwise exclusive or ( |
| Bitwise shift left (shifts |
| Bitwise shift right (shifts |
| Concatenate |
| Repeats string |
| Comma operator—evaluates |
| Conditional expression—if |
Except for the comma operator and conditional expression, these operators can also be used with the assignment operator, similar to the way addition (+
) can be combined with assignment (=
), giving +=
.
Perl supports string constants that have special meaning or cannot be entered from the keyboard. Table 30.6 shows most of the constants supported by Perl.