Perl2.4.2 How to Rewind the STDIN? |
30.08.2008 |
| ← 2.4.1 File Access | [ up ] - [ a - z ] - [ search PC ] - [ top ] | 2.5 Databases → |
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; }
| ← 2.4.1 File Access | [ up ] - [ top ] | 2.5 Databases → |
[ home ] - [ search ] - [ feedback ]
copyright by reto - created with mytexi