develooper Front page | perl.perl5.porters | Postings from July 2012

Re: [perl #113974] package NAMESPACE manpage comments

Thread Previous | Thread Next
From:
David Golden
Date:
July 5, 2012 16:12
Subject:
Re: [perl #113974] package NAMESPACE manpage comments
Message ID:
CAOeq1c-ocMcA6qVp07Ag-GGGQm0T4LiBqZMLHNLHDxJTABhSmw@mail.gmail.com
On Thu, Jul 5, 2012 at 6:44 PM, Linda W <perl-diddler@tlinx.org> wrote:
>     Is nested package scoping documented somewhere?...

It's just a natural consequence of scoping.  A package statement
(without a trailing block) doesn't create a scope -- it merely changes
the namespace of the remainder of the compilation of that scope.

>     I get regularly flamed for just wanting to put multiple packages in the
> same file --
> I can't imaging the reaction that would occur with I use nested packages...

I don't know the situation you're in -- if you have a particular
constraint that forces you to keep everything in a single file.  I
wouldn't flame you for it, but as I said, in my *own* code, having
multiple namespaces in one file is a warning to me that I haven't
modularized my code enough.

There are exceptions, usually when I need to monkey patch some other
module -- and sometimes I might include a small class as a separate
package -- but *very* rarely do I access variables directly across
packages.

> "our" -- which sounds like it is more than a little misleading!)...

I suspect that it's intended to sound related to "my".

Personally, I find the opening paragraph of the 'our' documentation to
be correct, but understanding why does require understanding all the
jargon.

> So when you use an 'our' variable, it's not necessarily a package variable
> of the current
> package!...

Right.  The package is associated when you say "our $foo", and then
$foo remains associated with that package for the rest of the scope
(even if the active package subsequently changes).

> So somehow it just seem WRONG to be referring to 'our' vars as package
> variables
> when really it is
> "use vars" that are the true packgage variables that are (obsolete?)
> or "discouraged"....

"our" variable *are* package variables -- but the package is bound to
the identifier when the "our" declaration appears.  It is not dynamic
as the active namespace changes.  Maybe that's what needs clarifying.

As far as I know, nothing built-in is dynamic in that way.  Even "use
vars" is not dynamic, the package is bound when 'use vars' appears.
It's just file-scoped rather than block-scoped -- making it truly a
'global' for the file, whereas 'our' creates an alias for the package
variable only for a block (which could also be the same as the file,
if it appears top level).

I agree that the documentation as written is confusing for someone not
already very familiar with the jargon.

-- David

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About