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

Re: [perl #113974] package NAMESPACE manpage comments

Thread Previous | Thread Next
From:
Ricardo Signes
Date:
July 5, 2012 19:42
Subject:
Re: [perl #113974] package NAMESPACE manpage comments
Message ID:
20120706024238.GB20266@cancer.codesimply.com
>    Actually, as near as I can tell, they are at the Perl-global level --
> and exist outside of packages -- and outside of files.
> 
> They are accessible to any package that "uses them", but don't
> belong to any package.
> 
> They are the true GLOBAL variables, but only accessible in those packages
> that "use vars" them.
> 
> At least that's how I understand it from my testing...if that's not
> what other people think, I'd like to know why given the posted test
> progs...

I wrote a blog post about what use vars vars are, some time ago.  Here:
http://rjbs.manxome.org/rubric/entry/1862

It's not very complicated.  It works like this:

When you create an alias in your package to a variable in another package, it's
marked "imported" so that you can refer to it easily.  This is great when you
want to get constant $OK from HTTP::Whatever and just say $OK.  The Exporter
does *YourPkg::OK = \$HTTP::Whatever::OK and the little "I was imported!" flag
is set on $YourPkg::OK so that when you say "$OK" in YourPkg, it's permitted
because you specifically asked for that variable from elsewhere.

"use vars" means "mark these variables as imported, even though they are not."

If you "use vars '$x'" in package Foo, then anywhere that you switch into
package Foo, $x will mean $Foo::x (unless it has been shadowed by a lexial
name).

In no case does this $x become a superglobal, available without qualification
everywhere.  It's always accessible as $Foo::x, sometimes as $x, and that's
that.

"our" variables are available by short name in the enclosing lexical scope.

"use vars" variables are available by short name whenever in the importing
package.

-- 
rjbs

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