Perl获取每个对象的函数

前端之家收集整理的这篇文章主要介绍了Perl获取每个对象的函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是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相关文章