是否可以获得特定 Perl 类的所有有效方法?
我正在尝试操作一个类的符号表并获取它的所有方法。我发现我可以通过 将子例程与非子例程分开$obj->can($method)
,但这并不完全符合我的想法。
以下返回:
subroutine, Property, croak, Group, confess, carp, File
但是,subroutine
is 不是一个方法,(只是一个子例程), and croak
, confess
, andcarp
都被导入到我的包中。
我真正想打印的是:
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;
}