Using Perl with Linux

 < Day Day Up > 

If you are familiar with some other programming languages, chances are you can write functional Perl code. Perl contains the best features of C, BASIC, and a variety of other programming languages with a hearty dollop of awk, sed, and shell scripting thrown in. Perl was created in the mid-1980s by Larry Wall to overcome file-handling limitations of the awk command. Wall, a developer already responsible for a number of important Unix utilities, such as the patch command (used to easily update and insert changes into a program's source code), claims that Perl really stands for "Pathologically Eclectic Rubbish Lister."

Although originally designed as a data extraction and report generation language, Perl appeals to many Unix and Linux system administrators because it can be used to create new software tools 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 (those without line terminators or that contain binary data, such as some databases or compiled programs), 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.


Perl interprets its language in text files known as Perl scripts. Although some consider Perl an interpreter, the programs are actually compiled in memory and then run by Perl's internal interpreter. 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, as 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 CD-ROMs 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. The 690-line Perl program physically resides under the /etc/log.d/scripts/ directory, but is launched as the symbolic link named 00-logwatch under the /etc/cron.daily directory. This program (which can be configured by editing the configuration file logwatch.conf) generates and emails a short report after analyzing selected system logs.

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

Perl Versions

As of this writing, the current production version of Perl is 5.8.6 (which is Perl version 5 point 8, patch level 6).

You can download the code from http://www.perl.com/ and build Perl yourself from source. You will occasionally find updated versions in RPM format for Fedora, which can be installed by updating your system. See Chapter 7, "Managing Software and System Resources," 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.

A Simple Perl Program

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.

NOTE

You will also find a Perl program named perlcc installed with Perl. This program can be used as a compiler to build executable commands out of Perl! The program can also be used to generate experimental C source code for use by the gcc compiler system (see Chapter 32, "C/C++ Programming Tools for Fedora").


To introduce you to the absolute basics of Perl programming, Listing 29.1 illustrates a simple Perl program that prints a short message.

Listing 29.1. A Simple Perl Program
 #!/usr/bin/perl print "Look at all the camels!\n"; 

That's the entire program. 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.

Command-Line Error

If you get the message bash: trivial.pl: command not found or bash: ./trivial.pl: Permission denied, it means that you either typed the command line incorrectly or forgot to make trivial.pl executable (with the chmod command):

 $ chmod +x trivial.pl 

You can force the command to execute in the current directory as follows:

 $ ./trivial.pl 

Or you can use Perl to run the program like this:

 $ perl trivial.pl 


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!

NOTE

#! 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. The technical name of the # character is the octothorpe, a word coined by Bell Labs engineer Don Macpherson in the early 1960s. Another pronunciation of #! is pound-bang because most people refer to the # character on a telephone keypad as pound. This notation is also used in shell scripts. See Chapter 14, "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. Using a comment character is standard practice in shell programming.

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\n"; 

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's an infinite loop:

 #!/usr/bin/perl # a block of code to print a greeting forever while (1) {         print "hello there\n"; }; 

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. \n 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:

 $ 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.


The next several sections provide an overview of Perl's language syntax and data handling.

     < Day Day Up > 


    Red Hat Fedora 4 Unleashed
    Red Hat Fedora 4 Unleashed
    ISBN: 0672327929
    EAN: 2147483647
    Year: 2006
    Pages: 361

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net