http://qs1969.pair.com?node_id=505297

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"

Replies are listed 'Best First'.
Re: finding Un-Initialised variables from the perl code
by Corion (Patriarch) 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 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)

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 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.)

    A reply falls below the community's threshold of quality. You may see it by logging in.