I doubt that you will find any example in which our makes a huge difference.

So why bother having it at all? I suppose that the key idea behind our is to mitigate some of the dangers associated with global variables (when used in conjunction with strict). For instance, consider this case:

use strict; use vars '$x'; $x = 3; sub critical { my $w = 50; # my $x = 100; # oops: commented out by mistake # my $y = 200; my $y = 300; frobnicate( $w, $x, $y ); # that's the global $x in there! }
So, by a programming error the global value is now passing as a lexical. our offers a way to reduce the likelihood of such an error:
use strict; $MyModule::x = 3; # ... sub critical { my $w = 50; # my $x = 100; # oops: commented out by mistake # my $y = 200; my $y = 300; frobnicate( $w, $x, $y ); # won't compile }
...but you can still turn off the qualification requirement selectively:
sub that_uses_global_x_heavily { our $x; # we drop the formalities locally foobar( $x ); # ... $x = tweak( $x ); # ... and_one_more_thing( $x ); # ... }

Of course, there are other ways to avoid the errors that this scheme is trying to prevent, starting with using very different naming conventions for globals and lexicals.

So, bottom line, if you believe that (1) globals that are "easily accessible" (i.e. without the need for full qualification) from anywhere in the package are an invitation to difficult-to-detect bugs; and (2) fully qualified names are a pain, then our lets you have your cake and eat it too. Whether this capability was worth adding (along with the attendant confusion and pitfalls) would depend on how strongly you feel about (1) and (2).

the lowliest monk


In reply to Re: Examples fo Where "our" is really needed by tlm
in thread Examples fo Where "our" is really needed by geekondemand

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.