<p>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 <code>use strict;</code> isn't sufficient for this purpose.</p>

[{'answer_id': 77969, 'author': 'Neil', 'author_id': 14193, 'author_profile': 'https://Stackoverflow.com/users/14193', 'pm_score': 2, 'selected': False, 'text': '<p>Include this:</p>\n\n<pre><code>use Carp ();\n</code></pre>\n\n<p>Then include <em>one</em> of these lines at the top of your source file:</p>\n\n<pre><code>local $SIG{__WARN__} = \\&amp;Carp::confess;\nlocal $SIG{__WARN__} = \\&amp;Carp::cluck;\n</code></pre>\n\n<p>The <code>confess</code> line will give a stack trace, and the <code>cluck</code> line is much more terse.</p>\n'}, {'answer_id': 77971, 'author': 'cjm', 'author_id': 8355, 'author_profile': 'https://Stackoverflow.com/users/8355', 'pm_score': 5, 'selected': True, 'text': "<pre><code>use warnings FATAL =&gt; 'uninitialized';\n\nuse Carp ();\n$SIG{__DIE__} = \\&amp;Carp::confess;\n</code></pre>\n<p>The first line makes the warning fatal.  The next two cause a stack trace when your program dies.</p>\n<p>See also <code>man 3pm warnings</code> for more details.</p>\n"}, {'answer_id': 77972, 'author': 'mopoke', 'author_id': 14054, 'author_profile': 'https://Stackoverflow.com/users/14054', 'pm_score': 1, 'selected': False, 'text': "<p>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).\nYou could put something in your code such as:</p>\n\n<pre><code>use Carp qw();\n\n[....]\n\nCarp::confess '$variableName is undef' unless defined $variableName;\n\n[....]\n</code></pre>\n"}, {'answer_id': 77980, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 2, 'selected': False, 'text': '<p>One way to make those warnings fatal is to install a signal handler for the <strong>WARN</strong> virtual-signal:</p>\n\n<pre><code>$SIG{__WARN__} = sub { die "Undef value: @_" if $_[0] =~ /undefined/ };\n</code></pre>\n'}, {'answer_id': 78239, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 4, 'selected': False, 'text': '<p>Instead of the messy fiddling with <code>%SIG</code> proposed by everyone else, just <code>use <a href="http://search.cpan.org/perldoc?Carp::Always" rel="noreferrer">Carp::Always</a></code> and be done.</p>\n\n<p>Note that you can inject modules into a script without source modifications simply by running it with <code>perl&#160;-MCarp::Always</code>; furthermore, you can set the <code>PERL5OPT</code> environment variable to <code>-MCarp::Always</code> to have it loaded without even changing the invocation of the script. (See <a href="http://p3rl.org/run" rel="noreferrer"><code>perldoc perlrun</code></a>.)</p>\n'}, {'answer_id': 5873113, 'author': 'user736584', 'author_id': 736584, 'author_profile': 'https://Stackoverflow.com/users/736584', 'pm_score': 0, 'selected': False, 'text': '<p>You have to do this manually. The above "answers" do not work! Just test out this:</p>\n\n<pre><code>use strict;\nuse warnings FATAL =&gt; \'uninitialized\';\nuse Carp ();\n$SIG{__DIE__} = \\&amp;Carp::confess;\n\nmy $x = undef; # it would be enough to say my $x;\nif (!$x-&gt;{test}) {\nprint "no warnings, no errors\\n";\n}\n</code></pre>\n\n<p>You will see that dereferencing did not cause any error messages or warnings. I know of no way of causing Perl to automatically detecting the use of undef as an invalid reference. I suspect this is so by design, so that autovivification works seamlessly.</p>\n'}]
['https://Stackoverflow.com/questions/77954', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14193/']