[oak perl] until loop(s)
Steve Fink
sfink at reactrix.com
Mon Mar 28 13:49:11 PST 2005
David Alban wrote:
> If you have:
>
> my $counter;
> until ( $counter == 5 ) { ... }
>
> Then (at least) the first time $counter is tested by the until loop,
> you're asking perl to compare the number five to $counter, which is
> undefined because it never was assigned a value.
>
> If you first initialize $counter:
>
> my $counter = 0;
> until ( $counter == 5 ) { ... }
>
> then unless you undefine (or some operation undefines) $counter in the
> loop, you'll never be comparing a number to an undefined value.
>
> TMTOWTDI. I might have done your loop as:
>
> for my $counter ( 1..5 ) {
> ...
> }
>
> if $counter wasn't needed after the loop and:
>
> my $counter;
> for $counter ( 1..5 ) {
> ...
> }
>
> if $counter was needed after the loop.
But not if the __value__ is needed after the loop, because 'for' will
implicitly localize the value.
my $counter = 10;
for $counter (1..5) {
}
print $counter;
will print 10.
More information about the Oakland
mailing list