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

I'm trying to parse an XML doc with XML::Twig XML::Simple*, which apparently is calling XML::SAX::PurePerl under the hood. At some point in the 18 MB XML file the parser chokes and throws an exception:

Invalid quote token [Ln: 1, Col: 14]

The problem is, I don't know what line in the file caused the error. I found the method that generates that output (I think) in XML::SAX::PurePerl and tried to redefine it to print the input data. I added the following code to the bottom of my script:

{ package XML::SAX::PurePerl; sub quote { my ($self, $reader) = @_; my $data = $reader->data; # Original # $data =~ /^(['"])/ or $self->parser_error("Invalid quote tok +en", $reader); # Modified $data =~ /^(['"])/ or do { $data =~ /^(.)/; my $quote_char = $1; warn "Invalid quote token found: -->$quote_char<--\n" . "Source line follows:\n" . $data; $self->parser_error("Invalid quote token", $reader); }; $reader->move_along(1); return $1; } }

Now, however, instead of displaying the warning message when the system hits the mystery line, I get a different error:

Can't locate object method "new" via package "XML::SAX::PurePerl" at C +:/Perl_5.8.8/site/lib/XML/SAX/ line 43.

I thought I was using package appropriately, but I obviously changed more than I expected. I'm a bit rusty (haven't had much time to code lately) and I think I'm missing a fundamental concept. Whack away with the clue-stick, please.


*Updated after additional debugging, per Re^2: Redefining a method in XML::SAX::PurePerl.

Replies are listed 'Best First'.
Re: Redefining a method in XML::SAX::PurePerl
by Anonymous Monk on Jul 31, 2009 at 18:21 UTC
    use XML::SAX::PurePerl; package XML::SAX::PurePerl; ...
    package XML::SAX::PurePerl::bobf; use XML::SAX::PurePerl; use base 'XML::SAX::PurePerl'; XML::SAX->add_parser(q(XML::SAX::PurePerl::bobf)); ...

      Thanks, AM, that did the trick. I assumed when XML::SAX::PurePerl was loaded via Twig I could just piggyback off of it. I didn't know I had to explicitly 'use' it in my script as well.

        unless it is loaded before you try overriding, it will overrides your override, and the only way to be sure is to load it yourself first.
Re: Redefining a method in XML::SAX::PurePerl
by mirod (Canon) on Aug 01, 2009 at 06:35 UTC

    I am very surprised to learn that XML::Twig uses XML::SAX::PurePerl. All these years I thought it was using XML::Parser! ;--)

    Seriously, I don't understand how XML::Twig could use anything else than XML::Parser to parse XML. Could you explain a little more under which circumstances this happens?

      You're right (of course). The program that I'm working on calls 3 custom modules. The two heavy-lifters use XML::Twig, but under certain conditions they call a third that uses XML::Simple to parse output from a short SOAP message. I didn't realize it at the time, but the method that threw the error described in the OP was from the third rather than the one of the first two.

      I'll update the OP with the correction. Thanks for the poke - I made a faulty assumption.

        XML::Twig has a method called simplify, that emulates XML::Simple's XMLin, maybe you can use it.