go ahead... be a heretic | |
PerlMonks |
Re: Recursive subroutines and closuresby GrandFather (Saint) |
on Sep 21, 2022 at 01:50 UTC ( [id://11147044]=note: print w/replies, xml ) | Need Help?? |
Consider:
Prints:
So maybe Inner isn't as encapsulated as you might hope? Now consider:
Prints:
In this case Inner has closed over $param from the enclosing Outer scope with results that may not be quite what you expected! Note that the second call to Outer doesn't recreate the closure and that the copy of $param used by Inner is that used on the first call to Outer. There are times when this is exactly what you want to happen, but not very often. Placing Inner after Outer makes the code clearer to my eye and without actually closing over variables as in the second example, there doesn't seem to be much advantage in nesting Inner in Outer.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
In Section
Seekers of Perl Wisdom
|
|