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

WISE ONES,
I want to use to parse & process all (242) message in an inbox.
I suspect it is something simple, but I can't seem to make it work for me.
Please point me in the right direction.
TIA!
#!/usr/bin/perl -w # use MIME::Base64 (); use MIME::Parser; #my $raw_file = "/tmp/bcm"; my $raw_file = "/var/spool/mail/schering"; # the file above has 242 messages in it. open(RAW,$raw_file); my $msg_cnt = 0; my $line_cnt = 0; my $msg_start = -1; my ($subject, $pdf_name, $decoded); $subject = "blank"; my ( $line, $pdf_start, $pdf_close ); $pdf_start = 0; $pdf_close = 0; ### Create parser, and set some parsing options: $parser = new MIME::Parser; $parser->output_under("/tmp/mimemail"); # while ( <RAW> ) { $line = $_; $line_cnt++; $msg_start = index( $line,"From "); if ( $msg_start == 0 ) { $msg_cnt++; # $head = MIME::Head->read(\*RAW); $subject = $head->get('Subject:', 0); chop($subject); $subject =~ s/ /_/g; $subject =~ s#/#-#g; $subject =~ s#,##g; $subject =~ s#'##g; $subject =~ s#&##g; $subject =~ s#\(##g; $subject =~ s#\)##g; print "$subject\n"; # # when the two $entity lines are uncommented, #the code stops processing after the 1st message. # What change is required to get all the messages parsed & processed? # with the two lines immediately below #(which are now commented out and start with $entity) # All 242 subjects get printed. #$entity = $parser->parse(\*RAW) or die "parse failed\n"; #$entity->dump_skeleton; $msg_start = 1; # } } exit;
You can lead {some|many|most} to knowledge, but a few will steadfastly refuse to think (for themsleves). Clue Repellant!

Replies are listed 'Best First'.
Re: MIME::Parser
by starbolin (Hermit) on Apr 19, 2005 at 04:34 UTC

    Very well written question solbeach. The expression:

    $entity = $parser->parse(\*RAW) returns a structure, instead of a True/False, so "or die" gets confused. The parse method will issue a "die" exception if it can't parse the message. So you don't need to do that here. You can check if it returns "undef" though.

    You should add checking on the "open" statment as this could fail for any of a number of reasons.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}