kgnickl has asked for the wisdom of the Perl Monks concerning the following question:

I wrote the below code trying to redirect all STDERR output to a function which can then handle it accordingly. When I try to print the error in the function it get redirected to its printing the error 2x's, specifically 1x on 2 separate lines..... Any ideas what I'm doing wrong or why this happens? I have ActivePerl and installed the IO-Stringy package.
use strict; use Tie::STDERR \&HandleError; print "Before\n"; die "....."; print "After\n"; sub HandleError(){ my ($error) = @_; &SomeActivity('0',$error); } sub SomeActivity(){ my ($rc, $error) = @_; print $rc . "\n" . $error; }


Before
0
..... at C:/ReqProAutomation/Frontend/ProjectSplit/redirecterror.pl line 5.
..... at C:/ReqProAutomation/Frontend/ProjectSplit/redirecterror.pl line 5.

Replies are listed 'Best First'.
Re: Double error when print w/ Tie::STDERR
by Khen1950fx (Canon) on May 11, 2011 at 21:48 UTC
    The documentation for Tie::STDERR is a little sparse, so I used tie_stderr.t. Try this:
    #!/usr/bin/perl use strict; use warnings; BEGIN { $|=1; } END { print "not ok\n" unless $::tiestderrloaded; } BEGIN { print "Loading Tie::STDERR\n"; } use Tie::STDERR \&HandleError; $::tiestderrloaded = 1; print "Before\n"; if ($@) { die "...."; } print "After\n"; use Tie::STDERR undef; sub HandleError { my ($error) = @_; &SomeActivity('0',$error); } sub SomeActivity { my ($rc, $error) = @_; print $rc . "\n" . $error; }