是否有可能获得特定Perl类的所有有效方法?是否有可能为特定的Perl类获取所有有效的方法?
我想操纵一个类的符号表并获得它的所有方法。我发现我可以通过$obj->can($method)
从非子程序中分离出子程序,但这并不完全符合我的想法。
以下回报:
subroutine, Property, croak, Group, confess, carp, File
然而,subroutine
不是方法,(只是一个子程序),并croak
,confess
,并都导入到我的包。
我真正想要打印的是:
Property,Group, File
但我会采取:
subroutine, Property,Group, File
下面是我的程序:
#! /usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
my $sections = Section_group->new;
say join ", ", $sections->Sections;
package Section_group;
use Carp;
sub new {
return bless {}, shift;
}
sub Add {
my $self = shift;
my $section = shift;
}
sub Sections {
my $self = shift;
my @sections;
for my $symbol (keys %Section_group::) {
next if $symbol eq "new"; # This is a constructor
next if $symbol eq "Add"; # Not interested in this method
next if $symbol eq "Sections"; # This is it's own method
push @sections, $symbol if $self->can($symbol);
}
return wantarray ? @sections : \@sections;
}
sub subroutine {
my $param1 = shift;
my $param2 = shift;
}
sub Group {
my $self = shift;
my $section = shift;
}
sub File {
my $self = shift;
my $section = shift;
}
sub Property {
my $self = shift;
my $section = shift;
}
然后,有时候人们实际上会从另一个包中导入一个方法(例如'使用Exporter'import';'而不是'@ISA = qw(Exporter);')。 – cjm
@cjm当然,但我想这不会经常发生,甚至可能是反模式。希望大多数人能够从OOP(它解决了许多命名空间问题)中精心分离程序性编程(包括导入)。没有办法知道导入的sub是否意味着成为一种方法,所以这必须足够好。 (等等,也许我们可以访问':method'属性......) – amon
_这很简单。不重要的?你在周末建造超快速发电机? Pervoc on ** svref_2object **:_引用任何Perl值,并将引用值转换为适当的B :: OP派生类或B :: SV派生类中的对象._我不知道什么这是在谈论。但是,它确实有效。我想现在是时候深入研究以前未发表的内容,并更多地改进我的Perl游戏。要么,要么学习Python。 –