Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

RE: i thought i knew 'our'...

by autark (Friar)
on Jul 24, 2000 at 20:39 UTC ( #24110=note: print w/replies, xml ) Need Help??


in reply to i thought i knew 'our'...

'our' does _not_ declare a lexical scoped variable! What 'our' does is to say that the variables _name_ shall be lexical scoped. so:
use strict; { our $y = 17; print "inner: $y\n"; } print "outer: $y\n";
says that you are allowed to use the name '$y' in the block. Its name is lexical scoped. It does enter the name $y into the stash. Now, as you have discovered, without strict you may access $y's value. So, how to do it with strict on ?
use strict; { our $y = 17; print "inner: $y\n"; } print "outer: $main::y\n";
strict don't let you use globals (variables that live in the symbol table) unless:
  1. being told that it's actually ok (using 'our' or 'use vars')
  2. you fully qualify its name, as in $main::foo
So naturally, without strict you may access variables without fully quallifying them as in your last example.

Autark.

Replies are listed 'Best First'.
RE: RE: i thought i knew 'our'...
by jlistf (Monk) on Jul 24, 2000 at 20:47 UTC
    are you saying that 'our' lexically scopes the name, but globally scopes the value?
      Something like that, yes. Because the variables are put into the symbol table, they are global. In fact, 'our' has no semantical effect unless you use strict vars, so:
      our $foo = 1;
      and
      $foo = 1;
      shouldn't be different at all. It is first when you introduce use strict vars that the our keyword will have a different semantical effect.

      Autark.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2020-11-27 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?