SPUG: Predeclaring packages

Ronald J Kimball rjk-spug at tamias.net
Sat Jan 3 12:44:09 PST 2009

On Sat, Jan 03, 2009 at 12:14:08PM -0800, DeRykus, Charles E wrote:
> >> I wouldn't say that $our exposes a value for $greeting - it simply
> exposes $greeting itself.
> Not to overdo semantics but the docs say 'our' exposes a value ..
> maybe just to emphasize that the global value set by 'our', although 
> similar in some ways to a lexical 'my',  behaves differently, eg, 

perldoc -f our states:

  "our" associates a simple name with a package variable in the current
  package for use within the current scope.

If some place in the documentation suggests that 'our' exposes a value, I'd
argue that's misleading and should be clarified.  :)

Not to put too fine a point on it, the global value is set by the
assignment, not by 'our'.

> You're right but the compile/runtime issues gets confusing 
> to me anyway because setting the global $Hello::greeting at 
> runtime does work but not via 'our'. 
> #our $greeting = 'Hello there';     # not ok
> $Hello::greeting = 'Hello there';   # ok
> Hello::sayHi();
> exit(0);
> package Hello;
> { our $greeting;
>   sub sayHi { our $greeting; print $greeting . "\n" }
> }

The documentation mentioned the "current package".  The commented-out line
in your code snippet is in the package main, not the package Hello.

our $greeting = 'Hello there';

package Hello;
sub sayHi { print $main::greeting . "\n" }


More information about the spug-list mailing list