Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Converting a coderef back to code

by citromatik (Curate)
on Apr 24, 2009 at 12:23 UTC ( #759823=note: print w/ replies, xml ) Need Help??


in reply to Converting a coderef back to code

I am wondering if it's possible to 'stringify' a coderef

Yes, this is possible:

my $stringify = B::Deparse->new->coderef2text($coderef);

See Is it useful to edit code at execution time? for an example of what you are trying to accomplish (I'm happy to see that you will give a "yes" to that question). Also, the module Sub::Compose uses this technique.

Hope this helps,

citromatik


Comment on Re: Converting a coderef back to code
Download Code
Re^2: Converting a coderef back to code
by oxone (Friar) on Apr 24, 2009 at 12:34 UTC
    Thanks! Just what I needed. I'm not actually going to edit or re-eval the code from the coderef, but getting it into string form means I can then work out whether the same code has been passed in before, such that a cached result can be returned. Conveniently, that means I also don't have to worry about the specific code formatting used by B::Deparse either :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://759823]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2015-07-07 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (89 votes), past polls