***** infoCopter.com/perl *****
Re-open the STDIN file handleHow to rewind the STDIN file handle?In special cases it can be needed to re-read from the STDIN file handle, e.g. in a mail or Listserver processing with Perl. There might be even more sophisticated solutions on this problem but the following one will work. If you had a better recipe please let me know. How to test this example:ls -l /tmp | script.pl Recipe#!/usr/bin/perl -w
use strict;
$| = 1;
local *FH;
*FH = &stdin(); # Now you have a rewind-able file handle to your STDIN data
# -- first run
while(<FH>) {
print "[R1]=> $_";
}
seek(FH, 0, 0);
# -- second run
while(<FH>) {
print "[R2]=> $_";
}
sub stdin () {
my $tempfile = time() . '_' . int(rand(1000000));
open(TEMP, "+>/tmp/temp_$tempfile") or print STDERR $!;
local $/ = undef;
print TEMP <STDIN>;
seek(TEMP, 0, 0);
\*TEMP;
}
|