An Example: Extract Subjects and Save Them

E-mail mailboxes are one of those formats that Perl is really good at managing because each message follows a very specific format (based on the protocol RFC822), and all are collected in a plain-text file. If you want to range over a mailbox and do something to messages that fits a specific criteria, then Perl is your language.For this example, however, let's do something really simple. The script in Listing 15.1 takes a mailbox as an argument on the script command line, reads in each message, extracts all the Subject lines, and writes a file called subjects in the same directory containing that list of subjects. If the file subjects already exist, this script will overwrite it (we'll learn in the next few sections how to test to see if the file exists and complain if it does).

Listing 15.1 shows the (very simple) code.

Listing 15.1. The subject.pl Script
1:  #!/usr/local/bin/perl -w
2:  use strict;
3:
4:  open(OUTFILE,">subjects.txt") or die "Can't open subjects file: $!
";
5:
6:  while (<>) {
7:      if (/^Subject:/) {
8:          print OUTFILE $_;
9:      }
10: }
11: close OUTFILE;
					

A short script almost not worth pointing out as an example, you might think. But that's just the point—reading from and writing to files uses the same techniques you've used all along for standard input and output. There are two things to watch for here. First, line 4 opens the file subjects for writing (note the > character at the start of the filename). Second is line 8, where we print to that same OUTFILE file handle, rather than to standard output.

This script has no visual output, although if you run it on a file of mail, and then examine the subjects.txt file, you'll see lines like this (I ran this particular example on a file of “commercial mail,” otherwise known as spam—hence the strange subjects):

Subject: FREE SOFTWARE TURN$ COMPUTER$ INTO CA$H MACHINE$!!
Subject: IBM 33.6 PCMCIA Modem $89.00
Subject: 48 MILLION Email Leads $195 + BONUSES
Subject: Re: E-ALERT: URGENT BUY RECOMMENDATION
Subject: Make $2,000 - $5,000 per week -NOT MLM
Subject: Email your AD to 57 MILLION People for ONLY $99
Subject: SHY?.....................................
Subject: You Could Earn $100 Every Time the Phone Rings!!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset