Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

finding Un-Initialised variables from the perl code

by jesuashok (Curate)
on Nov 03, 2005 at 12:01 UTC ( #505297=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Hi monks.

#!/usr/bin/perl use strict; use diagnostics; my $i;
In the above code even after including diagnostics module it doesn't throw any error regarding the un-initialised variable $i. Is there any module available to check un-initialised. This will be useful for me to remove the un-initialised variables from the code.

diagnostics throws the error only when "$i" is used inside the code without initialising.

"Keep pouring your ideas"

Comment on finding Un-Initialised variables from the perl code
Download Code
Re: finding Un-Initialised variables from the perl code
by Corion (Pope) on Nov 03, 2005 at 12:10 UTC

    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

Re: finding Un-Initialised variables from the perl code
by pboin (Deacon) on Nov 03, 2005 at 12:50 UTC

    I'm not sure how to say this without sounding sarcastic, etc. but I can't imagine a reason to put your valuable time into tracking this down. I mean sure, it would be nice, but there's no material downside to a few extra declarations. If you really want to optimize, there's *gotta* be something better than this. (Try taking your hot subroutines and coding them in C if you're bored or something.)

      hi pboin,

      actually Java Handles this. Since I love perl I am eager to know about the module that handles this. Thanks for your time :-)

      "Keep pouring your ideas"
Re: finding Un-Initialised variables from the perl code
by Perl Mouse (Chaplain) on Nov 03, 2005 at 13:55 UTC
    The warning is misleading or at least, confusing. It doesn't mean variables are un-initialised - it means that values are undefined. And while each un-initialized variable contains an undefined value, the reverse isn't true.
    my $foo = undef; # Variable is initialized. print $foo; __END__ Use of uninitialized value in print
    Your request is also unfeasable. Sure, it theory Perl could check that whenever you use my it's on the LHS of an assignment. Beside that it would flag code that has run without warnings for many years, it also won't do you any good. For instance, while:
    my @foo = qw /bar baz quux/;
    initialized $foo[0], $foo[1] and $foo[2], it still means that $foo[3], $foo[4], $foo[5], ..., etc are uninitialized.
    Perl --((8:>*
Re: finding Un-Initialised variables from the perl code
by rinceWind (Monsignor) on Nov 03, 2005 at 13:57 UTC

    You might want to take a look at B::Xref in the core.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://505297]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-12-27 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls