Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Mine or Ours

by logie17 (Friar)
on Jan 20, 2007 at 18:28 UTC ( #595691=perlquestion: print w/replies, xml ) Need Help??

logie17 has asked for the wisdom of the Perl Monks concerning the following question:

Could someone please give a good example as to why one would use to use our $varname vs. my $varname. According to the Perldoc:
"our" has the same scoping rules as "my", but does not necessarily cre +ate a variable.

Could someone explain this to me and supply a good working example?

Thanks in advance,
s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Replies are listed 'Best First'.
Re: Mine or Ours
by BrowserUk (Patriarch) on Jan 20, 2007 at 18:43 UTC
Re: Mine or Ours
by jdporter (Chancellor) on Jan 20, 2007 at 19:07 UTC

    Did you think to check out the Tutorials section of the site? If so, you should have found 'our' is not 'my'. Other tutorials in the Variables and Scoping category might also be helpful to you.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: Mine or Ours
by shmem (Chancellor) on Jan 21, 2007 at 00:03 UTC
    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 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 ""; 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 ;-)


    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}
      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.


        _($_=" "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}

      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?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://595691]
Approved by Joost
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-08-10 05:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found