Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: How to call a sub-routine ref

by blue_cowdawg (Monsignor)
on Oct 22, 2012 at 12:47 UTC ( #1000324=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to call a sub-routine ref
in thread How to call a sub-routine ref

Did you actually test that code?

$ cat greengaroo.pl use strict; my $ref = sub { print "Hello World!\n"; }; # Both are equivalent $ref->(); &{ref}(); # But which one is best? That is the question! $ perl greengaroo.pl Hello World! Undefined subroutine &main::ref called at greengaroo.pl line 9.

your second call fails.

If you fix this by rewriting it thusly:

&{$ref}();
there is still the open question of "which is better?" From a readability standpoint I'd vote for the latter rather than the former. Others may disagree but that is the essence of TIMTOWTDI. If you feel there may be a performance issue involved here have you tried profiling the code?


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re^3: How to call a sub-routine ref
Select or Download Code
Re^4: How to call a sub-routine ref
by greengaroo (Hermit) on Oct 22, 2012 at 14:27 UTC

    You are right! I forgot a $... sorry about that! Thanks for pointing it!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2015-07-05 18:28 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 (67 votes), past polls