The title says it all pretty much. I'm currently using a instantiator sub to generate a utility sub that closes over some instance state:
sub instantiator{ my $state = @_; return sub{ return #some function of state and @_; } } ... my $utility = instantiator( 'init' ); ... $utility->( args );
That works fine for my purpose, but now I'd like to be able to chose/adjust the type (scalar/array/hash) of the closure when calling the utility sub, but each instance of the utility sub needs it's own independant closure. Any thoughts?
Update: I should have been clearer. The type of the closure would need to change during the lifetime of the utility function in response to arguments passed when calling it, so I can't generate an appropriate closure when it is instantiated.
So the question is really: Is there any way for a sub to generate a new closure--in my terms, a piece of cross-call persistant storage, lexically visible only from within the sub so there is no conflict between different instances of the sub--from within that sub?
Normally not, but I'm wondering if there is some B::* or Devel::* trick that can generate a closure on-the-fly?
In reply to Can a sub generate a closure from within itself? by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |