Greetings Wise Monks,

Normally when I have a perl problem I at least have an idea where to start looking but I think I'm missing something fundamental (well that and "filehandle" "filter" "io" are crappy search terms) So I'm hoping that someone here will has a suggestion of where I should start.

I have a huge (200+Mb to 2Gb with no line breaks) XML file that gets inserted into a SQL DB every week. It uses XML::Parser::PerlSAX to avoid trying to load the whole thing into memory. However the XML generator (which is outside of my control) frequently generates invalid XML, now I have a number of regexp substitutions that fix up the file but pre-processing to a temp file is a _real_ pain. so I'd like to switch to stream filter that could sit in between the input filehandle and PerlSAX. However as I not totally comfortable with the underlying C-ness of perl IO I'm at a bit of a loss on how to start.

I could just pipe the input through another perl script but how do I hook into the read request (overloading the < > opperator?) so that I only read from the source file when PerlSAX tries to read from its filehandle. If I wrote a straight STDIN->process->STDOUT type filter my gut says that it would go all out reading the XML (most likely faster that PerlSAX would read from the other end) as run out of buffering space.

What I really want is some kind of callback on a filehandle when a read request is issued, such that I could then read from the source, filter it and provide the result to PerlSAX via a filehandle. But don't have enough mojo to know where to start.

I looked through all the Filter::*, IO:: CPAN modules and found an output filehandle filter (Filter::Handle) but I didn't seem adaptable.

Anyone done anything like this? Or perhaps suggest an alternate approach. Any suggestions welcome.


In reply to Input Filehandle Filtering by Anonymous Monk

Title:
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, details, 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, summary, 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.