如何检查Perl标量是否包含对某个子例程的引用?

Zai*_*aid 8 perl

换句话说,我如何检查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" ;
Run Code Online (Sandbox Code Playgroud)

jpa*_*cek 11

您可以使用普通(数字)相等(==),就像所有引用一样:

Perl> $selected == \&foo


Perl> $selected == \&bar


Perl> $selected == \&baz
1
Run Code Online (Sandbox Code Playgroud)

在这里生活

当引用被过载==0+(对于coderefs来说不太可能)的东西祝福时,这会中断.在那种情况下,你要比较Scalar::Util::refaddr($selected).

来自man perlref:

使用引用作为数字会生成一个整数,表示其在内存中的存储位置.要做的唯一有用的事情是用数字比较两个引用,看它们是否引用相同的位置.

      if ($ref1 == $ref2) {  # cheap numeric compare of references
           print "refs 1 and 2 refer to the same thing\n";
       }
Run Code Online (Sandbox Code Playgroud)