in reply to Re: Re: declaration of variables
in thread declaration of variables

I think this is a perfectly valid place to undefine a variable. You could also use my to redeclare the variable, but there is no need to declare the variable both inside the loop and outside:
my @final; my @array = qw(foo bar baz); for (0..$#array) { my $temp; for my $j (0..$#array) { $temp .= $array[$j]; } push @final, $temp; }
Remember, undef is not bad, you just have to know when it is a good choice:
my $temp; for (0..$#array) { for my $j (0..$#array) { $temp .= $array[$j]; } push @final, $temp; undef $temp; }
Here, it is perfectly fine because we are undefining a scalar. But, what happens if we push another array instead?
for my $i (0..$#array) { my @temp = @array; push @final, \@temp; undef @temp; }
This is a bad choice. Now @final contains three anonymous, empty array references. What happened? We copied the contents of @array, but after we pushed our temp array, we undefined it - hence, we 'erase' the data we just pushed. But, had we pushed a new anonymous array reference that contained our temp array, then it works:
for my $i (0..$#array) { my @temp = @array; push @final, [@temp]; undef @temp; }

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: 3Re: declaration of variables
by bradcathey (Prior) on Sep 02, 2003 at 15:47 UTC
    Wow, that's quite a thorough answer. I'm glad to get a practical explanation of undef.

    Blessings, bradcathey