How to redirect STDOUT and STDERR to a variable

Deck picture Deck · Dec 11, 2010 · Viewed 16.7k times · Source

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.

Answer

salva picture salva · Dec 11, 2010