< Catching Errors >

You can catch otherwise fatal errors by putting the parse or parsefile method inside an eval { } and checking $@.


use strict;
use XML::Parser;

my $p = new XML::Parser();
die "catch_error.pl <xml-file>\n" unless $ARGV[0] && -e $ARGV[0];
eval { $p->parsefile($ARGV[0]) };
print "Caught error: $@\n" if $@;
print "Done.\n";