Hello Monks,
I am attempting to code a plug-in driven application. (The initial outline is here but is largely irrelevant to the issue at hand.)
Since the construction of Objects is generic and almost identical, it makes the most sense to use a base class then inherit the constructor methods into the plugin(subclass); I would like to make crating plugins as easy as possible by removing all redundant code.
I have this working mostly, the problem is, I am required to have the 'set_id' method in every subclass I create.
Here is what I mean:
File: test.pl
#!/usr/bin/perl
use common::sense;
use lib './';
package main;
my $monitor_plugins = SNMPMonitor->new();
my @plugins = $monitor_plugins->plugins();
print "Plugin: $_ \n" foreach @plugins;
my $c = SNMPMonitor::Plugin::Atest->new('1.2.3');
use Data::Dumper;
print Dumper $c;
print "\n";
print "Name: " . $c->name . "\n";;
print "Full Name: " . $c->full_name . "\n";
print "OID: " . $c->plugin_oid . "\n";
#$c->set_oid();
# print "\n";
# print "Name: " . $c->name . "\n";;
# print "Full Name: " . $c->full_name . "\n";
# print "OID: " . $c->r_oid . "\n";
print Dumper $monitor_plugins;
package SNMPMonitor;
use Module::Pluggable require => 1; #instantiate => 'new';
sub new {
my $obj_class = shift;
my $root_oid = shift;
my $class = ref $obj_class || $obj_class;
my ($name) = $class =~ /::Plugin::(.*)/;
my $self = {
name => $name,
full_name => $class,
root_oid => $root_oid,
plugin_oid => '',
};
bless $self, $class;
for my $datum (keys %{$self}) {
no strict "refs";
*$datum = sub {
shift; # XXX: ignore calling class/object
$self->{$datum} = shift if @_;
return $self->{$datum};
};
}
eval {$self->set_oid()} if $self->can('set_oid');
return $self;
}
#sub set_oid {
# my $self = shift;
# no strict "vars";
# $self->plugin_oid($plugin_oid);
#}
File: ./SNMPMonitor/Plugin/Atest.pm
package SNMPMonitor::Plugin::Atest;
use common::sense;
our @ISA = qw(SNMPMonitor);
my $plugin_oid = '1.2.5';
sub set_oid {
my $self = shift;
$self->plugin_oid($plugin_oid);
}
1;
__END__
If I comment out the set_oid method in the subclass SNMPMonitor::Plugin::Atest and uncomment it in the base class SNMPMonitor I get an error that $plugin_oid doesn't exist in SNMPMonitor. (hence the no strict "vars"). Authors note: This warning is indicative that I haven't done something right, I'm just not sure what.
It is very important to set the OID in the plugin subclass and not the SNMPMonitor super class because each monitor will be responsible for their own OID space; not to mention I don't want to have to maintain an additional database of OID -> plugin mappings, nor do I believe that it is necessary.
Thanks in advance,
Any help is greatly appreciated.
(Note: In the final version every thing in package SNMPMonitor will be abstracted to SNMPMonitor.pm, it is here to ease development so I'm not in and out of the two files all the time.)