#!/usr/bin/perl # Jaime Prilusky, 2008 # simple use of 'select' for redirecting STDOUT # this will save to an external file, when provided # or send the output to the screen my $f = $ARGV[0]; # optional filename to send the output print "Starting ...\n"; # this goes to the default STDOUT: screen if ($f) { # if we received a filename open(OUT,">$f"); # open it for writing on filehandle OUT $oldfh = select OUT; # store the current STDOUT in $oldfh # and set STDOUT to OUT } foreach $i (1 .. 10) { print "$i\n"; } # send something to STDOUT if ($f) { # if we received a filename close(OUT); # close the filehandle select $oldfh; # restore the old STDOUT } print "Done.\n"; # this goes to the default STDOUT: screen