There is no way for Perl to find "uninitialized variables" before they are used. warnings.pm will give you a warning if you use a variable before initializing it.

Your problem seems to be not "uninitialized" but unused variables - from your description, I think you have code like this:

my ($foo,$bar,$baz,$i); for $i (0..10) { print "$i\n"; };

and you want to eliminate $foo,$bar,$baz from your code. The only way to do that is to remove them all and add only the necessary declarations back in.

There can be no automated way to do that which works every time because of the following program:

my $i; if (rand > 0.5) { $i = 1; };

You could create a special object which registers itself with a central authority and deregisters itself when it gets overwritten and at program end check if the object was not overwritten. That way you could find out all variables that have not been used throughout one run of the program. But it is likely much easier to do this manually.

Update: Roy Johnson spotted an embarassing typo - I forgot the parentheses around a my declaration


In reply to Re: finding Un-Initialised variables from the perl code by Corion
in thread finding Un-Initialised variables from the perl code by jesuashok

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.