in reply to Identify Unused and Uninitialised variables.

What do you mean by uninitialised?

Are my $scalar; or my @array uninitialised? or are my $scalar =''; or my @array = () ?

And what about my @array = (); print $array[0];? Does every element in the array need to have some defined value to be considered initialised?

What about a hash that has keys but all values are undef? It would probably get even worse to solve if one thinks about objects or complicated structures of variables built through references.

A static analysis of the perl-code cannot find all cases of uninitialised variables.

And then, are uninitialised variables a "bad thing" per se? I doubt it.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Identify Unused and Uninitialised variables.
by wfsp (Abbot) on Jan 14, 2009 at 11:00 UTC
    I think the op may have been thinking of "useless variables" as mentioned in your sig. I often go back over my code and find that as it developed uncessary variables (even subs and objects) are left hanging around.

    One way of reducing this is to declare vars as close too where the vars are used as you can, and in the smallest scope possible. Even to the extent of creating a scope to put them in. When reviewing/refactoring you can see that the whole block (with its vars) can come out.

    my $html; { # $filename and $fh only used here # and they won't conflict with anything else my $filename = q{some/file.html}; open my $fh, q{<}, $filename or die qq{cant open $filename: $!\n}; $html = do{local $/;<$fh>}; } # any previous $filename and $fh remain unharmed
Re^2: Identify Unused and Uninitialised variables.
by perlpal (Scribe) on Jan 14, 2009 at 12:56 UTC
    To be more precise, i meant useless variables which are there in the script but bot being used. They may or maynot be initialized.