Yes it introduces a new namespace, but again lexical variables have nothing to do with the symbol table and package variables. Maybe this and a reading of Coping with Scoping will help.

use strict; use warnings; ## Define a lexical variable $foo (which will be otherwise unrechable +after ## the next our save by the closure) my $foo = 'file lexical'; sub closure { print "closure says: $foo\n" } print "before our: $foo\n"; ## This masks the above lexical $foo for the rest of the outer file sc +ope our $foo = '$main::foo'; package One; ## And this masks the prior lexically visible $foo that pointed to $ma +in::foo our $foo = '$One::foo'; { package Two; ## This $foo temporarily masks the prior $One::foo our $foo = '$Two::foo'; print "inside package Two: $foo\n" } ## Prior our $foo was scoped to the explicit block, so unadorned $foo ## once again means $One::foo . . . print "back outside Two's block: $foo\n"; package main; print "unadorned \$foo: $foo\n"; ## But we can still explicitly refer to each of them by full package n +ame { no strict 'refs'; print "$_: ${$_}\n" for qw( main::foo One::foo Two::foo ); } ## And now the sub closure is the only thing which has a handle on the ## original lexical $foo closure(); exit 0; __END__ before our: file lexical inside package Two: $Two::foo back outside Two's block: $One::foo unadorned $foo: $One::foo main::foo: $main::foo One::foo: $One::foo Two::foo: $Two::foo closure says: file lexical

Update: Expanded example with more chattiness and a closure.

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re^3: To be, or not to be, a package? That is the question by Fletch
in thread To be, or not to be, a package? That is the question by Bloodnok

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.