Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2018-06-23 05:20 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.