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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Use of "my" after Perl v5.14
by Rohit Jain (Sexton) on Sep 20, 2012 at 23:27 UTC | |
by tobyink (Canon) on Sep 20, 2012 at 23:38 UTC | |
by AnomalousMonk (Archbishop) on Sep 21, 2012 at 08:51 UTC | |
by Rohit Jain (Sexton) on Sep 21, 2012 at 00:02 UTC | |
by AnomalousMonk (Archbishop) on Sep 21, 2012 at 05:00 UTC | |
by Anonymous Monk on Sep 21, 2012 at 00:19 UTC | |
by Rohit Jain (Sexton) on Sep 21, 2012 at 00:28 UTC | |
by Anonymous Monk on Sep 21, 2012 at 02:41 UTC |