'Refer to a subroutine with a scalar
Is it possible to get the example below to work so that the name of the subroutine is stored and called via a scalar variable?
use strict;
use warnings;
sub doit {
my ($who) = @_;
print "Make us some coffee $who!\n";
}
sub sayit {
my ($what) = @_;
print "$what\n";
}
my $action = 'doit';
$action('john');
Solution 1:[1]
According to an answer to this question: How can I elegantly call a Perl subroutine whose name is held in a variable?
You could try this:
__PACKAGE__->can($action)->('john');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chad |
