Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: IO::Zlib with XML::Twig/XML::Parser::Expat

by athomason (Curate)
on Aug 24, 2002 at 00:51 UTC ( #192503=note: print w/replies, xml ) Need Help??

in reply to IO::Zlib with XML::Twig/XML::Parser::Expat

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,


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://192503]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-09-26 19:20 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (297 votes). Check out past polls.