Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2023-12-05 08:21 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (26 votes). Check out past polls.