develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Infinite loop in for (@a) { push @a, $_ }

Thread Previous | Thread Next
From:
Zefram
Date:
March 19, 2013 16:10
Subject:
Re: Infinite loop in for (@a) { push @a, $_ }
Message ID:
20130319161005.GS20429@lake.fysh.org
Brad Gilbert wrote:
>$ perl -E'@a=10;for(@a){push @a, $_-1 if $_} say join ",", @a'

That falls squarely within the range of things that perlsyn(1) warns
you not to do.  We can break that with a clear conscience, if we have
a better behaviour to implement.

The only behaviour that I think it would be worth changing this for
is to detect modification of an array being iterated over and to die
when that happens.  (Either immediately upon modification or at the
start of the next iteration after modification.)  This could perhaps be
achieved, with acceptable performance characteristics, by something akin
to local()ly setting the readonly flag on the array while it's being
used by the iterator.  (Best not the actual SvREADONLY flag, as that
implies that the array will *always* be read-only.  But we're short of
spare flag bits.)

-zefram

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About