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

I am developing a script for parsing email attachments with XML data in them. The script runs just fine when there is only one message in the inbox but breaks when there is more than one. The error message is as follows:</>

Can't call method "XMLin" on unblessed reference at new_test.pl line 126.

As can be seen in the following code snippet, I have tried a couple of different things including using bless but I am not sure what the problem is.

foreach my $msgnum (keys %$msgnums) { print "Parsing email...\n"; #Get current messgae and create MIME object $lines = $pop3->get($msgnum); $current = join('', @$lines); $parsed = Email::MIME->new($current); #For each part in the message, extract the xml data foreach $part ($parsed->parts) { if (($part->{ct}->{discrete} eq 'application') && ($pa +rt->{ct}->{composite} eq 'plain')) { $xmlData = $part->body; #print $xmlData; #$xml = $xml->XMLin($xmlData); } } #For each extracted part of the xml data, parse the xml $xml = $xml->XMLin($xmlData); print "Component Data\n"; .... }

Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Unblessed Reference Error, XMLin
by bart (Canon) on Apr 09, 2012 at 20:06 UTC
    $xml = $xml->XMLin($xmlData);
    In each loop, you're replacing the previous value in $xml with new data. That is bound to go wrong the second time you go through the loop body.

    Most likely you need to use a different variable.

Re: Unblessed Reference Error, XMLin
by Anonymous Monk on Apr 09, 2012 at 21:53 UTC
    You want to instantiate your object one time, setting $xml as a reference to that, then you want to invoke methods of that one object as-needed. If those methods return a useful value, capture it. But don't replace the value of the variable that refers to the object you're invoking!