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

[perl #36538] perlfunc/our contradicts itself

Thread Next
From:
Michael G Schwern via RT
Date:
July 14, 2005 19:11
Subject:
[perl #36538] perlfunc/our contradicts itself
Message ID:
rt-3.0.11-36538-117411.7.57026072725992@perl.org
> [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.
http://www.stonehenge.com/merlyn/UnixReview/col54.html

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
parentheses.

    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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About