This one's for all the perlguts hackers.

One of the things I've wanted to do from time to time is have a sub remember it's arguments in a given context. (Mostly for Iterators) While iterators are easily implemented with closures (and I often use them but that's not the point), I wish there was a more natural DWIM way.

One trick I've done is to use save the args in a hash keyed by stringifying the return from caller(1). This works ok but if in a while loop or other looping construct, it fails if the loop is exited prematurely or if the lexical arguments are declared in an scope outside of the loop. In other words, if the loop is later executed again, it will start off from where it left off which is not DWIM.

One thought I had was, what if we could insert a value into the caller's lexical scope? If you used Padwalker's peek_my from within the sub, you could see if your lexical was set. If not you could do your initialization and set the my var in the caller's pad using a yet uncreated function called "poke_my", then (theoretically) when the loop is exited, the value would be destroyed (theoretically) so if it's reentered, the subroutine will correctly see that it has not been called from this "context" and reinitialize itself correctly (theoretically). :)

Does this sound um... sound? From my limited understanding of internals, I would think you could grab the caller's xcv_padlist, add the suitably mangled var name (to avoid collisions) to the scratchpad and set it's value. I thought I remembered reading that the pad isn't always de-allocated as an optimization. If that is true I would think that would foil this.

Any suggestions or comments would be appreciated.

-Lee

"To be civilized is to deny one's nature."

In reply to Iterating & Playing with Caller's Pads by shotgunefx

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.