Reputation: 13685
I don't see how to make perl fail upon use of uninitialized-value. Is there a way to make this the default behavior? Thanks.
https://perlmaven.com/use-of-uninitialized-value
Upvotes: 1
Views: 508
Reputation: 222482
Dave's answer sure is the best pick for the described use case.
Here is another solution, that demonstrates the use of the warning signal handler (see http://perldoc.perl.org/functions/warn.html).
The benefit of using a signal handler is flexibility : you can trap any kind of warning, analyze it and then implement any behavior you like. In the given use case this is an overkill, it but can be useful in more complex cases.
use strict;
use warnings;
use feature "say";
local $SIG{__WARN__} = sub {
if ($_[0] =~ /^Use of uninitialized value/) {
die $_[0];
} else {
warn $_[0] ;
}
};
my $foo;
say "Foo is $foo";
say "Dont get here";
Upvotes: 1
Reputation: 69264
Something like this perhaps:
#!/usr/bin/perl
use strict;
use warnings;
use warnings FATAL => qw[uninitialized];
use feature 'say';
my $foo;
say "Foo is $foo";
say "Don't get here";
Comment out the FATAL
line to see the standard behaviour.
Upvotes: 3