-->

Previous | Table of Contents | Next

Page 494

Listing 24.2. continued


 9:      if (-e "/home/ftp/incoming/$file")

10:           {`cp /home/ftp/incoming/$file /home/ftp/private/$file`;

11:           chmod 0644, "/home/ftp/private/$file";

12:           `rm -f /home/ftp/incoming/$file`;

13:           if (uc($file) ne $file)  {

14:                $ucfile = uc($file);

15:                `ln /home/ftp/private/$file /home/ftp/private/$ucfile`;

16:               }

17:           if (lc($file) ne $file)  {

18:                 $lcfile = lc($file);

19:                 `ln /home/ftp/private/$file /home/ftp/private/$lcfile`;

20:                }

21:

22: # Send mail

23: open (MAIL, "| /usr/sbin/sendmail -t  ftpadmin,$user");

24: print MAIL <<EndMail;

25: To: ftpadmin,$user

26: From: ftpadmin

27: Subject: File ($file) moved

28:

29: The file $file has been moved

30: The file is now available as

31: ftp://ftp.databeam.com/private/$file

32:

33: ftpadmin\@databeam.com

34: =================================

35: EndMail

36: close MAIL;

37: }

38:

39:     else {  #  File does not exist

40:          print "File does not exist!\n";

41:          }   #  End else (-e $file)

42:

43: } #  End else ($user eq "")

44:

45: sub usage {

46: print "move <filename> <username>\n";

47: print "where <username> is the user that you are moving this for.\n\n";

48: }



Without going through Listing 24.2 line-by-line, the following paragraphs take a look at some of the high points that demonstrate the power and syntax of Perl.

In lines 4_5, the array @ARGV contains all command-line arguments. The place where one argument ends and another begins is taken to be every space, unless arguments are given in quotes.

In line 9, the -e file test tests for the existence of a file. If the file does not exist, perhaps the user gave me the wrong filename, or one of the other server administrators beat me to it.

Page 495

Perl enables you to open a pipe to some other process and print data to it. This allows Perl to "use" any other program that has an interactive user interface, such as sendmail, or an FTP session. That's the purpose of line 23.

The << syntax allows you to print multiple lines of text until the EOF string is encountered. This eliminates the necessity to have multiple print commands following one another—for example,


24: print MAIL <<EndMail;

...

35: EndMail

The subroutine syntax allows modularization of code into functions. Subroutines are declared with the syntax shown in lines 45_48, and called with the & notation, as shown in line 7:


7: ... {&usage}

...

45: sub usage {

...

48: }

Purging Logs

Many programs maintain some variety of logs. Often, much of the information in the logs is redundant or just useless. The program shown in Listing 24.3 removes all lines from a file that contain a particular word or phrase, so lines that you know are not important can be purged.

Listing 24.3. Purging log files.


 1: #!/usr/bin/perl

 2: #

 3: #       Be careful using this program!!

 4: #       This will remove all lines that contain a given word

 5: #

 6: #       Usage:  remove <word> <file>

 7: ###########

 8: $word=@ARGV[0];

 9: $file=@ARGV[1];

10:

11: unless ($file)  {

12: print "Usage:  remove <word> <file>\n"; }

13:

14: else    {

15: open (FILE, "$file");

16: @lines=<FILE>;

17: close FILE;

18:

19: # remove the offending lines

20: @lines = grep (!/$word/, @lines);

21:

22: #  Write it back

23: open (NEWFILE, ">$file");

24: for (@lines)    { print NEWFILE }

25: close NEWFILE;

26:         }  #  End else

Page 496

Listing 24.3 is fairly self-explanatory. It reads in the file and then removes the offending lines using Perl's grep command, which is similar to the standard UNIX grep. If you save this as a file called remove and place it in your path, you will have a swift way to purge server logs of unwanted messages.

Posting to Usenet

If some portion of your job requires periodic postings to Usenet—a FAQ listing, for example—the following Perl program can automate the process for you. In the sample code, the text that is posted is read in from a text file, but your input can come from anywhere.

The program shown in Listing 24.4 uses the Net::NNTP module, which is a standard part of the Perl distribution.

Listing 24.4. Posting an article to Usenet.


 1: #!/usr/bin/perl

 2: open (POST, "post.file");

 3: @post = <POST>;

 4: close POST;

 5: use Net::NNTP;

 6:

 7: $NNTPhost = `news';

 8:

 9: $nntp = Net::NNTP->new($NNTPhost)

10:         or die "Cannot contact $NNTPhost: $!";

11:

12: # $nntp->debug(1);

13: $nntp->post()

14:         or die "Could not post article: $!";

15: $nntp->datasend("Newsgroups: news.announce\n");

16: $nntp->datasend("Subject: FAQ - Frequently Asked Questions\n");

17: $nntp->datasend("From: ADMIN <root\@rcbowen.com>\n");

18: $nntp->datasend("\n\n");

19: for (@post)     {

20: $nntp->datasend($_);

21: }

22:

23: $nntp->quit;

For More Information

The Perl community is large and growing. Since the advent of the WWW, Perl has become the most popular language for Common Gateway Interface (CGI) programming. There is a wealth of sources of information on Perl. Some of the better ones are listed here. The following books are good resources:

Page 497

On Usenet, check out the following:

Check these sites on the World Wide Web:

Summary

Perl, in the words of its creator, Larry Wall, "combines the best elements of C, sed, awk, and sh," but is also a great language for folks who have no experience with these languages.

Perl's powerful regex library and ease of use have made it one of the preferred scripting languages in use today, particularly in the realm of CGI programming. Many people even think of Perl as exclusively a CGI language, when, in fact, it is capable of so much more.

Although this book is focused on Red Hat Linux, Perl is also available for many other platforms, and scripts that you write in Perl on one platform will run without changes on another.

Previous | Table of Contents | Next