BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can a sub generate a closure from within itself?
by Fletch (Bishop) on Apr 24, 2004 at 01:05 UTC | |
|
Re: Can a sub generate a closure from within itself?
by broquaint (Abbot) on Apr 24, 2004 at 01:59 UTC | |
|
Re: Can a sub generate a closure from within itself?
by Zaxo (Archbishop) on Apr 24, 2004 at 01:08 UTC | |
|
Re: Can a sub generate a closure from within itself?
by TomDLux (Vicar) on Apr 24, 2004 at 01:05 UTC | |
|
Re: Can a sub generate a closure from within itself?
by simonm (Vicar) on Apr 24, 2004 at 01:02 UTC | |
|
Re: Can a sub generate a closure from within itself?
by dave_the_m (Monsignor) on Apr 24, 2004 at 11:53 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2004 at 13:45 UTC |