in reply to Re: To be, or not to be, a package? That is the question
in thread To be, or not to be, a package? That is the question

I take your point Fletch, but doesn't package introduce a new namespace - so perl should be able to distinguish between the instances of the new() subs ... shouldn't it ? ... or have I missed that boat as well ?

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^3: To be, or not to be, a package? That is the question
by Fletch (Bishop) on Dec 04, 2008 at 14:16 UTC

    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.

Re^3: To be, or not to be, a package? That is the question
by almut (Canon) on Dec 04, 2008 at 14:39 UTC

    As Fletch said, a namespace is not the same as a lexical scope...

    This is important to keep in mind if you merge several modules (.pm files) into one file — for easy distribution, or whatever. Forgetting to put extra braces around each original file / package can introduce nasty subtle bugs... (in particular if the modules are coded sloppily, with lots of global vars, no strictures, etc.).

    The behavior is mentioned in the package docs, btw:

    (...) A package statement affects only dynamic variables--including those you've used local on--but not lexical variables, which are created with my.
      Following Fletch and zentaras replies, I had a look at some of my (really) old code where I vaguely remembered last utilizing multiple packages in one file ... to find I'd used braces round all package(s) - no wonder I hadn't encountered the problem 'til now.

      A user level that continues to overstate my experience :-))