in reply to Identify Unused and Uninitialised variables.

Perl::Critic can help you find unused variables:
$ 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) ..
I guess uninitialised variables only can be detected at run-time, but I can not think of an immediate solution apart from rigorous testing.

(Output from perlcritic was edited for clarity).

Update:

print and some math functions will warn if it detects uninitialised variables (assuming warnings is enabled):

$ 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.
However, you may not always want to print or do calculations on your variables.

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.

--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Replies are listed 'Best First'.
Re^2: Identify Unused and Uninitialised variables. (perlcritic)
by toolic (Bishop) on Jan 14, 2009 at 18:44 UTC
    I can not reproduce your perlcritic output. Here is what I get (running on v5.8.8 built for x86_64-linux):
    $ cat foo.pl use strict; use warnings; my ($unused, $i, $j); $i = $j; # uninitialized __END__ $ perlcritic -3 foo.pl Code not contained in explicit package at line 1, column 1. Violates +encapsulation. (Severity: 4) Module does not end with "1;" at line 5, column 1. Must end with a re +cognizable true value. (Severity: 4) $ perlcritic -Version 1.080 $

    What version of perlcritic do you have? Perhaps mine is too old (1 year old), and I need to upgrade.

    Do you have a .perlcritic file? Maybe that has some tool configuration settings that I need to use. Now that you have shown me this cool capability, I really want to use it. Any help would be appreciated.

      I'm using
      $ perlcritic --version 1.090
      Do you see the warning if you use perlcritic -2 instead?

      Update the module, you are most likely 6 months behind. See the Changes file:

      1.088 Released on 2008-07-04

      * Due to the consensus at YAPC::NA 2008, Variables::ProhibitUnusedVariables default severity has been raised to medium/3.

      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
        Do you see the warning if you use perlcritic -2 instead?
        No. I tried this with all severity settings (1 .. 5).

        Thanks for giving me something to look for in the Changes file. Chances are this issue will be fixed if I upgrade. It's easy to get out of date with this module: 10 updates in the last year (10!).