Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Mine or Ours

by shmem (Chancellor)
on Jan 21, 2007 at 09:24 UTC ( [id://595742]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Mine or Ours
in thread Mine or Ours

Similar or identical? what would be the difference, if similar?

{ our $foo = "bar"; my $quux = "foo"; } print "'$quux'\n"; print "'$foo'\n"; __END__ '' 'bar'

our and my behave pretty different here, which is why I said "file scoped", which may be wrong. The "syntactical range" of my and our are identical in so far as my variables also are visible within their scope throughout different packages (which scope is different to that of our variables).

our creates a package global, which is visible througout the entire file, even crossing packages. So file or lexical scope is pretty much the same. If "file scoped" means "only visible in that file", then no, our is not file scoped.

Sorry for any confusion caused.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^4: Mine or Ours
by gaal (Parson) on Jan 21, 2007 at 09:44 UTC
    No, that snippet is not strict-safe.

    our allows you to refer to somebody's package global in unqualified manner, even when you leave the package. But this permission lasts only within lexical scope.

    use strict; package First; our $foo = 42; { print $foo; # 42 package Second; print $foo; # 42, refers to $First::foo our $bar = 54; { package Third; print $foo; # 42, refers to $First::foo print $bar; # 54, refers to $Second::bar } } print $Second::bar; # 54, fully qualified. print $bar; # error: not strict-safe as the our is not in scop +e.

      .oO(and the monk was enlightened.)

      Wonderful. Thanks for showing how our is lexical scoped.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found