SPUG: can't die() from XML::Parser handlers?

Colin Meyer cmeyer at helvella.org
Sat Sep 15 18:04:09 CDT 2001


Hi, Jason, 

On Fri, Sep 14, 2001 at 09:48:53PM -0700, Jason Lamport wrote:
> I've discovered what I consider very strange behaviour in XML::Parser:
> 
> I'm using the 'Subs' style of parsing, like this:

[ snipped some code ... ]

> 
> sub table {
> 	my $table_id;
> 
> 	# ... some stuff to find the "id" attribute
> 
> 	die( "table must have an id attribute" ) unless defined $table_id;
> 
> 	# ... more stuff
> }
> 
> __END__
> 
> Now, it turned out that due to an error in the XML file, $table_id 
> was in fact undef inside table().  The weird thing is that the die() 
> call isn't causing the program to die:  it just exits the handler but 
> keeps on going parsing the XML file as if nothing had happened.  I 
> can only assume that XML::Parser is calling the handlers from inside 
> an eval block (which would make sense) and then ignoring the error 
> messages (which doesn't make any sense at all).

Your guess is correct. Here's an excerpt of the XML::Parser code that
shows it ignoring any error in your sub:

###################################################################
 
package XML::Parser::Subs;
$XML::Parser::Built_In_Styles{Subs} = 1;
 
sub Start {
  no strict 'refs';
  my $expat = shift;
  my $tag = shift;
  my $sub = $expat->{Pkg} . "::$tag";
  eval { &$sub($expat, $tag, @_) };
}

###################################################################

I am not sure if there is a reasonable explanation of why the error
is being trapped and ignored.  Errors seem to propagate in most
other areas of that code.  Perhaps you should provide the one or
two line patch to Clark Cooper to fix this up.

Personally I find it much easier to use one of the CPAN modules that
provides a higher level of abstraction from the parsing for dealing with
xml documents. For data extraction from xml, I find myself leaning
towards XML::XPath. XPath, part of the XSLT specification, provides a
regular-expression like wildcard language for extracting specific pieces
of data from an xml document. For xml transformations, or filtering, I
have been using XPathScript, or Template-Toolkit with XML::XPath. These
CPAN modules typically provide decent error handling.

There are many other great xml modules on the CPAN, for building dom or
dom-style data structures, for wrapping database query returns in xml,
and just about anything you can imagine to do with xml. Modules that
support the SAX interface can be chained together in a pipeline fashion
to create very complex xml transforms.

Have fun,
-C.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list