in reply to Close STDOUT/STDERR temporarily

these are just file handles, you can save the old values° close them and open them temporarily to another file or a var ref

See select , open , close

Especially the docs for open have explicit sample code for exactly this task.

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

°) called "duplicate"

Replies are listed 'Best First'.
Re^2: Close STDOUT/STDERR temporarily
by kennethk (Abbot) on Sep 10, 2014 at 16:55 UTC
    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.

      Ok, thanks for the replies.
Re^2: Close STDOUT/STDERR temporarily
by locked_user sundialsvc4 (Abbot) on Sep 12, 2014 at 01:20 UTC

    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 ☆☆☆☆ :)