I am creating a module which takes various parameters, including coderefs. Since my module also does some internal cacheing of return values where parameters have been seen before, I am wondering if it's possible to 'stringify' a coderef in order work out if the module has seen it before. So for example:
my $coderef = sub{ return "Hello"; }; # Possible somehow to manipulate $coderef # here, to get back to the code itself?
I presume the answer is no, and that the scalar value of the coderef - eg. CODE(0x225c2c) etc. - is just a memory location of no use in this context as it may be different if code is the same, or indeed the same even if the code is different.

My planned workaround is for the module to accept code as string values instead of coderefs, and then 'eval' them into coderefs inside the module, but this doesn't seem very elegant.

Is there a better way?


In reply to Converting a coderef back to code by oxone

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.