I must be understanding the warnings documentation wrong. The way I read it, this code:
use warnings;
use warnings FATAL => 'all';
warnings::warn('numeric', 'blarg');
print "finished\n";
Should print the 'blarg' warning and die since I've asked for all warnings to be fatal. However, when I run the code I get:
$> /opt/local/bin/perl x.pl
blarg at x.pl line 3
finished
Can somone help me understand why I can't get warn to die?
Best Answer
Okay. This is ugly. I had a post half-prepared explaining this as a bug in
warnings
, and then I realized it's not, it's just a really evil subtlety in the waywarnings
works.Warnings starts looking for a relevant stack frame to get the warning bits from in
warnings::warn
's caller's caller. The idea is that you're writing some module and you usewarnings::warn
orwarnings::warnif
in your functions, and whether or not the warning is printed (or fatal) depends on theuse warnings
setting in scope in the code that uses your module. There's no option provided to have it start atcaller(1)
instead ofcaller(2)
, so the effect you want isn't possible.An example of code that does work (and demonstrates how this interface was expected to be used by whoever wrote it):
And you can't defeat the way it works by just adding another level of subroutines, because it takes the flags from the first caller that's in a different package from the caller of
warn
/warnif
/enable
/etc.