Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Mine or Ours

by gaal (Parson)
on Jan 21, 2007 at 06:53 UTC ( [id://595731]=note: print w/replies, xml ) Need Help??


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

our isn't file scoped, it's lexically scoped. If you happen to put your our at the top level, it behaves similarly to file scope. (But the storage allocated is in some package's symbol table and can be accessed from outside the file, for example with its fully qualified name.)

The syntactic range ("range" being a tentative disambiguation term I'm inventing here instead of the overloaded "scope") of our and my are identical.

Replies are listed 'Best First'.
Re^3: Mine or Ours
by shmem (Chancellor) on Jan 21, 2007 at 09:24 UTC

    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}
      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://595731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found