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:
In reply to Re: local our?
by ikegami
in thread local our?
by gam3
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |