Perrin Harkins schrieb am 27.04.2010 um 14:20:13 (-0400):
> On Tue, Apr 27, 2010 at 2:08 PM, Michael Ludwig <mil...@gmx.de> wrote:
> > Variables declared with "our" are a funny hybrid between global
> > variables, which are attached to a package, and lexical variables,
> > which are attached to a scope.
> 
> They are package variables (usually referred to as globals), which
> have a lexically-scoped alias that lets you call them by their short
> name.  It's the short name alias that is lexical.

I used to think (and still do so) that non-lexical variables, just
like subroutines, belong to the package they're in and do not need
the package prefix. Using which, however, it is possible to refer
to variables in a package other than the current one, like
$Data::Dumper::Indent = 1 or something similar. But simply writing
$bar in package Foo just refers to $Foo::bar:

          \,,,/
          (o o)
------oOOo-(_)-oOOo------
package Foo;
$bar = 1;
package main;
print "bar = $bar";
print "Foo::bar = $Foo::bar";
-------------------------

$ perl -l /tmp/pkg.pl
bar = 
Foo::bar = 1

It's only the strict pragma that (fortunately) forces you to qualify
your globals. In other words, the "alias" is not really an alias, but
rather the thing itself, regardless of whether is is excepted from the
strict 'vars' pragma by means of (1) full qualification or (2) "use
vars" or (3) "our".

> Here's a normal use of a package variable:
> 
> package Foo;
> use strict;
> use warnings;
> 
> $Foo::bar = 1;

I think I've more frequently encountered the form:

use strict;         # ban unqualified globals
use vars qw($bar);  # make exceptions
$bar = 1;           # use them

As perldoc -f our informs you, the above has been superseded by "our".
However, you still see a lot of "use vars" for reasons of backward
compatibility.

-- 
Michael Ludwig

Reply via email to