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

I am learning how to use the various classes of MIME::Tools. I am getting the hang of it but have recently run into a wall. I am trying to get the Subject header just to display - and it is a no go.

I have made sure to have "use" on all of the modules involved (Parser, Entity, and Head). running with strict, warnings and diagnostics. Nothing in the error logs.

Can anyone see where I am going wrong? I have commented out the the alternate code (though not all of it's variations) I attempted to use so you can see the alternate path I tried.

eval { # OK here is where we will try to use the MIME::Tools suite of clas +ses # Wish me luck - Hopefully I won't screw it up too much. my $parser = new MIME::Parser; my $entity = $parser->parse_data($email) or die("Bad Parse. "); # Tried this - doesn't work #my $crap = (defined($entity->head->get('subject',0)))?$entity->hea +d->get('Subject',0):'undef'; #$page->p("Subject: $crap<br />\n"); # Also tried this - doesn't work $page->p('Subject: '.$entity->head->get('subject',0)); # The wierd part - this does work. Even when the other # parts are still in $page->p($entity->head->print()); }; if ($@) { print $page -> p("Error occured $@ \n"); }
Update: Solved.

I am an idiot. Finally figured out what was going wrong. I forgot to print the stupid $page->p. Just shoot me

life is a game... so have fun.

Replies are listed 'Best First'.
Re: trouble with MIME::Tools
by McD (Chaplain) on Oct 24, 2005 at 11:53 UTC
    I've found it useful for myself in the past to break down the steps involved, rather than trying for that one great swoopy assignment right off the bat.

    my $headers = $entity->head(); die "No headers in entity!" unless defined $headers; my $subject = $headers->get('Subject'); $subject ||= '(empty)'; print "Subject is: $subject\n";
    Once you get it working, you can try collapsing it back to something briefer, or not, depending on clarity and your specific needs. But for starters, I would try breaking down the steps and insuring each one works along the way.

    Peace,
    -McD
Re: trouble with MIME::Tools
by GrandFather (Saint) on Oct 24, 2005 at 09:13 UTC

    Can you provide sample contents for $page or $email that cause the failure? A self contained example is much more likely to attract usefull answers than partial code that likely misses the crucial information - like that data being parsed in this case.

    It may be worth checking that line endings are as you expect and that the character class is as you expect.


    Perl is Huffman encoded by design.

      $page is strictly for display (use CGI module)

      $email is a string that holds the email message in it's entirty (verified earlier by being printed out). It was loaded using Net::POP3. Specifically with the get method.

      life is a game... so have fun.

        The following works for me. What's your beef? Give a similar example that doesn't work for you.

        use warnings; use strict; use MIME::Parser; my $email = 'Subject: none'; my $parser = new MIME::Parser; my $entity = $parser->parse_data ($email) or die "Bad Parse. "; my $subject = $entity->head->get ('subject', 0); my $crap = defined $subject?$ entity->head->get ('Subject', 0) : 'unde +­f'; print $crap;

        Perl is Huffman encoded by design.