加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Perl中调用名称为哈希值的子例程?

发布时间:2020-12-15 21:58:40 所属栏目:大数据 来源:网络整理
导读:$cat test.pluse strict;use warnings;sub route { print "hello,world!";}my %h;$h{'a'} = 'route';print "1n";$h{a};print "2n";$h{a}();print "3n";"$h{a}".();$perl test.plUseless use of hash element in void context at test.pl line 12.Useless
$cat test.pl
use strict;
use warnings;

sub route {
    print "hello,world!";
}

my %h;
$h{'a'} = 'route';

print "1n";
$h{a};

print "2n";
$h{a}();

print "3n";
"$h{a}".();
$perl test.pl
Useless use of hash element in void context at test.pl line 12.
Useless use of concatenation (.) or string in void context at test.pl line 18.
1
2
Can't use string ("route") as a subroutine ref while "strict refs" in use at test.pl line 15.
$

调用route()的正确方法是什么?

解决方法

您正尝试使用$h {a}作为符号引用.并且“使用严格”明确禁止这一点.如果你关闭严格模式,那么你可以这样做:
no strict;
&{$h{a}};

但最好的方法是在哈希中存储子程序的“实际”引用.

#!/usr/bin/perl

use strict;
use warnings;

sub route {
    print "hello,world!";
}

my %h;
$h{a} = &;route;

$h{a}->();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读