Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect STDOUT and STDERR to a variable

I want to redirect STDERR and STDOUT to a variable. I did this.

close(STDOUT);
close(STDERR);

my $out;
open(STDOUT, ">>", \$out);
open(STDERR, ">>", \$out);

for(1..10)
{
    print "print\n"; # this is ok. 
    warn "warn\n"; # same
    system("make"); # this is lost. neither in screen nor in variable.
}

The problem with system. I want the output of this call to be captured too.

like image 639
Deck Avatar asked Dec 11 '10 06:12

Deck


People also ask

What is the meaning of 2 >& 1?

&1 is used to reference the value of the file descriptor 1 (stdout). Now to the point 2>&1 means “Redirect the stderr to the same place we are redirecting the stdout” Now you can do this.

How would you redirect a command stderr to stdout?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


1 Answers

use Capture::Tiny!

like image 130
salva Avatar answered Sep 21 '22 14:09

salva