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

Re: Mine or Ours

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


in reply to Mine or Ours

Apart from the links already provided, my/local, space/time might be helpful; our is different to both. Apart from our, you have also use vars LIST. They are similar, but different. Both create package globals, but variables created with our are also file lexical scoped. If you have multiple packages (namespaces) in one file, a variable declared with our is visible in all those packages but file scoped for alien packages, because a symbol table slot only exists for the package the variable was declared in:
use strict; package foo; our $quux = "Howdy, world!\n"; # dump symbol table print "foo: $_ => $foo::{$_}\n" for keys %foo::; package bar; # dump symbol table print "bar: $_ => $bar::{$_}\n" for keys %bar::; print $quux; __END__ foo: quux => *foo::quux Howdy, world!

There you have the "does not necessarily create a variable" part - no variable is created in package bar. The variable $quux is shared between both packages - they would say "it's our $quux" if they could speak. Same applies for variables declared with our in different files. Having a file as

# file include.pl use strict; our $me; sub japh { print $me,"\n"; }

to be included in a main script

#!/usr/bin/perl use strict; our $me = "Just another perl hacker"; require "include.pl"; japh();

running the main script will output

Just another perl hacker

The $me variable is shared between the two files. That's what our means ;-)

--shmem

update: corrected "file scoped" to "lexical scoped". See Re^4: Mine or Ours.

_($_=" "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^2: Mine or Ours
by gaal (Parson) on Jan 21, 2007 at 06:53 UTC
    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.

      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.
Re^2: Mine or Ours
by Herkum (Parson) on Jan 21, 2007 at 03:10 UTC

    I did not know that our was also file scoped. I never use it which probably why it has never been a problem but you never know when you may come across something.

    For a simple question, still get to learn something... :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found