Perl Monk, Perl Meditation | |
PerlMonks |
Re: Closure Over Scalar?by davido (Cardinal) |
on Feb 18, 2021 at 16:46 UTC ( [id://11128529]=note: print w/replies, xml ) | Need Help?? |
Consider the following code:
The order printed will be 1, 4, 2, 3, 5, 6. In your code, your definition of $FIXED_STRING happens in position 3, which happens after the sub call (position 4). The 'my' declaration happens at compiletime. The definition happens at runtime, and run time happens line by line unless flow is altered by a loop, subroutine, conditional, exception, or other control of flow mechanisms. Really the best solution is to manufacture your closure through a subroutine:
This strategy would certainly work reliably (assuming I didn't submit a typo). I'm not thrilled with needing to return accessor subs for both setting and dumping. It feels like we're going in the direction that is satisfied by object systems. But it pains me to say that because, I love the elegance of subs manufacturing subs capturing closures. Dave
In Section
Seekers of Perl Wisdom
|
|