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

Hi Monks!

I am parsing some XML but sometimes the XML doc coming in is not well formated, I am looking to trap the erros and after submit to myself an email. The problem I am having is this:
Software error: Can't call method "appendChild" on an undefined value at


And here is the code I am using. Is there any other way to trap the erros?
my $xp = XML::XPath->new(filename => $open_this); #####Test my ($parser,$list_item,$file_list,$root_node); eval { $parser = XML::XPath::XMLParser->new( filename => $xp-> $open_ +this); $root_node = $parser->parse; $xp->find('/*', $root_node); }; if ($@) { # if we get an error, include the details in the output. $list_item->appendChild(XML::XPath::Node::Text->new("ERROR par +sing file '$open_this': ". $@) ); $file_list->appendChild($list_item); next FILE; }
Thanks!!!

Replies are listed 'Best First'.
Re: XML::Path Errors
by Corion (Patriarch) on Apr 04, 2008 at 15:30 UTC

    It's just as Perl says - $list_item is undefined and Perl cannot call a method on the undefined value. You will have to assign $list_item a meaningful value before you try to call methods on it. Maybe you meant to pass it as a parameter?

      I see the $list_item been the processed result(s) of the eval sub, shouldn’t it have something coming from there?

        Your reply makes no sense. $list_item is not assigned to anywhere in the code you show. Maybe you mean something else?

Re: XML::Path Errors
by pileofrogs (Priest) on Apr 04, 2008 at 17:26 UTC

    I don't know if this is exactly what you want, but you might want to add this for any cases that you fail to catch.

    $SIG{__DIE__} = sub { email_me(shift) };

    Where email_me is a subroutine that sends the 1st arg to your email.

    Since you're talking about emailing yourself, I assume you're running this unattended. The snippet above in any unattended scripts can save your bacon.