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


Hello wisiest ones! for first time in my 'career' I considering to modify $SIG{__WARN__}.

Until now I have considered this like something of Magic an Power never disclosed to other then Perl fellows.. so I never played with it in a reverencial ignorance (I suspect also that a BAD or Buggy signal handler is dangerous like a mad dog).

Now, when the ChatterBox is empty like my bank-account, I'm write a little module in which I wont to use Carp's cluck and confess facility and I want to log succes and failure so my first approach in the caller script was:
use MyLittlePlace::BrandNewMod qw(a b c); close STDERR; open(STDERR, ">&STDOUT") || die " uh uh ?! we mad ?! "; $|++;
so that I can log to a file all the return output with useful information and in the right order..

Then the 'UcanDObetter' devil told me 'you dont want a dayly spam mail full of OK report, true? invent some kind of trigger that fire an advise on more then zero errors..'.

Mmh.. I have SuperSearched this and I replaced the above code in this way in the caller script:
close STDERR; # no more redirect of handler # open(STDERR, ">&STDOUT") || die " uh uh ?! we mad ?! "; $|++; my $err_count = 0; $SIG{__WARN__} = sub { ++$err_count;print STDOUT @_ }; $SIG{__DIE__} = sub { ++$err_count;print STDOUT @_ }; #do all the stuff and at the end.. &advise_me($content) if $err_count > =; print $err_count, " ERROR",($err_count eq 1 ? '' : 'S' ), " IN $0\n";
Is a Rigth thing to do in my case? is better do it in the module in some way ? it is dangerous? it make sense?

thks in advance

Lor*

Replies are listed 'Best First'.
Re: doubious about redefine $SIG{__WARN__}
by samtregar (Abbot) on May 12, 2008 at 17:09 UTC
    The __WARN__ handler looks ok to me. It's pretty harmless to redirect warnings to STDOUT and count them.

    The __DIE__ handler is incorrect since you're not re-throwing the exception and you're not examining $^S to determine if the die() is happening in an eval block. Some modules use die() as an internal exception mechanism and you won't want to treat those non-fatal deaths as errors.

    -sam

      thnk sam, i'm investigating on this $^S now..
      UPDATE: I've asked also node_id=686241