Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Perl die if a warning is generated?

Tags:

warnings

die

perl

I would like my script perl to die whenever a warning is generated, including warnings which are generated by used packages.

For example, this should die:

use strict;
use warnings;
use Statistics::Descriptive;

my @data = ( 8, 9, 10, "bbb" );
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@data);

use warnings FATAL => 'all'; won't help since it's lexically scoped. Test::NoWarnings also doesn't do the work since it doesn't kill the script.

like image 683
David B Avatar asked Oct 09 '10 15:10

David B


1 Answers

I believe you're looking for $SIG{__WARN__} as documented in perlvar. Something similar to

$SIG{__WARN__} = sub { die @_ };

might be what you want.

like image 101
rafl Avatar answered Sep 17 '22 12:09

rafl