-->

Previous | Table of Contents | Next

Page 491

while

while performs a block of statements while a particular condition is true:


while ($x<10) {

     print "$x\n";

     $x++;

     }

until

until is the exact opposite of the while statement. It will perform a block of statements while a particular condition is false—or, rather, until it becomes true:


until ($x>10) {

     print "$x\n";

     $x++;

     }

Regular Expressions

Perl's greatest strength is in text and file manipulation. This is accomplished by using the regular expression (regex) library. Regexes allow complicated pattern matching and replacement to be done efficiently and easily.

For example, the following one line of code will replace every occurrence of the string Bob or the string Mary with Fred in a line of text:


$string =~ s/bob|mary/fred/gi;

Without going into too many of the gory details, Table 24.1 explains what the preceding line says.

Table 24.1. Explanation of $string =~ s/bob|mary/fred/gi;.


Element Explanation
$string =~ Performs this pattern match on the text found in the variable called $string.
s Substitute.
/ Begins the text to be matched.
bob|mary Matches the text bob or mary. You should remember that it is looking for the text mary, not the word mary; that is, it will also match the text mary in the word maryland.
/ Ends text to be matched, begin text to replace it with.
fred Replaces anything that was matched with the text fred.

                                                               continues

Page 492


Table 24.1. continued


Element Explanation
/ Ends replace text.
g Does this substitution globally; that is, wherever in the string you match the match text (and any number of times), replaces it.
i The search text is case-insensitive. It will match bob, Bob, or bOB.
; Indicates the end of the line of code.

If you are interested in the gory details, I recommend the book Mastering Regular Expressions by Jeffrey Friedl, which explains regular expressions from the ground up, going into all the theory behind them and explaining the best ways to use them.

Although replacing one string with another might seem like a rather trivial task, the code required to do the same thing in another language, for example, C, is rather daunting.

Access to the Shell

Perl is useful for administrative functions because, for one thing, it has access to the shell. This means that any process that you might ordinarily do by typing commands to the shell, Perl can do for you. This is done with the `` syntax; for example, the following code will print a directory listing:


$curr_dir = `pwd`;

@listing = `ls -la`;

print "Listing for $curr_dir\n";

foreach $file (@listing) {

     print "$file";

     }





NOTE
The `` notation uses the backtick found above the Tab key, not the single quote.

Access to the command line is fairly common in shell scripting languages, but is less common in higher level programming languages.

Command-Line Mode

In addition to writing programs, Perl can be used from the command line like any other shell scripting language. This enables you to cobble together Perl utilities on-the-fly, rather than having to create a file and execute it.

Page 493

For example, running the following command line will run through the file foo and replace every occurrence of the string Joe with Harry, saving a backup copy of the file at foo.bak:


perl -p -i.bak -e s/Joe/Harry/g foo

The -p switch causes Perl to perform the command for all files listed (in this case, just one file).

The -i switch indicates that the file specified is to be edited in place, and the original backed up with the extension specified. If no extension is supplied, no backup copy is made.

The -e switch indicates that what follows is one or more lines of a script.

Automation Using Perl

Perl is great for automating some of the tasks involved in maintaining and administering a UNIX machine. Because of its text manipulation abilities and its access to the shell, Perl can be used to do any of the processes that you might ordinarily do by hand.

The following sections present examples of Perl programs that you might use in the daily maintenance of your machine.

Moving Files

One aspect of my job is administering a secure FTP site. Incoming files are placed in an "incoming" directory. When they have been checked, they are moved to a "private" directory for retrieval. Permissions are set in such a way that the file is not shown in a directory listing, but can be retrieved if the filename is known. The person who placed the file on the server is informed via e-mail that the file is now available for download.

I quickly discovered that people were having difficulty retrieving files because they incorrectly typed the case of filenames. This was solved by making the file available with an all-uppercase name and an all-lowercase name, in addition to the original filename.

I wrote the Perl program in Listing 24.2 to perform all those tasks with a single command. When I have determined that a file is to go onto the FTP site, I simply type move filename user, where filename is the name of the file to be moved, and user is the e-mail address of the person to be notified.

Listing 24.2. Moving files on an FTP site.


 1: #!/usr/bin/perl

 2: #

 3: #  Move a file from /incoming to /private

 4: $file = @ARGV[0];

 5: $user = @ARGV[1];

 6:

 7: if ($user eq "") {&usage}

 8: else {


                                         continues

Previous | Table of Contents | Next