close STDOUT; open STDOUT, '>', \$variable or die "Can't open STDOUT: $!";
You could combine that with local *STDOUT to limit that capturing to a scope:
#!/usr/bin/perl use warnings; use strict; sub writes_to_stdout { print "some information\n"; } sub captured { local *STDOUT; my $result; open STDOUT, '>', \$result; writes_to_stdout(); return $result; } my $val = captured(); print "No output yet\n"; print "But now: $val"; ## and this is what it writes: $ perl foo.pl No output yet But now: some information
Update: added complete example
In reply to Re: n a perl sub, how do I capture STDOUT, STDERR that comes from another perl sub?
by moritz
in thread In a perl sub, how do I capture STDOUT, STDERR that comes from another perl sub?
by seank
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |