hackermike has asked for the wisdom of the Perl Monks concerning the following question:

I am intrigued by how really difficult it is to do "Practical Extract and Report Language" tasks. This little script extracts the plain text from the body of a users's eMail msg on my server's incoming storage dir and works fine when there is only one message. The thing needed now is that since each new message is appended to the file in the server < /var/email/ > I need to get the script to extract from the LAST messsage. How do I instruct the script to find the LAST instance of the text for splitting? Or, can the file be read from the end up? Could all messages but the last one be deleted by the script? (After the first call, there would be only the one message in the file, don't want that deleted until users sends another). thx, mike
#!/usr/bin/perl $filename2 = "/usr/local/etc/httpd/htdocs/dancewithdebbie/email.txt"; $filename = "/var/mail/debbienl"; $redirect = "http://www.dcdancenet.com/dancewithdebbie/news2.shtml"; open(FILE,"$filename"); @lines = <FILE>; close(FILE); $start=0;$finish=0; open(WRITE,">$filename2") || die "Can't open $filename2!\n"; foreach (@lines) { chomp; #now you can ignore the \n if ($_ eq "Dance with Debbie") { $start=1; } if ($_ eq "Content-Type: text/html;") { last; } if ($start ==1) { print WRITE "$_\n"; #add it back in } } close (WRITE); print "Location: $redirect\n\n";
  • Comment on Extracting the last desired block of text from a server eMail file
  • Download Code

Replies are listed 'Best First'.
Re: Extracting the last desired block of text from a server eMail file
by bpphillips (Friar) on Sep 16, 2004 at 19:38 UTC
    One of my favorite IO:: modules is IO::All which wraps up a bunch of different IO operations into a surprisingly easy to use module.

    One of these operations allows you to read the file backwards (using File::ReadBackwards):
    # not tested of course my @message; my $io = io('mail_file')->backwards->chomp; while (my $line = $io->getline) { unshift @message, $line; last if($line =~ m/$message_start_pattern/); } # @message now contains all lines of the # last message in correct order for(@message){ last if($_ eq "Content-Type: text/html;"); print "$_\n"; }
    You of course could use File::ReadBackwards by itself

    --b
      Thx for your reply! My ISP does not have the File::ReadBackwards): Installed. Tried, but do not have the permisssions to copy it in the perl folder. I have hacked the code every which way, even resorted to
      open(FILE,"$filename"); @lines = <FILE>; close(FILE); @lines = reverse sort(@lines);
      preceeding my origninal code. Oddly, while some text is printed to the output filename2, I is weirdly done, prints some lines in the correct order but does not strickly follow the test for split directive. Any ideas? thx mike @lines = reverse sort(@lines);
Re: Extracting the last desired block of text from a server eMail file
by Anonymous Monk on Sep 17, 2004 at 23:27 UTC
    #!/usr/bin/perl $/='Dance with Debbie'; open(FILE,"$filename"); @lines = <FILE>; close(FILE); print $lines[-1];