Perl (whose name comes from the Practical Extraction and Report Language or the Pathologically Eclectic Rubbish Lister, depending on whom you speak to) is a powerful scripting tool that enables you to manage files, create reports, edit text, and perform many other tasks. Perl is included with and installed in Ubuntu by default and could be considered an integral part of the distribution because Ubuntu depends on Perl for many types of software services, logging activities, and software tools.
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 learn what a Perl program looks like, how the language is structured, and where you can find modules of prewritten code to help you write your own Perl scripts. This chapter also includes 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 they can use it 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.
Note
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 commands. These programs generally work on any computer platform because Perl has been ported to nearly every operating system.
Perl programs are used to support a number of Ubuntu services, such as system logging. For example, if you install the logwatch
package, the logwatch.pl
program is run every morning at 6:25 a.m. by the crond
(scheduling) daemon on your system. Other Ubuntu services supported by Perl include the following:
▸ 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
Perl is installed in Ubuntu by default. You can download the code from www.perl.com and build the newest version from source if you want to, although a stable and quality release of Perl is already installed by default in Ubuntu and most (perhaps all) Linux and UNIX-like distributions, including macOS. Updated versions might appear in the Ubuntu repositories, but they’re generally only security fixes that can be installed by updating your system. See Chapter 9, “Managing Software,” to see how to quickly get a list of available updates for Ubuntu.
You can determine what version of Perl you have installed by typing perl -v
at a shell prompt. When you install the latest Ubuntu distribution, you should have the latest version of Perl that was available when the software for your Ubuntu release was gathered and finalized.
Note that there was a development effort underway to replace Perl 5. The new version was to be called Perl 6, but had diverged far enough away from Perl 5 that the decision was made to rename it to Raku. You can learn about Raku at www.raku.org.
This section introduces a very simple Perl program example to get you started using Perl. Although trivial for experienced Perl hackers, this 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 42.1 illustrates a simple Perl program that prints a short message.
#!/usr/bin/perl print 'Look at all the camels! ';
Type in the program shown in the listing and save it to a file called trivial.pl
. Then make the file executable by using the chmod
command (see the following sidebar) and run it at the command prompt.
Command-Line Error
If you get the message bash: trivial.pl: command not found
or bash: ./trivial.pl: Permission denied
, you have either typed the command line incorrectly or forgotten to make trivial.pl
executable with the chmod
command, as shown here:
matthew@seymour:~$ chmod +x trivial.pl
You can force the command to execute in the current directory as follows:
matthew@seymour:~$ ./trivial.pl
Or you can use Perl to run the program, like this:
matthew@seymour:~$ perl trivial.pl
The sample program in the listing is a two-line Perl program. When you type in the program and run it (using Perl or by making the program executable), you are creating a Perl program, a process duplicated by Linux users around the world every day.
Note
#!
is often pronounced shebang, which is short for sharp
(musicians’ name for the #
character), and bang
, which is another name for the exclamation point. This notation is also used in shell scripts. See Chapter 14, “Automating Tasks and Shell Scripting,” 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. Comments are useful for documenting scripts, like this:
#!/usr/bin/perl # a simple example to print a greeting print "hello there ";
The #
character can also be used in a quoted string and used as the delimiter in a regular expression.
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 "; };
A Perl statement is terminated with a semicolon (;
). A Perl statement can extend over several screen lines because Perl is not concerned about white space.
The second line of the simple program in Listing 42.1 prints the text enclosed in quotation marks.
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:
matthew@seymour:~$ perldoc
To get introductory information on Perl, you can use either of these commands:
For an overview or table of contents of Perl’s documentation, use the perldoc
command, like this:
matthew@seymour:~$ 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 https://perldoc.perl.org.
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.
Perl has three variable types: scalars, arrays, and hashes. A different character is used to signify each variable type, so you can have the same name used with each type at the same time.
A scalar variable is indicated with the $
character, as in $penguin
. Scalars can be numbers or strings, and they can change type 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 numeric calculation, but the string "polar bear"
evaluates to 0
.
A Perl array is indicated with the @
character, as in @fish
. An array is a list of values 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
.
A hash is indicated with the %
character, as in %employee
. A hash is a list of name/value pairs. Individual elements in a hash are referenced by name rather than by index (unlike in 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 that contains 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 42.2 displays all the values in your environment, much like typing the bash
shell’s env
command.
#!/usr/bin/perl foreach $key (keys %ENV) { print "$key = $ENV{$key} "; }
Perl has a 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. You see this 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 list of the special variables, see the perlvar
man page.
Perl supports a number of operators for performing 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 they are used to specify and compare values (including strings). A comparison operator is most often used within an if
statement or loop. Perl has comparison operators for numbers and strings. Table 42.1 shows the numeric comparison operators and their meanings.
Table 42.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 |
<=> |
Returns |
!= |
Not equal to |
.. |
Range of |
Table 42.2 shows the string comparison operators and their meanings.
Table 42.2 String 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 |
|
Returns -1 if less than, 0 if equal to, and 1 if greater than |
|
Matched by regular expression |
|
Not matched by regular expression |
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 42.3 shows the compound pattern operators and their meanings.
Table 42.3 Compound Pattern Operators in Perl
Operator |
Meaning |
---|---|
|
Logical |
|
Logical |
|
Logical |
|
Parentheses; used to group compound statements |
Perl supports a variety of math operations. Table 42.4 summarizes these operators.
Table 42.4 Perl Arithmetic Operators
Operator |
Purpose |
---|---|
|
Raises |
|
Calculates the remainder of |
|
Adds |
|
Subtracts |
|
Multiplies |
|
Divides |
|
Negates |
|
Increments |
|
Uses the value of |
|
Decrements |
|
Uses the value of |
|
Assigns the 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 do not fit any of the prior categories. Table 42.5 summarizes these operators.
Table 42.5 Other Perl Operators
Operator |
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, you can also use these operators 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 42.6 shows most of the constants supported by Perl.
Table 42.6 Perl Special String Constants
Expression |
Meaning |
---|---|
|
The means of including a backslash |
|
The alert or bell character |
Backspace |
|
|
Control character (like holding the Ctrl key down and pressing the C character) |
|
Escape |
|
Formfeed |
|
Newline |
|
Carriage return |
|
Tab |
|
Indicates that |