确定 Perl 代码引用的子例程名称 [英] Determining the subroutine name of a Perl code reference

查看:45
本文介绍了确定 Perl 代码引用的子例程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定 Perl 代码引用的子程序名称?我还想区分命名子程序和匿名子程序.

How would one determine the subroutine name of a Perl code reference? I would also like to distinguish between named and anonymous subroutines.

感谢这个问题我知道如何打印代码出来了,但我还是不知道怎么取名字.

Thanks to this question I know how to print out the code, but I still don't know how to get the name.

例如,我想从以下内容中获取inigo_montoya":

For example, I'd like to get 'inigo_montoya' from the following:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;
$Data::Dumper::Deparse = 1;

my $sub_ref = \&inigo_montoya;

print Dumper $sub_ref;



# === subroutines ===

sub inigo_montoya {
  print <<end_quote;
I will go up to the six-fingered man and say, "Hello. My name is Inigo
Montoya. You killed my father. Prepare to die."';
end_quote
}

推荐答案

为什么不问,编译器看到了什么?(它会在匿名订阅上返回 __ANON__).

Why not ask, what the compiler sees? (It would return __ANON__ on anonymous subs).

#!/usr/bin/perl

use strict;
use warnings;

my $sub_ref = \&inigo_montoya;


use B qw(svref_2object);
my $cv = svref_2object ( $sub_ref );
my $gv = $cv->GV;
print "name: " . $gv->NAME . "\n";


sub inigo_montoya {
    print "...\n";
}

这篇关于确定 Perl 代码引用的子例程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆