Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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}

In reply to Re: Mine or Ours by shmem
in thread Mine or Ours by logie17

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found