-->
Page 487
by Rich Bowen
Page 488
Perl (Practical Extraction and Report Language) was developed in 1986 by Larry Wall. It has grown in popularity, and is now one of the favorite scripting languages for UNIX platforms.
Perl is similar in syntax to C, but also contains much of the style of UNIX shell scripting. And, thrown in with that, it contains the best features of every other programming language that you have ever used.
Perl is an interpreted language rather than a compiled one, which is either an advantage or a disadvantage, depending on how you look at it. Perl has been ported to virtually every operating system out there, and most Perl programs will run without modifications on any system that you move them to. That is certainly an advantage. In addition, for the small, almost trivial, applications used in everyday server maintenance, you might not want to go to all the trouble of writing the code in C and compiling it.
Perl is very forgiving about such things as declaring variables, allocating and deallocating memory, and variable types, so you can get down to the actual business of writing code. In fact, those concepts really do not exist in Perl. This results in programs that are short and to the point, while similar programs in C, for example, might spend half the code declaring variables.
To introduce you to the absolute basics of Perl programming, Listing 24.1 illustrates a trivial Perl program.
Listing 24.1. A trivial Perl program.
#!/usr/bin/perl print "Red Hat Unleashed, 2nd edition\n";
That's the whole thing. Type that in, save it to a file called trivial.pl, chmod +x it, and exe-cute it.
If you are at all familiar with shell scripting languages, this will look very familiar. Perl combines the simplicity of shell scripting with the power of a full-fledged programming language.
The first line of this program indicates to the operating system where to find the Perl interpreter. This is standard procedure with shell scripts, and you have already seen this syntax in Chapter 21, "Shell Programming."
If /usr/bin/perl is not the correct location for Perl on your system, you can find out where it is located by typing which perl at the command line. If you do not have Perl installed, you might want to skip forward to the section titled "For More Information" to find out where you can obtain the Perl interpreter.
The second line does precisely what you would expect it to doit prints the text enclosed in quotes. The \n notation is used for a newline character.
Page 489
Although it does not have the concept of datatype (integer, string, char, and so on), Perl has several kinds of variables.
Scalar variables, indicated as $variable, are interpreted as numbers or strings, as the context warrants. You can treat a variable as a number one moment and a string the next if the value of the variable makes sense in that context.
There is a large collection of special variables in Perl, such as $_, $$, and $<, which Perl keeps track of, and you can use if you want to. ($_ is the default input variable, $$ is the process ID, and $< is the user ID.) As you become more familiar with Perl, you will find yourself using these variables, and people will accuse you of writing "read-only" code.
Arrays, indicated as @array, contain one or more elements, which can be referred to by index. For example, $names[12] gives me the 13th element in the array @names. (It's important to remember that numbering starts with 0.)
Associative arrays, indicated by %assoc_array, store values that can be referenced by key. For example, $days{Feb} will give me the element in the associative array %days that corresponds with Feb.
The following line of Perl code lists all the elements in an associative array (the foreach construct is covered later in this chapter):
foreach $key (keys %assoc){ print "$key = $assoc{$key}\n"};
NOTE |
$_ is the "default" variable in Perl. In this example, the loop variable is $_ because none was specified. |
The syntax of the Perl if/else structure is as follows:
if (condition) { statement(s) } elsif (condition) { statement(s) } else { statement(s) }
Page 490
condition can be any statement or comparison. If the statement returns any true value, the statement(s) will be executed. Here, true is defined as
For example, the following piece of code uses the if/else structure:
if ($favorite eq "chocolate") { print "I like chocolate too.\n" } elsif ($favorite eq "spinach") { print "Oh, I don't like spinach.\n"; } else { print "Your favorite food is $favorite.\n" }
Perl has four looping constructs: for, foreach, while, and until.
The for construct performs a statement (or set of statements) for a set of conditions defined as follows:
for (start condition; end condition; increment function) { statement(s) }
At the beginning of the loop, the start condition is set. Each time the loop is executed, the increment function is performed until the end condition is achieved. This looks much like the traditional for/next loop. The following code is an example of a for loop:
for ($i=1; $i<=10; $i++) { print "$i\n" }
The foreach construct performs a statement (or set of statements) for each element in a set, such as a list or array:
foreach $name (@names) { print "$name\n" }