in reply to local our?

local and our are completely unrelated. For now, let's expand the code and look at the statements individually.
our $bob; local $bob; $bob = 'bob';

Now, let's put these into context.

Say you wish to take advantage of dynamic scoping. That means you need to use a package variable, not a lexical (my) variable. To avoid having to specify the package name when using that package variable, you use our. Let's also say you wish to protect the value that's currently being held by this variable. local would be used.

Why would you want to use dynamic scoping? Maybe you want to override a global option temporarily. For example,

use strict; use warnings; package MyModule; sub do_it { my ($arg) = @_; our $VERBOSE; ... warn(...) if $VERBOSE; ... } sub do_it_quietly { my ($arg) = @_; our $VERBOSE; # Avoid saying $MyModule::VERBOSE local $VERBOSE; # Save existing value. $VERBOSE = 0; # Disable logging. do_it($arg); } 1;

Finally, since local and our return their arguments as lvalues, it's possible to chain these functions. For example, the following are all equivalent:

Replies are listed 'Best First'.
Re^2: local our?
by shmem (Chancellor) on Apr 14, 2019 at 20:54 UTC
    local and our are completely unrelated.

    That's not true. local is about aliasing package globals, and our creates a package global in the package where that declaration occurs (and a lexical alias to that in the current scope), so they are related: they both act upon slots in the symbol table of a package. So they are related.

    $ perl -E 'package Foo; local our $bar; package main; say for keys %Fo +o::' bar

    and

    $ perl -E 'our $bar = "foo"; say "1: $bar"; package Foo; { local our $ +bar = "quux"; say "2: $bar"; }; say "3: $bar"; package main; say "4: +$bar"; say "Foo::$_" for keys %Foo::' 1: foo 2: quux 3: foo 4: foo Foo::bar
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      The fact that they both can take a package variables as an argument doesn't make them related in any meaningful way.

      local is about aliasing package globals

      No. For example, my %h; local $h{x};. It's about making temporary backups of some values.

      our creates a package global

      No. our creates a lexical variable (that's aliased to the specified package variable).

      Simply mentioning the package var is what creates it.

      $ perl -le' print $::{var} // "[undef]"' [undef] $ perl -le'$var; print $::{var} // "[undef]"' *main::var
      I think it's important to stay accurate:

      > our creates a package global in the package where that declaration occurs

      our will create a new STASH entry only, iff it doesn't already exist and it will not touch the value.

      > local is about aliasing package globals

      I can't see any "aliasing" process here, it's still the same underlying package variable.

      what do you call an "alias" here when doing local $main::bob; ?

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

Re^2: local our?
by LanX (Saint) on Apr 14, 2019 at 20:55 UTC
    > disables use strict vars for $bob for the remainder of current scope.

    this is misleading, no strict "vars" has a different effect.

    { local our $bob; #no strict 'vars'; $bob = "bob"; warn "$bob\n"; # > bob package OTHER; warn "$bob\n"; # > bob }

    { #local our $bob; no strict 'vars'; $bob = "bob"; warn "$bob\n"; # > bob package OTHER; warn "$bob\n"; # ERR: Use of uninitialized value $OTHER::bob in concatenation }

    > local and our are completely unrelated.

    well not completely, since local can't be used for private vars

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

Re^2: local our?
by aufflick (Deacon) on Apr 05, 2006 at 05:48 UTC
    our $bob; disables use strict 'vars' for $bob for the remainder of current scope.
    For my interest, is that actually how our is implemented, or is that just your way of explaining the effect?
      > > disables use strict vars for $bob for the remainder of current scope. > For my interest, is that actually how our is implemented ...

      no, see my other answer in this thread.

      our $bob is aliasing at compile time a "simple" symbol $bob to a full qualified $__PACKAGE__::bob for the remainder of current scope.

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

      I'm not sure I understand your question. I didn't mention any algorithm, so I don't see how I could have spoken of implementation. I described its function and its effect.
        Sorry, I didn't fully qualify that question. I was interested in how the perl compiler/runtime implements the our directive (if anyone here happens to know that).
Re^2: local our?
by Anonymous Monk on Apr 14, 2019 at 19:44 UTC
    Just a warning: "Finally, since local and our return their arguments as lvalues, it's possible to chain these functions" implies that

      our local $var

    should also be equivalent. It's not: Perl returns the error:

      No such class local at ..., near "our local"

    In the end,

      local our $var

    is the way to go when you need to do this.

Re^2: local our?
by BillKSmith (Monsignor) on Jun 18, 2015 at 15:27 UTC
    Thanks for your great example. I never could think of a practical use for 'our' because it never occurred to me that the same name could be declared in more than one scope and that both would refer to the same variable. This is a great solution to the problem of initializing a variable in a BEGIN block and using it in the main code.
    Bill