in reply to how do i redirect STDOUT, STDIN, or STDERR to a FILE?
package STDERR_REDIRECTOR; use strict; use Fcntl qw(:seek); my $ORIGSTDERR; my $CATCHERR; sub capture{ # save STDERR filehandle open($ORIGSTDERR, ">&", STDERR); # create temporary filehandle to catch STDERR open($CATCHERR, "+>", undef) or die("cannot open temp file :: $!") +; open(STDERR, ">&", $CATCHERR); # redirection } sub release{ # restore STDERR filehandle open(STDERR, ">&", $ORIGSTDERR); } sub showcapture{ my $prefix = shift || ">"; my $arrayref = shift; # read captured STDERR from temporary filehandle seek($CATCHERR, 0, SEEK_SET); while (<$CATCHERR>){ $arrayref and push (@$arrayref,$_),next; print "$prefix $_" ; } close($CATCHERR); } 1;
STDERR_REDIRECTOR::capture(); # Do stuff that writes to STDERR|; STDERR_REDIRECTOR::release(); STDERR_REDIRECTOR::showcapture();
"Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
-Friedrich Nietzsche: A Dynamic Translation
|
|---|