Thursday, 22 August 2013

Perl Returning a list of all valid methods for a class

Perl Returning a list of all valid methods for a class

Is it possible to get all valid methods for a particular class?
I am trying to manipulate the symbol table of a class and get all of its
methods. I found I can separate out the subroutines from the
non-subroutines via the $obj->can($method), but that doesn't do exactly
what I think it does.
The following returns:
However, subroutine isn't a method, (just a subroutine), and croak,
confess, and carp were all imported into my package.
What I really want to print out is:
Property,Group, File
But I'll take:
subroutine, Property,Group, File
Below is my program:
#! /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;
}

No comments:

Post a Comment