Your code shows is that it prints the current value of $main::x, as set using = or by local restoring the backed up value.

it seems to me that an enclosed global does in fact get whatever happens to be the localized value of its current context.

I have no idea what that means. "Enclosed global" is a contradiction. Values aren't localised, variables are. (It backups up their value and restores it later.)

when it runs in a block where $x is localized, it uses the localized value, not the value from the outer block.

Again, "localised value" makes no sense.

The "when" is misleading. There's nothing conditional about it. The same would apply if it wasn't localised. The whole sentence could be replaced with "It uses the current value".

I was thinking that localization "overlay" the variable with a new variable,

Localisation

  1. Backups the variable (to be restored on scope exit).
  2. Creates a new SV.
  3. Aliases the variable to that SV.

But it's still the same variable. Any change to the variable will be seen globally.

$y = 123; sub f { print("$y\n"); } # 456 { local $y = 456; f(); } # Same var

That differs from my which actually creates a new var.

my $x = 123; sub f { print("$x\n"); } # 123 { my $x = 456; f(); } # Different var

In reply to Re^5: 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.