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

[perl #36538] perlfunc/our contradicts itself

Thread Next
Michael G Schwern via RT
July 14, 2005 19:11
[perl #36538] perlfunc/our contradicts itself
Message ID:
> [ysth - Wed Jul 13 14:13:53 2005]:
> Wording looks good, but I'd prefer to see C<use strict 'vars'>.

Here it is again with that fixed plus the simple explaination lifted
from Randal's column.

I also clarified in the cross-package example that the $bar used in
package Bar refers to $Foo::bar.  Didn't feel it was entirely obvious.

C<our> associates a simple name with a package variable in the current
package for the remander of the lexical scope.  The listed variables
are declared to be valid globals within the enclosing block, file, or
C<eval>.  That is, it has the same scoping rules as a "my"
declaration, but does not create a local variable.  When C<use strict
'vars'> is in effect, the C<our> declaration lets you use the declared
global variable without qualifying it with a package name.  (But only
within the lexical scope of the C<our> declaration.  In this it
differs from "use vars", which is package scoped.)

If more than one value is listed, the list must be placed in

    our $foo;
    our($bar, $baz);

An C<our> declaration declares a global variable that will be visible
across its entire lexical scope, even across package boundaries.  The
package in which the variable is entered is determined at the point
of the declaration, not at the point of use.  This means the following
behavior holds:

    package Foo;
    our $bar;		# declares $Foo::bar for rest of lexical scope
    $bar = 20;

    package Bar;
    print $bar;		# prints 20 as it refers to $Foo::bar

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