in reply to Re: Using 'no warnings' to disable own debug warn(ing)s?
in thread Using 'no warnings' to disable own debug warn(ing)s?
Well, my thinking was that since I've used warnings::register in my modules and warnings::warnif to create custom warnings, and that function is sensitive to FATAL warnings, I thought it'd be sensitive to no warnings as well, since AFAIK warn just unconditionally warns (and AFAIK the only way to catch it is with %SIG). And it does indeed work, but unfortunately it seems that it only works across package boundaries, I'm not entirely sure yet why, but from a quick look at the source it seems to be because it uses (parts of) Carp under the hood, looking up the call stack.
use warnings; use strict; { package Foo; use warnings::register; sub bar { warnings::warnif("bar: $_[0]"); } } { no warnings; Foo::bar("inside"); } Foo::bar("outside"); use warnings::register; use Carp; { no warnings; warn "warn"; warnings::warn("warnings::warn"); carp "carp"; warnings::warnif("inside 2"); } warnings::warnif("outside 2"); __END__ bar: outside at w.pl line 17. warn at w.pl line 23. warnings::warn at w.pl line 24. carp at w.pl line 25.
So at the moment my only two suggestions are to either go across package boundaries like I did in the above, or write your own debug function, which I tend to do. Just one example from here:
sub _debug { my ($self, $lvl, @out) = @_; return if $self->{debug}<$lvl || !@out; print STDERR __PACKAGE__, " DEBUG: ", @out, "\n"; } # ... $self->_debug(2, ...);
|
|---|