in reply to Identify Unused and Uninitialised variables.
I guess uninitialised variables only can be detected at run-time, but I can not think of an immediate solution apart from rigorous testing.$ cat foo.pl use strict; use warnings; my ($unused, $i, $j); $i = $j; # uninitialized __END__ $ perlcritic -3 foo.pl .. "$unused" is declared but not used. at line 4, column 1. Unused varia +bles clutter code and make it harder to read. (Severity: 3) ..
(Output from perlcritic was edited for clarity).
Update:
print and some math functions will warn if it detects uninitialised variables (assuming warnings is enabled):
However, you may not always want to print or do calculations on your variables.$ perl -wle 'my $i; print $i' Use of uninitialized value in print at -e line 1. $ perl -wle 'my $i; $i += $i' Use of uninitialized value in addition (+) at -e line 1. $ perl -wle 'my $i; if ($i>0) {}' Use of uninitialized value in numeric gt (>) at -e line 1.
The warnings are described in perldiag:
Use of uninitialized value%s(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into ""that " . $foo", and the warning will refer to the "concatenation (.)" operator, even though there is no "." in your program.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Identify Unused and Uninitialised variables. (perlcritic)
by toolic (Bishop) on Jan 14, 2009 at 18:44 UTC | |
by andreas1234567 (Vicar) on Jan 14, 2009 at 19:58 UTC | |
by toolic (Bishop) on Jan 14, 2009 at 20:41 UTC | |
by andreas1234567 (Vicar) on Jan 14, 2009 at 21:13 UTC |