in reply to Problem of scope : $SIG{__WARN__} in a module...

Well, I am not sure why you would want to do that. If you are trying to capture DBI warnings/errors, you can give the DBI handle an error handler:

$h->{RaiseError} = 1; # Turn all warnings into dies $h->{HandleError} = sub { set_dberror(join('',@_)) };

Any errors generated by the dbh will be passed to your sub.

If you really want to capture capture all warning for specific modules, you might try:

package TrapWarnings; my %Modules; BEGIN { $SIG{__WARN__} = \ &trapWarning; } sub import { $Modules{caller} = 1; } sub trapWarning { my $caller = caller 1; if ($Module{$caller}) { set_dberror(join('',@_)) } else { warn @_ } } # Example use package Foo; use TrapWarnings; warn "foo";

Well that code is completely untested. The general idea is everytime you import your TrapWarnings package, it adds that package to the list of packages to trap warnings for. When a warn happens, it checks the original caller to see if its package is in the list. If so, it traps it, otherwise, just re-warns it.

This is far from ideal, but with creative use of the Carp module you might get where you want to be.

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

Replies are listed 'Best First'.
Re^2: Problem of scope : $SIG{__WARN__} in a module...
by wazoox (Prior) on Dec 03, 2004 at 18:11 UTC
    you can give the DBI handle an error handler:

    $h->{RaiseError} = 1; # Turn all warnings into dies
    $h->{HandleError} = sub { set_dberror(join('',@_)) };

    Any errors generated by the dbh will be passed to your sub.
    Errr... I don't really get it, how is this code supposed to work? won't it pass any warning to the sub, not only the dbh warnings ?
      Nope, this code will pass only DBH warnings to the set_dberror sub. You do it when when you initialize your DBH handle. Although, I don't think it handles connnection errors.

      Ted Young

      ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

        Ok, I got it now. It's OK for me, it'll do the trick. I just want my script to report neatly all errors when exiting, I don't mind seeing all sort of warnings mixing up with the stdout...

        Thanks all :)