You're wrong :-)

our makes a lexically scoped alias for a global. That means it remains in scope even if you change the package. Compare:

use strict; package foo; our $bar = 1; # at this point, using $bar could mean TWO things: # 1) try to access the global $foo::bar # 2) try to access the alias named $bar for $foo::bar # The lexical (second) one wins in perl, so you don't # get a use strict error print $bar; package baz; # Now $bar could mean: # 1) try to access the global $baz::bar # 2) try to access the alias named $bar for $foo::bar # The lexical (second) one still wins. print $bar; # works, prints $foo::bar even though we are in baz
with:
use strict; package foo; use vars qw/$bar/; $bar = 1; # $bar has only one meaning, access $foo::bar print $bar; package baz; # $bar has only one meaning, access $baz::bar print $bar; #error

To answer the original question, the documentation is right, the our (as used in the first code fragment) has no effect outside the file. But... you can still access the global $foo::bar from another file. But this is NOT using the "our" alias as you can for example see by trying to do in that other file:

package foo; print $bar; # prints the global $foo::bar package baz; print $bar; # prints the global $baz::bar
The lexical alias is gone, and $bar just accesses the global in the package that is in scope.

In reply to Re: Re: 'our' scoping by thospel
in thread 'our' scoping by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.