How to conditionally make warnings fatal?

I want to have fatal errors that can be reduced to warnings (or, conversely, warnings that can be extended to fatal errors) depending on the user's preferences. I am currently using dieand overriding it as follows:

my $force;
BEGIN {
    no warnings 'once';
    Getopt::Long::Configure('pass_through');
    GetOptions('force', \$force);
    *CORE::GLOBAL::die = sub { warn @_ } if $force;
    Getopt::Long::Configure('no_pass_through');
}

use My::Module;
...
die "maybe";
...
exit(1) if $force;
exit(0); # success!

I do not like this approach, mainly because there are several places where I still want to die (for example, missing command line arguments). I would rather change the (most) instances dieto warnand make a conditional use warnings FATAL => 'all' 1 . The problem is that it is use warningslexically limited, so it is inefficient:

if (!$force) {
    use warnings FATAL => 'all';
}

Trying to use a postfix condition is a syntax error:

use warnings FATAL => 'all' unless $force;

use :

$force or use warnings FATAL => 'all';  # syntax error

( /, script if/else ..), , use warnings . ( , , , .)


1: , use warnings::register use warnings FATAL => 'My::Module' script, .
+3
1

:

use if !$force, warnings => qw( FATAL all );

, if, Perl, .

, , $force . (, BEGIN { ... }.) , , , , .

, :

BEGIN {
    require warnings;
    warnings->import(qw/ FATAL all /) unless $force;
}

$SIG{__WARN__} $SIG{__DIE__}, , .

+4

All Articles