If that's the best one can do to explain the difference between our and my, I'd have to say the Anon troll who's been going round lately proclaiming Perl is dead is right.

Djees. my in combination with statement modifiers are awkward, and in some cases broken. Could you explain why the latter code prints 1 and not 3 (or throw an error?). And why the former prints 3, and not the same result as the latter? (Hint, it has to do with the runtime effects of my).

So our is somewhat "exotic" in that it declares a package variable, but in a lexical scope.
Eh, not quite. It declares a package variable - but because it's a package variable, it's not lexically scoped. All that's lexically scoped is the name.
{ # Scope 1 our $s = "foo"; # Sets $main::s } { # Different scope. print our $s; # *Same* variable! } __END__ foo
our just means: from now on, till the end of the innermost enclosing block, whenever I refer to the variable(s) following our, I mean the package variables with the same name, in the current package (as per use of our, not necessarely per use of the variable).

So, my gives you a new lexical variable, local gives you a temporary (not lexical!) value, and our gives you a lexical alias or shortcut.

Perl --((8:>*

In reply to Re^4: difference between my and local by Perl Mouse
in thread difference between my and local by jeanluca

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.