Punto has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I have this script that get a lot of data from STDIN. I need to get some data from the first 3 or 4 lines of the input (I don't know exactly how many) and then send everithing (including the first 3 or 4 lines) to a different object (mime::parse). I can't put all STDIN on an array, because the script may run out of memory, so, is there any way to "restart" reading from STDIN? Once I get the stuff from <STDN>, is there a way to "put it back in"?

Thanks..

Replies are listed 'Best First'.
Re: re-start reading from stdin?
by lhoward (Vicar) on May 27, 2000 at 02:25 UTC
    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.