Scoping, compile time / run time, and global / my issues. my variables are located in space, local variables in time. Package variables are "grandfather local" variables - see my/local, space/time (was: Re: The difference between my and local).

Your code commented:

my $value = "four"; # lexical variable my @array = ( "one", "two", "three" ); foreach $value (@array) { # variable $value inside the loop is set # to each element of @array at runtime print "foreach value: $value\t"; function(); } # outer scope lexical $value visible here sub function { # outer scope lexical $value visible here # with its initial value print "function value: $value\n"; }

Had you used a package global throughout, the outcome would have been different:

$value = "four"; my @array = ( "one", "two", "three" ); foreach $value (@array) { print "foreach value: $value\t"; function(); } sub function { print "function value: $value\n"; } __END__ foreach value: one function value: one foreach value: two function value: two foreach value: three function value: three

What about allocating a sub in a block's scope? Let's look at this weird construct:

my $value = "four"; my @array = ( "one", "two", "three" ); foreach $value (@array) # note outer $value variable { print "foreach value: $value\t"; function(); sub function { print "function value: $value\n"; } } __END__ foreach value: one function value: four foreach value: two function value: four foreach value: three function value: four

The result is the same, although the sub is allocated inside the for() block scope. Why? Because the sub, on compilation, gets the outer $value, whereas the loop iteration variable is an alias to the outer.

But what about that?

my $value = "four"; my @array = ( "one", "two", "three" ); foreach my $value (@array) { print "foreach value: $value\t"; function(); sub function { print "function value: $value\n"; } } __END__ foreach value: one function value: foreach value: two function value: foreach value: three function value:

How come? Declaring the loop variable as a my variable to the loop block, the inner loop gets that at compile time, and closes over it (it's a closure), and this variable is initialized empty (assignment happens at run time). The inner sub variable is different from the aliased loop variable.

All this is presented here with a lot of handwaving. The perl sources might provide a clue about what's really happening. Pass arguments to your subroutines to be save.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: foreach localizing variables by shmem
in thread foreach localizing variables by AndyJ

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.