Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: finding Un-Initialised variables from the perl code

by Corion (Patriarch)
on Nov 03, 2005 at 12:10 UTC ( #505301=note: print w/replies, xml ) Need Help??


in reply to finding Un-Initialised variables from the perl code

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://505301]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-29 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?