Is there a direct way to redirect what would go to STDOUT and STDERR to variables
Yes, you can open STDOUT/STDERR to $variables. For example
my $script_content = 'print STDERR "foo"'; open(SAVEERR, ">&STDERR"); close(STDERR); open(STDERR, '>', \my $buf) or die $!; eval $script_content; print "buf = $buf\n"; # "buf = foo"
But note that this technique does not capture what external programs further down the line might write to stderr (just in case that's what you're having in mind).
I.e., if $script_content was something like 'system "cat does-not-exist"', $buf would not hold cat's error message "cat: does-not-exist: No such file or directory". Reason is that file handles opened to scalar variables are Perl-internal (indicated by fileno returning -1), not accessible from processes outside of the Perl program.
In reply to Re: Reassign STDOUT/STDERR contents to a variable
by almut
in thread Reassign STDOUT/STDERR contents to a variable
by lothos
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |