in reply to How do I call a sub using a variable as its name, objectively
Calling a sub:
my $sub = \&$name; $sub->( @args )
Optionally, you can add the following:
die "No sub named `$name`" if !defined( &$sub );
Note that \&$name will create an undefined sub if it doesn't already exists.
Calling a method:
$invocant->$name( @args )
Calling a method (alternative):
my $method = $invocant->can( $name ); $invocant->$method( @args )
Optionally, you can add the following to the alternative approach:
die "No method named `$name`" if !$method;
Strict refs prevents none of these.
|
|---|