In particular, from open:
Here is a script that saves, redirects, and restores STDOUT and STDERR using various methods:
#!/usr/bin/perl
open(my $oldout, ">&STDOUT") or die "Can't dup STDOUT: $!";
open(OLDERR, ">&", \*STDERR) or die "Can't dup STDERR: $!";
open(STDOUT, '>', "foo.out") or die "Can't redirect STDOUT: $!";
open(STDERR, ">&STDOUT") or die "Can't dup STDOUT: $!";
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
open(STDOUT, ">&", $oldout) or die "Can't dup \$oldout: $!";
open(STDERR, ">&OLDERR") or die "Can't dup OLDERR: $!";
print STDOUT "stdout 2\n";
print STDERR "stderr 2\n";
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
| [reply] [d/l] |
Ok, thanks for the replies.
| [reply] |
Rolf, are you saying that the OP’s strategy would work? I presume that you are referring to the paragraph in perldoc -f open which begins “Here is a script that saves, redirects, and restores "STDOUT" and "STDERR" using various methods:” but I do get pretty-lost pretty-fast when reading it. Could you perhaps offer a couple of specific examples that do, specifically, what the OP has in mind to do? Or, otherwise, elaborate on how you’d do it?
| |
There are plenty of specific and working examples in this thread!
The confusion stems from demonstrating more than one way to do it within the same sample script (like 3 vs 2 argument open AND lexical filehandles vs bare words AND addressing filehandles as globrefs)
I have to admit that Perldocs are overly confusing in this respect.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] |