Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Re: 'our' scoping

by thospel (Hermit)
on Jun 02, 2004 at 03:08 UTC ( [id://359083]=note: print w/replies, xml ) Need Help??


in reply to Re: 'our' scoping
in thread 'our' scoping

You're wrong :-)

our makes a lexically scoped alias for a global. That means it remains in scope even if you change the package. Compare:

use strict; package foo; our $bar = 1; # at this point, using $bar could mean TWO things: # 1) try to access the global $foo::bar # 2) try to access the alias named $bar for $foo::bar # The lexical (second) one wins in perl, so you don't # get a use strict error print $bar; package baz; # Now $bar could mean: # 1) try to access the global $baz::bar # 2) try to access the alias named $bar for $foo::bar # The lexical (second) one still wins. print $bar; # works, prints $foo::bar even though we are in baz
with:
use strict; package foo; use vars qw/$bar/; $bar = 1; # $bar has only one meaning, access $foo::bar print $bar; package baz; # $bar has only one meaning, access $baz::bar print $bar; #error

To answer the original question, the documentation is right, the our (as used in the first code fragment) has no effect outside the file. But... you can still access the global $foo::bar from another file. But this is NOT using the "our" alias as you can for example see by trying to do in that other file:

package foo; print $bar; # prints the global $foo::bar package baz; print $bar; # prints the global $baz::bar
The lexical alias is gone, and $bar just accesses the global in the package that is in scope.

Replies are listed 'Best First'.
Re: Re: Re: 'our' scoping
by Sandy (Curate) on Jun 02, 2004 at 14:00 UTC
    Thankyou thankyou thankyou...

    You have fixed many confusions that I had. I thought that 'use vars' and 'our' were identical, and it did cause me some confusions because some of my code has a mixture of both (pre 5.6 and post 5.6).

    I went back and re-read the our documentation in the camel book and it is very precise, and now I understand (I think) what it is talking about.

    However, when I went back and re-read the use vars documentation in the camel book, I realize the source of my confusion...

    quote (my italics)

    use vars qw($frobbed @munge %seen);
    This pragma, once used to declare a global variable, is now somewhat deprecated in favor of the our modifier. The previous declaration is better accomplished using :
    our ($frobbed, @munge, %seen);
    ...

    No matter which of these you use, remember that they're talking about package globals, not file-scoped lexicals.

    This information had led me astray (it may be technically correct, but it confused me nonetheless).

    In closing, I would be really appreciative if you would turn your post into a mini-tutorial.

    Thanks,

    Sandy

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://359083]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-18 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found