aral has asked for the wisdom of the Perl Monks concerning the following question:
I have read numerous articles that detail how it is not possible to have a PEEK at STDIN (if it is linked to a pipe). The typical suggestion - which I would implement, given different circumstances - is to use a read-ahead buffer and redo a loop iteration, reusing the buffer.
My problem is now that I would like to use an XML parser (XML::Twig) if, and only if, the first line of the input contains a valid XML header.
So a read-ahead buffer is not doing me much good if XML::Twig does not accept an incomplete XML file (missing the header) in its "parsefile" function.
Basically, I was hoping to do it like this:my $header = peek(<>); if (is_xml ($header)) { my $t = XML::Twig->new(); $t->parse (\*STDIN); } else { # do something else with <> }
Any suggestions on how to tackle this?
|
|---|