Perl:获取特定类的对象 [英] Perl : Get the objects of a particular class

查看:62
本文介绍了Perl:获取特定类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 perl 中获取特定类的对象?

Is there a way to fetch the objects of a particular class in perl ?

示例:

use <class1>;
use <class2>

sub Main {

    my $call1 = <class1>->new(<ARGS>)->(<Routine call>);
    my $call2 = <class1>->new(<ARGS>)->(<Routine call>);
    my $call3 = <class1>->new(<ARGS>)->(<Routine call>);

    .
    .
    .
    my $call4 = <class2>->new(<ARGS>)->(<Routine call>);
}

能否获取的对象?

$call1
$call2
and
$call3

推荐答案

这里有几点提示:如何列出所有变量在给定的范围内?

使用此工具:http://search.cpan.org/dist/PadWalker/PadWalker.pm 您可以访问给定范围内的所有包和词法变量.

With this tool: http://search.cpan.org/dist/PadWalker/PadWalker.pm you can access all of the package and lexcial variables in a given scope.

或者您也可以直接访问给定范围的符号表:keys %{'main::'}

Or you can access the symbol table also directly for a given scope: keys %{'main::'}

您可以使用 ref() 获取变量的类型/类.http://perldoc.perl.org/functions/ref.html

And you can get the type/class of a variable with ref(). http://perldoc.perl.org/functions/ref.html

我认为您的问题没有直接的解决方案.

I don't think there are direct solutions for your problem.

也许您可以扩展该类并将实例收集到覆盖构造函数中的哈希表中.

Perhaps you could extend the class and collect the instances to a hash table in an overridden constructor.

这篇关于Perl:获取特定类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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