![]() |
|
Keep It Simple, Stupid | |
PerlMonks |
Re^3: OUR declarationby nobull (Friar) |
on Sep 23, 2006 at 12:43 UTC ( #574510=note: print w/replies, xml ) | Need Help?? |
You cannot use local on lexically scoped variables. This is a totally artificial restriction but it means that in those (rare) ocasions when the semantics of local are the tidiest way to implement something you need to use package variables.
Perl does not support nested named subroutines. If you need nested subroutines you can use anonymous subroutines but this makes stackdumps nigh-on unreadable. Using file-scoped lexicals with local() would be a solution but as I said before you can't do that. So the next best thing is our(). I once wrote some twisted recursive code to render tables of tables in HTML. Sometimes it would cluck() and I'd get a backtrace with a dozen levels of anonynous subroutine.
I decided it was more maintainable as:
Note that frobnicate may call awful() but this inner awful() would not want to share the outer awful()'s variables. Now there is a potential problem here if frobnicate() store its CODE reference argument somewhere and thus manages to call back to recursive1() after awful() completes or for that matter during a reentrant call of awful(). But I happened to know that wasn't going to happen. There's a similar problem to the nested subroutine problem with regex containing (?{}) except you don't get the warning.
In Section
Seekers of Perl Wisdom
|
|