Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

•Re^5: How to de-reference a coderef?

by merlyn (Sage)
on Dec 09, 2004 at 18:02 UTC ( #413633=note: print w/ replies, xml ) Need Help??


in reply to Re^4: How to de-reference a coderef?
in thread How to de-reference a coderef?

To make that even simpler, you could write a subroutine called Sub that takes a coderef argument and records its file and line number and returns an object that when used as a coderef invokes the coderef, but when used as a string returns the filename/linenumber of creation. How's that for slick? I may even have to code that up for a snippet. Usage would be something like:

my $sub = Sub { my $x = shift; print $x }; $sub->("foo"); # prints foo print "that was from $sub\n";
Pretty simple to do.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


update: see Track the filename/line number of an anonymous coderef.


Comment on •Re^5: How to de-reference a coderef?
Download Code
Re^6: How to de-reference a coderef?
by Anonymous Monk on Dec 09, 2004 at 20:50 UTC
    I'd like to see this, namely as in my head I can only imagine the syntax of...

    my $sub = Sub sub { my $x = shift; print $x };

    Being that I thought { } was the anonymous hash composer. I'm really curious about this though, and syntax reduction in general. Writing things that act like Ruby/Groovy blocks is kind of ugly when you have to use the sub keyword all of the time.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-07-31 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (249 votes), past polls