[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] That was productive...



On Tue, Jan 18, 2000 at 12:35:25PM -0500, John Porter wrote:
> * Jeff Pinyan (jeffp@crusoe.net) [000118 11:54]:
> >
> > Sometimes, I use something like:
> >
> >   for ($i = 0; local $_ = $array[$i], $i < @array; $i++) {
> >     ...
> >   }
>
> I'm not sure, but I think that may trigger the array
> extension mechanism, since sometimes you're accessing
> $array[ $#array + 1 ].

Taking a reference to an unallocated element of an array will extend the
array.  So, be careful creating an alias, as in C<local *_ = \$array[$i]>,
which would give you an infinite loop in the above code.

Simply using an unallocated element as an rvalue will not affect the
contents of the array, however.

Ronald


#!perl -w

use strict;

my @array = (0..3);
my $i;

for ($i = 0; local $_ = $array[$i], $i < @array; ++$i) {
    print "$i: $_\n";

    last if $i > 5;
}

print scalar(@array), "\n";

for ($i = 0; local *_ = \$array[$i], $i < @array; ++$i) {
    print "$i: $_\n";

    last if $i > 5;
}

print scalar(@array), "\n";

__END__

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe