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;
|
|---|