Simplified example:
$ perl -le'$x="green"; for our $x ("red", "blue") { push @a, sub { $x +} } print $_->() for @a' green green $ perl -le'for my $x ("red", "blue") { push @a, sub { $x } } print $_- +>() for @a' red blue

Package variables never go out of scope. Each anon sub in the first command of the example therefore refers to the same variable, $main::x. A for loop with a package variable as its iterator will backup the value of the variable and restore it when the loop is exited to avoid clobbering its parent, so when the anon subs in the first command are executed, they display $main::x which was restored to 'green'.

On the other hand, a new my variable is created each time the scope that contains it is executed(*). That means that my variables that exist outside of a sub need to be captured by the sub to ensure that they are still around the sub is executed. For named subs, this is done when the sub is compiled. For anon subs, this is done when the sub expression is executed. Even if the variable goes out of scope, it will be kept alive and the sub will see the variable that existed when it was captured.

For example, even though $x went out of scope at the end of the file in the following code, set and get keep it alive and can use it to exchange data.

package Module; my $x; sub set { $x = shift } sub get { $x } 1;
use Module; Module::set(123); print Module::get(); # 123

When the anon subs in the second command of the top example are executed, they the current value of the variable they captured. One of those subs captured a variable that has 'red' for value. The other captured a variable that has 'blue' for value.

* — The implementation varies slightly due to optimisation.


In reply to Re: How do closures and variable scope (my,our,local) interact in perl? by ikegami
in thread How do closures and variable scope (my,our,local) interact in perl? by ELISHEVA

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.