in reply to How to capture STDOUT/STDERR of a function
STDERR and STDOUT can be localized, like any other global file handle, . That done you can call your function and output will go to the new handle. In perl 5.8,
That takes 5.8+ because of the PerlIO trick of opening a file to memory by using a reference to a scalar in the filename slot. In pre-5.8 perls, you can use IO::Scalar or IO::Stringy for the same purpose.my ($errors, $output); sub foo { print STDERR "Something to worry about\n"; } { open local(*STDERR), '>', \$errors; foo(); } warn "STDERR ought to be restored now"; $errors =~ s/Something/Nothing/; print $errors;
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to capture STDOUT/STDERR of a function
by Anonymous Monk on Aug 31, 2004 at 18:07 UTC | |
by Zaxo (Archbishop) on Aug 31, 2004 at 18:13 UTC | |
by johnnywang (Priest) on Aug 31, 2004 at 18:09 UTC |