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.


In reply to Re: re-start reading from stdin? by lhoward
in thread re-start reading from stdin? by Punto

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.