in reply to Re: Re: Re: Opening and Closing STDOUT
in thread Opening and Closing STDOUT
++hawtin , I think I shall need to cram on type_globs before I quite understand (/me attempts to de-dogear Perl-ina-Nutshell). Your code I think is correct, in syntax and function. This exhibits the right behaviour.
#!/usr/bin/perl -w local (*SAVED_STDOUT); open (SAVED_STDOUT, ">&STDOUT"); print "Roo"; close STDOUT; open (STDOUT , ">>foo.bar") || die "$!"; print "s"; close STDOUT; open(STDOUT, ">&SAVED_STDOUT"); print "ters\n
And of course the key here is RTFM (TOASTER!@) because perldoc -f open is insightful enough to provide....
sweet.Here is a script that saves, redirects, and restores STDOUT and STDERR: #!/usr/bin/perl open(OLDOUT, ">&STDOUT"); open(OLDERR, ">&STDERR"); open(STDOUT, '>', "foo.out") || die "Can't redirect stdout"; open(STDERR, ">&STDOUT") || 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 close(STDOUT); close(STDERR); open(STDOUT, ">&OLDOUT"); open(STDERR, ">&OLDERR"); print STDOUT "stdout 2\n"; print STDERR "stderr 2\n";
|
|---|