Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: call external sub ref from method in single line (some golf)

by mr_ron (Chaplain)
on Oct 05, 2015 at 15:57 UTC ( [id://1143831]=note: print w/replies, xml ) Need Help??


in reply to Re^2: call external sub ref from method in single line
in thread call external sub ref from method in single line

Some golf code can have the advantage of clarifying the question by being read quickly. It may be more for quick examples because in production code spelling things out more may make for easier debugging and modification.

It later occurred to me that you don't always need a subroutine reference to call outside an object or package although it could be useful. If the call is not object dependent you can just specify the name of the other package like "main::hello()".

I included my simplified/golfed example below:

package C; sub new { my $self = shift; my $class = ref($self) || $self; return bless {sub_ref => shift}, $class; } sub doit { $_[0]->{sub_ref}(); # you don't always need a reference to call outside of an object main::hello(); } package main; sub hello { print "hello\n"; } C->new(\&hello)->doit;
Ron

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found