# Here is a script that saves, redirects, and restores "S
+TDOUT"
# and "STDERR" using various methods:
#!/usr/bin/perl
open my $oldout, ">&STDOUT" or die "Can't dup S
+TDOUT: $!";
open OLDERR, ">&", \*STDERR or die "Can't dup S
+TDERR: $!";
open STDOUT, '>', "foo.out" or die "Can't redirect
+STDOUT: $!";
open STDERR, ">&STDOUT" or die "Can't dup STDOU
+T: $!";
select STDERR; $| = 1; # make unbuffered
select STDOUT; $| = 1; # make unbuffered
print STDOUT "stdout 1\n"; # this works for
print STDERR "stderr 1\n"; # subprocesses too
close STDOUT;
close STDERR;
open STDOUT, ">&", $oldout or die "Can't dup \$oldo
+ut: $!";
open STDERR, ">&OLDERR" or die "Can't dup OLDERR
+: $!";
print STDOUT "stdout 2\n";
print STDERR "stderr 2\n";
regards,
tomte
Hlade's Law:
If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.
|