http://www.perlmonks.org?node_id=994779


in reply to Re^2: Use of "my" after Perl v5.14
in thread Use of "my" after Perl v5.14

I suppose it's also worth mentioning that my and our have two other friends: local and state.

local sets a new, temporary value for a package variable, which expires at the end of the current scope, with the variable's original value being restored.

In this example, bar() sets $::hello temporarily to "Goodbye" and this new value is visible to foo(), but after bar() has finished running, the original value of "Hello" is restored again.

use v5.14; sub foo { say $::hello; } sub bar { local $::hello = "Goodbye"; foo(); } $::hello = "Hello"; bar(); say $::hello;

And state can be used to create a variable which keeps its state, not being re-initialised.

use v5.14; sub counter { state $count = 1; say $count++; } counter(); counter(); counter(); counter();
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'