Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Close STDOUT/STDERR temporarily

by Superfox il Volpone (Sexton)
on Sep 10, 2014 at 15:45 UTC ( [id://1100171]=perlquestion: print w/replies, xml ) Need Help??

Superfox il Volpone has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I would like to close temporarily STDOUT and STDERR so that an invoked function cannot write on it. For instance something like:
my $fh = *STDOUT; *STDOUT = undef; foo(); # foo cannot print into the stdout STDOUT = $fh; # restore STDOUT
Is there any way to do that?

Thanks,
S.Fox

Replies are listed 'Best First'.
Re: Close STDOUT/STDERR temporarily
by LanX (Saint) on Sep 10, 2014 at 16:08 UTC
    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"

      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.

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

Re: Close STDOUT/STDERR temporarily
by stylechief (Sexton) on Sep 11, 2014 at 16:03 UTC

    Some example code snippets that work...

    # preserve STDOUT open $oldSTDOUT, ">&STDOUT" || warn "Can't preserve STDOUT\n$!\n"; + close STDOUT; # preserve STDERR open (OLDER, ">&", \*STDERR) || warn "Can't preserve STDERR\n$!\n" +; close STDERR; # if you want to capture any messages in the meantime, point STDER +R to a memory location open (STDERR, ">", \$err) || warn "Can't redirect STDERR\n$!\n"; # restore STDERR open (STDERR, ">&", \*OLDER) || warn "Can't restore STDERR\n$!\n"; close OLDER; # restore STDOUT open STDOUT, ">&", $oldSTDOUT; # you may want to flush buffers at some point select STDERR; $| = 1; select STDOUT; $| = 1;
    SC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100171]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-09-08 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.