Have you considered using the seek function.
This test shows reading the first 3 lines off of
STDIN, then seeking back to the beginning of STDIN
and then iterating through the handle normally.
my $c=0;
while($c<3){
my $l=<STDIN>;
print "## $c ## $l";
$c++;
}
seek(STDIN,0,0);
while(<STDIN>){
print ".... $_";
}
There is probably some annoying OS trickery that
would cause this "seek on STDIN" technique
to fail on certain OSes and under
certain conditions, but it tested properly where I wrote it.
I hope you are as lucky.
Another idea would be build a "rewindable filehandle" with
either a tied filehandle or a subclass of the
IO::File
module that would hold a "back buffer" and have a
rewind function.
It's too late on a friday for me to come up with any
more details
of exactly how this would be done, but I think
it is doable.
Of course, this would only be necessary if the
seek method doesn't
work for you.
There is always the less-elegant solution of dumping all
of STDIN
out to a temp file, opening the temp file, grabbing the first
n lines for your pre-processing,
closing/reopening (or seeking back to BOF)
the tempfile, and passing that handle to MIME::Parser.
This is easier to code than the advanced approaches
I mentioned above, but
will really increase disk IO and just feels icky. |