换句话说,我如何检查 coderef “平等”?
smartmatch 运算符由于明显的原因不起作用(将其视为CODE->(ANY)
),但我已将其包含在示例中以显示我所追求的:
use strict;
use warnings;
use feature 'say';
sub pick_at_random {
my %table = @_;
return ( values %table )[ rand( keys %table ) ];
}
my %lookup = ( A => \&foo,
B => \&bar,
C => \&baz );
my $selected = pick_at_random( %lookup );
say $selected ~~ \&foo ? "Got 'foo'" :
$selected ~~ \&bar ? "Got 'bar'" :
$selected ~~ \&baz ? "Got 'baz'" :
"Got nadda" ;