-->
Previous Table of Contents Next


Perl

Perl is a freeware scripting language developed to handle a number of system administration tasks. Perl stands for Practical Extraction and Report Language. The whole point of the language is to make it easier for you to extract data from UNIX and output reports on things such as Usenet news disk usage and a list of all users on your systems, sorted in order of largest disk usage. (Perl tends to excel at tasks that revolve around reporting system information.)

To make sure you’ve installed perl when you install Linux, type in the following:


     gilbert:/$ perl -v

If you have perl on your system, you should see the version number for perl. If not, you need to run the setup program again. (The setup program installs Perl 5.002.)

A First Perl Script

Perl, like most Tcl and other UNIX scripting languages, uses the # as a comment marker. Any line with # is ignored from the # onward. To print data in a perl script, use the print statement:


     #! /usr/bin/perl

     print "Linux runs perl!\n";

     print "Oh, joy!\n";

When you run this script, you’ll see the following output, as you’d expect:


     Linux runs perl!

     Oh, joy!

The \n stands for a new line, or linefeed character, and is typical UNIX parlance as we described in the section on C programming.

You can also prompt for data in Perl, using the following odd syntax:


     #! /usr/bin/perl

     # Prompting for input in perl.



     print "What is your first name: ";



     # <STDIN> stands for standard input: the keyboard.

     $first_name = <STDIN>;



     # Remove trailing linefeed.

     chomp($first_name);



     printf "What is your last name: ";

     $last_name = <STDIN>;



     chomp($last_name);



     print "Your name is $first_name $last_name.\n";

When you run this script, you’ll see the following prompts:


     What is your first name: Eric

     What is your last name: Johnson

     Your name is Eric Johnson.

Perl provides a lot of support for arrays, UNIX process control, and string handling. Perl offers a string set of array operations, which allow you to have a set of data treated as one unit, for example:


     (1,2,3,4,5,6)

This array has the values 1 through 6. You can also mix text and numeric values, as shown here:


     (1, 2, 3, 4, "Linux is out the door")

You can assign this array to a variable and then access any element in the array. A great strength of perl is its associative arrays, where you can use a key value for an array index and associate this with a data value. For example, you can have a perl array for a first name, last name, and street address. You could then access the street address as shown here:


     #! /usr/bin/perl

     # Associative arrays in perl.



     # zippy is an associative array.



     $zippy{"firstname"} = "Zippy";

     $zippy{"address"} = "1600 Pennsylvania Ave.";



     # Print the data.

     print $zippy{"firstname"};

     print "'s address is ";

     print $zippy{"address"};



     # End with a carriage return.

     print "\n";

This example stores a first name and an address in the associative array named zippy. Associative arrays form a very powerful feature and can be used effectively in a lot of system administration tasks.

The output of the preceding script looks something like the following (and predicts the results of the next election):


     Zippy's address is 1600 Pennsylvania Av.

In addition to associative arrays, perl has a lot of commands to format text to allow you to create reports (the original reason for perl’s existence). perl is intimately tied in with UNIX and provides a number of shortcuts for common UNIX activities, like accessing the password file, as we show here:


     #! /usr/bin/perl

     # Accessing the password file.



     # Get Eric's password entry and print it.



     @erc_entry = getpwnam("erc");

     ($username, $realname, $homedir) = @erc_entry[0,6,7];



     print "User $realname has";

     print " a home directory of $homedir";

     print " and a username of $username.\n";

When you run this script, you’ll see output like the following:


     User Eric F. Johnson has a home directory of /home/erc

     and a username of erc.

Naturally, you’ll want to use a username available on your system.

There’s a lot more to perl, which fills more than one book on the subject. If you’re interested in learning more about perl, see Appendix A.


Previous Table of Contents Next