This is definitely off the beaten track, so it'll probably help if I start with why I want to do something this screwy.

I'm extending Cons, a Perl-based substitute for Make, to support generating a target file by calling a Perl code ref instead of an external command.

Cons will rebuild a target file if the file's build command changes. Cons tracks this by adding the command line into an MD5 signature for the target file. If the command line changes, the signature changes and the file gets rebuilt.

Ideally, this should work the same for code refs. If the Perl code used to generate a target file changes, the file should get rebuilt. So how do I figure out if the code changed since the last build?

What I'd like to do is be able to use the same MD5 signature mechanism on the "contents" of the code ref--maybe, for example, treat the compiled code as a byte string and generate an MD5 signature on that. Something like that would be cleanest for the Cons architecture, but the only things I can figure out to do with a code ref are:
# call it: $coderef->(); # not what I want, obviously # stringify it: "$coderef" # generates "CODE(0x8405f54)", e.g.
Note that I can't use the representation of the memory address itself, because that will likely be different each time Cons is run. If I knew how long the compiled code ended up being, I could theoretically generate the signature on N bytes starting at the supplied address, but I don't see how to do that, either.

If there isn't any magic that will let me at the compiled "contents" of a code ref in this way, my workaround will be to use the MD5 signature of the entire config file in which the ref appears. That would have the drawback, though, of forcing anything with a code ref to be rebuilt anytime the config file changes, so I'd be grateful for any suggestions about other ways to try to do this...

In reply to how can I access code ref contents, not call it? by knight

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.