in reply to local our?
our $bob; local $bob; $bob = 'bob';
our $bob; disables use strict 'vars' for $bob for the remainder of current scope. It allows you to use package variable $bob without its package name where you might have had to without our $bob;. [Note that this is a bit of an oversimplification. (Upd)]
local $bob; saves the current value of $bob (and sets $bob to undef). When the scope is exited (whether normally or by exception), $bob reclaims the value saved by local $bob.
$bob = 'bob'; assigns a value to $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 | |
by ikegami (Patriarch) on Apr 15, 2019 at 01:05 UTC | |
by LanX (Saint) on Apr 14, 2019 at 21:15 UTC | |
|
Re^2: local our?
by LanX (Saint) on Apr 14, 2019 at 20:55 UTC | |
|
Re^2: local our?
by aufflick (Deacon) on Apr 05, 2006 at 05:48 UTC | |
by LanX (Saint) on Apr 14, 2019 at 21:25 UTC | |
by ikegami (Patriarch) on Apr 05, 2006 at 14:16 UTC | |
by aufflick (Deacon) on Apr 07, 2006 at 02:10 UTC | |
by Errto (Vicar) on Apr 07, 2006 at 04:08 UTC | |
|
Re^2: local our?
by Anonymous Monk on Apr 14, 2019 at 19:44 UTC | |
|
Re^2: local our?
by BillKSmith (Monsignor) on Jun 18, 2015 at 15:27 UTC |