SPUG: Confusing behaivior with exported variables

Tye McQueen tyemq at cpan.org
Tue Mar 24 20:10:38 PDT 2009

On Tue, Mar 24, 2009 at 3:37 PM, Jacinta Richardson <
jarich at perltraining.com.au> wrote:

> > I'll leave the in-depth explaining of the differences between "lexical
> > variables" and "package globals" to other sources, but those are the two
> > choices. :)
> Technically you're both wrong.  Declaring variables with "our" does not
> make them global.  It makes them package variables.

By that definition, Perl doesn't implement global variables.

In Perl, "global variables" are "package variable" are "package globals".
Package variables are globally accessible but have names partitioned into
namespaces (and the namespaces are called "packages").

Package variables (those declared with our, or created when you mention a
> variable without strict turned on) are visible from the point they are
> created
> to the end of their file; and are accessible to other files as well (when
> exported or when their full name is used):

$foo::bar is certainly a global variable.  The shorter form, $bar, can
sometimes be used to access the global variable.  There are lots of ways to
make that happen ["package foo;" w/o "use strict" or w/ "our $bar;" or w/
"use vars qw($bar)" or "bar" actually something special like "ISA",
importing, "bar" actually being a punctuation mark and "foo" being "main",
etc.] and the details of each (especially "our") can get a bit complicated,
even confusing.  But in such cases, $bar is not a different variable from
$foo::bar, so $bar is just a convenient name for the global variable, even
though whatever trick you used to make the short form usable may not apply

Your "visible from the point they are created to the end of their file" is
sort-of close to true for most of the tricks for making the short form
usable.  The 'our' trick is actually lexically scoped (which is what makes
'our' such a complex case).  Lots of the other tricks work in any code
compiled after the trick was executed (which often seems close to "from here
to the end of the file" but is rather different in several ways).

> Perl's true globals are its special variables, which are visible and
> changable
> by everything - whether you want them to be or not.

Um, Perl's punctuation variables /are/ package variables.  $_ is just short
for $main::_.  It is just that $_ is almost always short for $main::_ (even
if the current package is not "main").

Package variables are often
> called globals, but they're not real globals in the computer science sense.
> Close enough though.

Package variables are visible and changeable by everything, whether you want
them to be or not.  The /variables/ are global.  Their long names are
global.  Their short names are partitioned into (global) packages (and can
be "enabled" in several ways, even including a few ways to enable the short
name outside of the package it lives in).  But that doesn't make the
variables "not really globals" even if you add "in the computer science
sense".  Technically.

Calling them "package globals" hints at some of these complexities regarding
the names of these global variables.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20090324/522f62a5/attachment.html>

More information about the spug-list mailing list