gpt4 book ai didi

Perl 获取每个对象调用的函数

转载 作者:行者123 更新时间:2023-12-05 01:03:35 28 4
gpt4 key购买 nike

我是 Perl 的新手,所以我不知道它是否可行。
我有兴趣创建一个可以捕获对其执行的所有调用的模块。

它的用法如下:

$object = new Foo;
$object->blah;

函数名称(因此在这种情况下,“blah”将被 Foo 咳嗽并作为字符串返回到屏幕)。

我不知道该怎么做是将被调用的函数名作为字符串捕获。

最佳答案

您可能想查看 AUTOLOADING

If you call a subroutine that is undefined, you would ordinarily get an immediate, fatal error complaining that the subroutine doesn't exist. (Likewise for subroutines being used as methods, when the method doesn't exist in any base class of the class's package.) However, if an AUTOLOAD subroutine is defined in the package or packages used to locate the original subroutine, then that AUTOLOAD subroutine is called with the arguments that would have been passed to the original subroutine


my $object = new Foo;
print $object->blah, "\n";

package Foo;
sub new { return bless {}, shift }

# catch-all function
sub AUTOLOAD {
return $AUTOLOAD;

}

输出 Foo::blah

关于Perl 获取每个对象调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549335/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com