<div class="gmail_quote">On Tue, Mar 24, 2009 at 3:37 PM, Jacinta Richardson <span dir="ltr"><<a href="mailto:jarich@perltraining.com.au">jarich@perltraining.com.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">> I'll leave the in-depth explaining of the differences between "lexical<br>
> variables" and "package globals" to other sources, but those are the two<br>
> choices. :)<br>
<br>
</div>Technically you're both wrong. Declaring variables with "our" does not make
them global. It makes them package variables.</blockquote><div><br>By that definition, Perl doesn't implement global variables.<br><br>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").<br>
<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Package variables (those declared with our, or created when you mention a<br>
variable without strict turned on) are visible from the point they are created<br>
to the end of their file; and are accessible to other files as well (when<br>
exported or when their full name is used):<br>
</blockquote><br>$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 globally.<br>
<br></div>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).<br>
<div> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Perl's true globals are its special variables, which are visible and changable<br>
by everything - whether you want them to be or not.</blockquote><div><br>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").<br>
<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Package variables are often<br>
called globals, but they're not real globals in the computer science sense.<br>
Close enough though.<br>
</blockquote>
<br>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.<br>
<br>Calling them "package globals" hints at some of these complexities regarding the names of these global variables.<br><br>Tye<br></div></div>