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?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Can a sub generate a closure from within itself? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.