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

Hopefully someone here is successfully using Mail::Field that can help me. I am trying to parse a received header and not having much success. Can someone point me to an example of using Mail::Field or tell me what is wrong with my code?
my $received=Mail::Field->new('Received',$string); my $parse_tree=$received->parse(); my $by=$parse_tree->{'by'}; print($by->{'domain'}."\n");
The error says that "$by" is initialized. Presumably that means 'parse()' is not returning what the documentation says it should. Indeed, when I use Data::Dumper, it seems all I get is the key 'TEXT' and the value is simply the original string. Thanks.

Replies are listed 'Best First'.
Re: Mail::Field Help Needed
by toolic (Bishop) on Aug 31, 2008 at 17:56 UTC
Re: Mail::Field Help Needed
by wfsp (Abbot) on Sep 01, 2008 at 08:33 UTC
    windows/AS 5.8

    After installing Mail::Field::Received using ppm and using the examples in the synopsis I got the following.

    #!/usr/local/bin/perl use strict; use warnings; use Mail::Field; my $header = q{from tr909.mediaconsult.com (mediacons.tecc.co.uk [193. +128.6.132]) by host5.hostingcheck.com (8.9.3/8.9.3) with ESMTP id VAA +24164 for <adam@spiers.net>; Tue, 1 Feb 2000 21:57:18 -0500}; print qq{header: $header\n}; my $received = Mail::Field->new(q{Received}, $header); my $results = $received->parse_tree(); # hangs perl #if ($received->parse_ok){ # print qq{parse ok\n}; #} #else{ # print qq{parse not ok\n}; #} print qq{by -> domain: $results->{by}{domain}\n};
    outputs
    header: from tr909.mediaconsult.com (mediacons.tecc.co.uk [193.128.6.1 +32]) by host5.hostingcheck.com (8.9.3/8.9.3) with ESMTP id VAA24164 f +or <adam@spiers.net>; Tue, 1 Feb 2000 21:57:18 -0500 by -> domain: host5.hostingcheck.com
    On my setup the $received->parse_ok method hung perl (see update) but the $received->parse_tree worked. I haven't had a chance to look at the source to identify why.

    update: see broomduster's reply pointing out my typo that caused the error.

      I had a few minutes to spare, so I thought I'd have a go at this. After banging my head for more than just a few minutes, I finally noticed...

      The method is parsed_ok (note the 'd'). With that change, your code works for me. Hopefully it will for the OP, as well.

        broomduster++

        That fixed it, I was clearly not fully awake.

        Many thanks

      Interesting. I've abandoned 'parse_tree' because for me it goes into a loop saying the function was depreciated. Also the documentation said I should not normally be calling those functions directly but rather via Mail::Field and in this case, 'parse()'.
Re: Mail::Field Help Needed
by apl (Monsignor) on Aug 31, 2008 at 20:33 UTC
    Could you provide the exact text of the error returned?
      Thanks for the reply.

      Use of uninitialized value $by in concatenation (.) or string at ...

      The message is referring to the 'print' statement.
        I've never used the package, but the CPAN documentation mentions the parsed_ok method, which would let you know if the parse() was successful. Using that might help you shed some light on the problem.
Re: Mail::Field Help Needed
by Krambambuli (Curate) on Sep 01, 2008 at 08:14 UTC
    You'll probably have to first extra manually install Mail::Field::Received from CPAN.

    Simply installing Mail::Field doesn't install that module, and Mail::Field itself is just a base class which you need to enrich yourself with the knowledge about Received: headers.

    Krambambuli
    ---
    Occasionally enjoying Mark Jason Dominus' Higher-Order Perl
      I think you are on to something here. I thought if it was not installed I'd get an error but apparently not. I tried to install it anyway and expected the 'up to date' message. However, that did not happen and it really tried to install it. Unfortunately it would not install and I cannot see why. Here is my output:
      Mail-Field-Received-0.24/MANIFEST CPAN: File::Temp loaded ok (v0.20) CPAN.pm: Going to build A/AS/ASPIERS/Mail-Field-Received-0.24.tar.gz Checking if your kit is complete... Looks good Writing Makefile for Mail::Field::Received cp Received.pm blib/lib/Mail/Field/Received.pm ASPIERS/Mail-Field-Received-0.24.tar.gz /usr/bin/make -- OK Running make test /usr/bin/perl.exe "-MExtUtils::Command::MM" "-e" "test_harness(0, 'bli +b/lib', 'b lib/arch')" t/*.t t/all....1/18 stringify() not implemented at /usr/lib/perl5/site_perl/ +5.10/Mail/ Field.pm line 190 Mail::Field::stringify('Mail::Field::Received=HASH(0x101e2da0) +') called at t/all.t line 209 t/all.... Dubious, test returned 255 (wstat 65280, 0xff00) Failed 3/18 subtests
      In this case it is not obvious why it is failing the tests. I can't tell what "stringify" it is trying to access but I've successfully used Mail::Field::stringify. I tried a force install but I still get the same error when I run my test script.