con con - 2 months ago 10
Perl Question

perl unintialized value: how to make more meaningful warnings?

in Perl, I can see warnings like

Use of uninitialized value in concatenation (.) or string at stat_plots.pl line 446.


However, I would like to make changes to perl, or add a subroutine at the beginning of the script, so that this warning would show an error like

Hash key {index} is not defined at stat_plots.pl line 446.


This would save a lot of time in the debugging process, and make the code look a lot nicer without having to add dozens of

if (!defined $hash{$key}) {
print "\$hash{$key} isn't defined.\n";
}


and make each script much longer and messier.

Is there a way to get perl to do this?

Answer

You can't change Perl to do that. But the people who maintain Perl have already changed Perl to do that for you.

$ perl -Mwarnings -E'say $_."XX"'
Use of uninitialized value $_ in concatenation (.) or string at -e line 1.
XX

This change was released in Perl 5.10.0 (in December 2007).

New or Changed Diagnostics

Use of uninitialized value

Perl will now try to tell you the name of the variable (if any) that was undefined.

(From perl5100delta)

So the answer to your question is - upgrade your version of Perl.