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]

In reply to Re: Identify Unused and Uninitialised variables. by andreas1234567
in thread Identify Unused and Uninitialised variables. by perlpal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.