|
|
| Keep It Simple, Stupid | |
| PerlMonks |
Re^5: Use of "my" after Perl v5.14by tobyink (Prior) |
| on Sep 20, 2012 at 23:38 UTC ( #994787=note: print w/ replies, xml ) | Need Help?? |
|
local can generally be implemented manually. The following two are pretty much exactly equivalent.
local just means that you don't have to remember to manually restore the original value, which can be especially tricky if your sub has several different exit routes (e.g. different return statements in if blocks). Given that local is so much easier than the manual way, there's little reason to go down the manual route. However situations where you need to do this kind of thing are usually pretty rare, and often confined to Perl's built-in variables like $_ and $/. state is also not especially difficult to handle manually. The following is a reimplementation of my previous "counter" example:
Unlike local there is a valid reason to avoid state. It's a relatively new feature, so if you use it your code will not run on Perl versions prior to 5.10.
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||