如何在Perl中找到从包中继承的所有包? [英] How can I find all the packages that inherit from a package in Perl?
问题描述
我有许多不同的网站,我从中下载数据并按摩到其他格式(使用Perl)以便在工作中使用,这些网站都是从一个Perl脚本运行的,如下所示:
#! / usr / bin / perl
使用严格;
使用My :: Package1;
使用My :: Package2;
my $ p1 = My :: Package1-> new;
$ p1->下载;
my $ p2 = My :: Package2-> new;
$ p2->下载;
依此类推。目前,每个 My :: Package
都是自己的包;它不会从基础包或任何东西继承。我打算使用 Moose
重新编写它们,我希望每次添加新软件包时都不必编辑运行下载的Perl脚本,是一种查找从基础包继承的包的方法,然后在循环实例化每个包并进行下载,有点像:
#! / usr / bin / perl
使用严格;
我的$ pname(packages_that_inherit_from(My :: Package)){
my $ package = $ pname-> new;
$ package->下载;
}
它可能或其他东西可能吗?
TIA
使用 Moose
的 Class :: MOP
支持你可以找到分配给每个类的子类(在那个时间点)。 / p>
来自 Class :: MOP :: Class
docs:
$ metaclass-> subclasses
这将返回此类的所有子类的列表,甚至是间接子类。
$ metaclass-> direct_subclasses
这将返回此类的直接子类列表,其中不包含间接子类。
例如,如果我们构建这些课程:
{
package Root;
使用驼鹿;
使用namespace :: clean -except => 元;
sub baz {说'some root thingy'}
子下载{说从下载。 __PACKAGE__}
}
{
包NodeA;
使用驼鹿;
扩展'Root';
使用namespace :: clean -except => 元;
子下载{说从下载。 __PACKAGE__}
}
{
包NodeA1;
使用驼鹿;
扩展'NodeA';
使用namespace :: clean -except => 元;
子下载{说从下载。 __PACKAGE__}
}
然后以您的示例为基础,我们可以这样做:
我的$ pname(Root-> new-> meta-> direct_subclasses){
my $ package = $ pname->新建;
$ package->下载;
}
#=> 从NodeA下载
以上运行 NodeA->下载
。将上面的内容更改为 meta->子类
也会运行 NodeA1->下载
。
/ I3az /
I have a number of different sites that I download data from and massage into other formats (using Perl) for use at work, that are all run from one Perl script kinda like so:
#! /usr/bin/perl
use strict;
use My::Package1;
use My::Package2;
my $p1 = My::Package1->new;
$p1->download;
my $p2 = My::Package2->new;
$p2->download;
and so on and so forth. At the moment each My::Package
is its own package; it doesn't inherit from a base package or anything. I am planning to re-write them using Moose
and I was hoping that rather than having to edit the Perl script that runs the download each time a new package is added, there might be a way of finding packages that inherit from a base package, and then in a loop instantiate each and do the downloading, kinda like so:
#! /usr/bin/perl
use strict;
for my $pname (packages_that_inherit_from("My::Package")) {
my $package = $pname->new;
$package->download;
}
Is it, or something ilke it, possible?
TIA
Using Moose
's Class::MOP
underpinning you can find the subclasses assigned to each class (at that point in time).
From Class::MOP::Class
docs:
$metaclass->subclasses This returns a list of all subclasses for this class, even indirect subclasses.
$metaclass->direct_subclasses This returns a list of immediate subclasses for this class, which does not include indirect subclasses.
So for example if we build these classes:
{
package Root;
use Moose;
use namespace::clean -except => 'meta';
sub baz { say 'Some root thingy' }
sub download { say "downloading from " . __PACKAGE__ }
}
{
package NodeA;
use Moose;
extends 'Root';
use namespace::clean -except => 'meta';
sub download { say "downloading from " . __PACKAGE__ }
}
{
package NodeA1;
use Moose;
extends 'NodeA';
use namespace::clean -except => 'meta';
sub download { say "downloading from " . __PACKAGE__ }
}
Then using your example as a basis we can do this:
for my $pname ( Root->new->meta->direct_subclasses ) {
my $package = $pname->new;
$package->download;
}
# => "downloading from NodeA"
So above runs NodeA->download
. Changing above to meta->subclasses
would also run the NodeA1->download
.
/I3az/
这篇关于如何在Perl中找到从包中继承的所有包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!