All,
I want to be able to take a class and in it's 'new'
subroutine do a check to ensure that each class has been given configuration
information for each subroutine it has defined locally within that class. The
problem I have is that I don't know how to differentiate between methods created
in a base class, methods created by included modules (through the use of the
use) and so on. I simply only want those that exist locally to the
class.
I came across the following code :-
sub methods
{ my $self = shift; my $class = ref($self) || $self; my @list; no strict 'refs'; print STDERR Dumper(keys
%{"${class}::"});
# Based on methods_via() in perldb5.pl foreach my $name (grep {defined &{${"${class}::"}{$_}}} keys %{"${class}::"}) { push @list, $name; } return \@list;
} But this doesn't quite work out, without having a
long list of 'ignore' keywords. And then it wouldn't be very future proof. Does
anybody have any ideas?
Regards
Marty
################################
Example of How I see this working, parent and base
classes:-
package ParentClass;
use base qw(BaseClass);
use vars qw(%methods);
%methods=(
'_load' => $configdata,
'list' => $configdata,
'anothersub' => $configdata,
);
package BaseClass;
sub new
{
blahusualblah
$self->_check_methods();
}
sub methods
{
#returns a list of methods for
this class, which doesn't include special ones like BEGIN, croak, AUTOLOAD
etc...
}
sub _check_methods
{
my $self=shift;
my $class=ref($self)||$self;
my
@methods=@{$self->methods()};
foreach my $method (@methods)
{
unless
(exists($self->methods->{$method}))
{
die
"No configuration supplied for $method for $class";
}
}
}
|
- Re: [Perl-unix-users] listing subroutines defined locally... Martin Moss
- Re: [Perl-unix-users] listing subroutines defined lo... Martin Moss
- [Perl-unix-users] [OT] Unsubscribe Timothy A. Canon