How do you get Perl to stop and give a stack trace when you reference an undef value, rather than merely warning? It seems that use strict; isn't sufficient for this purpose.
-
Include this:
use Carp ();Then include one of these lines at the top of your source file:
local $SIG{__WARN__} = \&Carp::confess; local $SIG{__WARN__} = \&Carp::cluck;The
confessline will give a stack trace, and thecluckline is much more terse.From Neil -
use warnings FATAL => 'uninitialized'; use Carp (); $SIG{__DIE__} = \&Carp::confess;The first line makes the warning fatal. The next two cause a stack trace when your program dies.
From cjm -
Referencing an undef value wouldn't be a problem in itself, but it may cause warnings if your code is expecting it to be something other than undef. (particularly if you're trying to use that variable as an object reference). You could put something in your code such as:
use Carp qw(); [....] Carp::confess '$variableName is undef' unless defined $variableName; [....]From mopoke -
One way to make those warnings fatal is to install a signal handler for the WARN virtual-signal:
$SIG{__WARN__} = sub { die "Undef value: @_" if $_[0] =~ /undefined/ };From zigdon -
Instead of the messy fiddling with
%SIGproposed by everyone else, justuse Carp::Alwaysand be done.Note that you can inject modules into a script without source modifications simply by running it with
perl -MCarp::Always; furthermore, you can set thePERL5OPTenvironment variable to-MCarp::Alwaysto have it loaded without even changing the invocation of the script. (Seeperldoc perlrun.)From Aristotle Pagaltzis
0 comments:
Post a Comment