develooper Front page | perl.perl5.porters | Postings from November 2016

Re: [perl #129345] perlmod doc bug wrt package scope

Thread Previous | Thread Next
November 24, 2016 07:19
Re: [perl #129345] perlmod doc bug wrt package scope
Message ID:
@Zefram: I am fine on what you say about "The [package] scopes are nested.
... The closest enclosing scope takes precedence, as usual. This is no
from lexical variables of the same name shadowing one another."

This makes sense.

I am tempted to say "Where do the manuals say this?"  But that is pushing
it - nobody expects the Perl manuals to be clear (where "clear" means "free
of surprises').

I do point out that those of us who run with 'use warnings' are used to
having 'lexical variables of the same name shadowing each other'.

%  perl -e 'use warnings; my $a=1;  my $a=2; print $a;'
"my" variable $a masks earlier declaration in same scope at -e line 1.

so the behavior of package is not exactly the same as lexical variables of
the same name - at least not wrt warnings.  But then warnings are an
optional part of Perl.

(No, I am not suggesting that 'package A; package A', i.e. two successive
uses of the same package name shadowing each other give a warning.   They
are not creating two different packages: they are just activating the same
package namespace, redundantly and in a nested manner.)

I think that my main confusion was thinking that "nested package scopes"
might imply that one package was nested within the other.  As in C++, where
'class A { ...   class B { ... } }'   creates classes A and A::B.  But in
Perl packages are not really nested in terms of type structure; 'package A;
... package B; ...'  does not create nested package A::B, it creates
packages A and B, where the lexical scope of B is supposedly nested within
the lexical scope of A.

I say "supposedly nested" because ... is there any way that you can tell
the difference between  'package A; ... package B; ...' with such scoping,
and the following with a hypothetical "end package scope" declaration -
'package A; ... end_package A; package B; ... end package B'  ?

(Where the hypothetical 'end_package' does not affect my variable scopes -
it just removes the current package A from the stack of nested scopes.)

I don't think there is such a way to distinguish such nesting.  Which is
fine: it is a distinction without a consequence, i.e. it is meaningless.
Just a convention.

The content of this message is my personal opinion only. Although I am an
employee (currently of MIPS Technologies; in the past of companies such as
Intellectual Properties, Intel, AMD, Motorola, and Gould), I reveal this
only so that the reader may account for any possible bias I may have
towards my employer's products. The statements I make here in no way
represent my employer's position on the issue, nor am I authorized to speak
on behalf of my employer.

On Tue, Nov 22, 2016 at 10:58 PM, Zefram via RT <>

> David Nicol wrote:
> >Saying "package" has the same scope as "my" can be read as implying
> >that outer packages are still part of what is called the "scope chain"
> >in the ECMA-262 standard,
> They are.  The scopes are nested.
> >      package a; @apples = qw/gala fuji/ ; { package b; print "@apples" }
> >
> >should print "galafuji."
> No.  The closest enclosing scope takes precedence, as usual.  This is no
> different from lexical variables of the same name shadowing one another.
> -zefram

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About