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

Is there anyway in XML Parser (I am using it within XML::Smart) to have an error passed to a log instead of aborting the entire script?
I'm running through 35k of XML files and I'd like to see the ones that don't parse without having to rerun it each time it encounters an XML error?

Replies are listed 'Best First'.
Re: XML Parser Error Handling
by borisz (Canon) on Mar 10, 2004 at 21:53 UTC
    use eval to run the part that throws the error.
    for my $xml_file ( @xml_files ) { eval { # do something with $xml_file }; if ( $@ ) { print $logfile "file: $xml_file has errors ( $@ )\n"; } }
    Boris
Re: XML Parser Error Handling
by mirod (Canon) on Mar 10, 2004 at 21:54 UTC

    Just follow the link to XML::Parser: the review, or type 'XML::Parser' in the Search box, and you will see the usual way to do this: wrap the parsing in an eval.

      Thanks everyone. I didn't try something as easy as searching for the module's name. If I hadn't spent a few hours working on this I wouldn't have asked.
      Thanks again!
Re: XML Parser Error Handling
by diotalevi (Canon) on Mar 10, 2004 at 21:52 UTC
    for my $file ( ... ) { eval { my $parser = XML::Smart->new( ... ); ... # Code that may die 1; } or warn "Something appropriate: $@"; }
Re: XML Parser Error Handling
by kvale (Monsignor) on Mar 10, 2004 at 21:55 UTC
    If you want to trap die, eval the code that asserts it:
    eval { # dangerous code} if ($@) { print "It up and died: $@\n"; }

    -Mark