Ah -- sorry, I didn't catch the issue about passing the pipe file handle to XML::Parser.

If the idea of using IO::Unread doesn't work out (though I expect it will do fine), there's also the possibility that you can use a script like the one I suggested, but instead of having two different subs in the dispatch table, you would have two different command lines -- one being your (separate) XML parser script invoked in a way to handle one type of stream, and the other(s) for handling the other type(s) of stream, and you just launch the right one as a subprocess:

my @holdem; my %dispatch = ( one => [ qw/my_parser --this_way/ ], two => [ qw/my_parser --that_way/ ], ); my $target; open( $pipe, "-|", "some_xml_generator" ); # not "ls", obviously while ( <$pipe> ) { if ( $target ) { print $target $_; } else { push @holdem, $_; my $decision = look_for_evidence( $_ ); if ( $decision =~ /one|two/ ) { open( $target, "|-", @{$dispatch{$decision}} ) or die "failed to launch: $dispatch{$decision}: $!\n"; print $target $_ for ( @holdem ); } } }
Your separate parser script uses the command line option to control how to set up event handlers for parsing.

(updated snippet to fix stupid mistakes, like forgetting to use a list for "command", "option" when opening the pipe to the parser script.)


In reply to Re^3: Can I seek in a command's piped output? by graff
in thread Can I seek in a command's piped output? by wardy3

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.