mikosullivan has asked for the wisdom of the Perl Monks concerning the following question:
This isn't your usual "How to I redirect STDERR?" question. I have a specific set of requirements that haven't been met by any of the techniques I can find. Those requirements are as follows:
sub get_stderr {
my ($capturer);
$capturer = CAPTURE::Class->new();
return $capturer;
}
# in some other file:
my $capturer = get_stderr();
# Run commands that might output to STDERR.
print STDERR "STDERR in perl\n";
system 'ls file-that-does-not-exist.txt';
# later...
undef $capturer;
print STDERR "stuff that doesn't go to capturer\n";
The closest I've been able to come is to capture STDERR, including stuff
from external commands, like this:
#!/usr/bin/perl -w use strict; # redirect STDERR to a log file open (STDERR, '>', './log.txt') or die "could not open STDERR: $!\n"; # print to STDERR in Perl print STDERR "STDERR in perl\n"; # run an external command that outputs something to STDERR system 'ls file-that-does-not-exist.txt';That's nice as far as it goes, but it doesn't quite meet my needs. First, STDERR is only sent to a file. I can't figure out how to send it do a tied object. I have found ways to send STDERR to a tied object, but those techniques don't capture STDERR from external commands. Second, I can't figure out how to restore STDERR to its previous state once I'm done with the tied handle.
Much thanks, and looking forward to your help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Capturing STDERR (this is not a FAQ)
by Tux (Canon) on Feb 10, 2015 at 07:15 UTC | |
|
Re: Capturing STDERR (this is not a FAQ)
by ikegami (Patriarch) on Feb 10, 2015 at 05:39 UTC | |
by mikosullivan (Novice) on Feb 10, 2015 at 06:52 UTC | |
by BrowserUk (Patriarch) on Feb 10, 2015 at 07:16 UTC | |
by BillKSmith (Monsignor) on Feb 10, 2015 at 13:59 UTC | |
by ikegami (Patriarch) on Feb 10, 2015 at 14:58 UTC | |
by ikegami (Patriarch) on Feb 10, 2015 at 14:57 UTC | |
| |
|
Re: Capturing STDERR (this is not a FAQ)
by Anonymous Monk on Feb 10, 2015 at 06:52 UTC |