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.)


In reply to Inherited Object Constructor Question by PyrexKidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.