This is an interesting edge case. I modified your code to eliminate $x, using localized $_ to see if that changed the behavior. It doesn't. Behold:

use strict; use warnings; my $cl3 = sub { local $_; foreach (0..1,5..6) { print $_++ , "\n"; } }; $cl3->(); $cl3->(); $cl3->();

This returns "015612672378". I ran it through B::Deparse to see what I wasn't seeing already. The deparsed version is like this:

use warnings; use strict 'refs'; my $cl3 = sub { local $_; foreach $_ ((0, 1), (5, 6)) { print $_++, "\n"; } } ; &$cl3(); &$cl3(); &$cl3();

If you run that, you get "Modification of a read-only value attempted at mytest.pl line 13." So deparse breaks your edge case by pre-enumerating the '..' lists.

Update: ikegami is probably right; you've found an optimization, and a way to demonstrate it changing a script's expected behavior. And given the fact that it's probably an optimization you've bumped into, it isn't surprising that the deparsed version isn't able to "benefit" from the optimization, since it enumerates out the .. list.

I'm curious: Every now and then someone posts one of these somewhat amazing but carefully constructed scenarios that exhibits an unexpected behavior or points out a bug. My curiosity is this, did you discover this while tracking down a real-world bug, while playing around with edge cases on your own, by reading the Perl source code, or was someone discussing it in some other forum (eg IRC or P5P) such that you caught wind of it and spilled it over into PerlMonks? I'm not making a value judgement, I always wonder how these things are first discovered. There have been a couple of oddities that I've uncovered while playing around with and refining obfuscations, for example.


Dave


In reply to Re: Unexpected behaviour with constant lists and foreach (and closures?) by davido
in thread Unexpected behavior of '..' lists by Crackers2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.