Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Just to follow up for posterity...

It turns out that the wrapper strategy wasn't the problem at all. In fact, it's a bug in IO::Zlib... sort of. When XML::Parser::Expat calls read on a filehandle, it does something a bit unusual. You might think that it would use the read's return value to determine the amount of data actually read, but that's not what it does. Instead, it uses the len parameter of the SvPV macro (see perlguts) to determine the actual length of the buffer. But IO::Zlib's implementation of Tie::Handle's READ method doesn't touch the buffer if it's at the end of file: it just returns 0 to indicate no bytes are left. So when used together, X::P::E takes the value of the buffer from the most recent non-EOF read, and so in fact does attempt to read more than it should. What it reads is the last chunk of the file, again. Unsurprisingly, this addition of incomplete data past the closing document tag results in illegal XML, which is why I got the error I did.

You could almost call this odd behavior a bug in XML::Parser::Expat instead of IO::Zlib, except for this line in read: SCALAR will be grown or shrunk to the length actually read. So technically, IO::Zlib's read should set the buffer to "" before returning 0 at EOF.

Solving the problem is therefore just a matter of changing return 0 if $self->{'eof'}; to

if ( $self->{'eof'} ) { $$bufref = ""; return 0; };
in IO::Zlib. I'll submit the patch later today.

Relieved to be done debugging XS code with printf's,


In reply to Re: IO::Zlib with XML::Twig/XML::Parser::Expat by athomason
in thread IO::Zlib with XML::Twig/XML::Parser::Expat by athomason

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-12-07 15:01 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (33 votes). Check out past polls.