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

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

Thread Previous
Paul Johnson
March 19, 2013 11:34
Re: Infinite loop in for (@a) { push @a, $_ }
Message ID:
On Tue, Mar 19, 2013 at 12:18:11PM +0100, demerphq wrote:
> This code is an infinite loop.
>    my @a = ("A".."D");
>    for (@a) {
>        say;
>        push @a, $_;
>    }
> I think this is a regression as compared to how it would have worked
> when for (@a) operated on the stack and put a copy of @a on the stack.
> Does anyone agree? I would expect this to be the same (more or less) as:
> push @a, @a;
> And not an infinite loop.

perlsyn says:

  If any part of LIST is an array, C<foreach> will get very confused if
  you add or remove elements within the loop body, for example with
  C<splice>.   So don't do that.

That's been there for a very long time, I believe.  An infinite loop
could be considered a symptom of confusion.

That's not to say that the previous behaviour should change if we can
help it, or shouldn't it we can't, or that we shouldn't try to DTRT if
we can work out what TRT is.

Paul Johnson -

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About