our is a compile-time declaration for package variables like my for private variables.

our is lexically aliasing $bob to $PKG::bob (the "fully qualified name")

"Lexically" means here till the end of the surrounding scope

PKG is the current package name, i.e. main by default.

It's important to understand that the variable is global and won't be destroyed at the end of the scope, like with private my-variables. Only the alias is disappearing.

local is a runtime directive to save the variables value and restore it at the end of the scope. local is the builtin mechanism to avoid global consequences to package vars. (Perl4 didn't have private my-vars, and unfortunately one "can't localize lexical variable" ³)

> Where and why would I use

You can write our $bob multiple times to avoid the need to write $PKG::bob inside dedicated scopes. But it won't create a new local variable like my does.

local our $bob = 'bob'; will protect the old value from being overwritten by "bob".

It's a shortcut for writing 2 statements our $bob;local $bob ="bob"; *)

use strict; use warnings; use Data::Dump qw/pp dd/; $main::bob = "by"; # fully qualified global var warn "$main::bob\n"; # > by { our $bob = "tail"; # lexical alias warn "$bob\n"; # > tail } warn "$main::bob\n"; # > tail # warn "$bob\n"; # ERR: Global symbol "$bob" requires explicit package name { local our $bob = "bob"; # protect from global effects warn "$bob\n"; # > bob package OTHER; warn "$bob\n"; # > bob (still aliased to $main::b +ob ²) } warn "$main::bob\n"; # > tail

So yes, there are circumstances where it's pretty useful to write local our $var

HTH! =)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

UPDATE
*) logical error corrected, thanks to choroba to notice the glitch.

²) This demonstrates an essential often ignored difference to "use vars ;"

³) it's unfortunate that "lexical var" is synonymously used for "private var", since like demonstrated, our has a lexical effect.


In reply to Re: local our? by LanX
in thread local our? by gam3

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.