Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Problem with Inheriting a Super Class

by PyrexKidd (Monk)
on Sep 19, 2011 at 19:44 UTC ( [id://926797]=perlquestion: print w/replies, xml ) Need Help??

PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am having a problem with loading multiple plugins using module pluggable. It seems like the second plugin is overwriting the first plugin... Can anyone help me shed some light on what is going on here?
Thanks!

To Illustrate my issue, here is the output from my program. As you can see the name has been overwritten in after the "In Atest" heading

[root@elara object_problem]# perl snmp_monitor.pl starting snmp_monitor.pl Found Plugins: Plugin: SNMPMonitor::Plugin::Atest Plugin: SNMPMonitor::Plugin::Btest --> Plugins Dump: $VAR1 = 'SNMPMonitor::Plugin::Atest'; $VAR2 = 'SNMPMonitor::Plugin::Btest'; --> Objects Dump: $VAR1 = bless( { 'plugin_oid' => '0.0.0', 'name' => 'Atest', 'root_oid' => '.1.3.6.1.4.1.99999.', 'full_name' => 'SNMPMonitor::Plugin::Atest', 'full_oid' => '.1.3.6.1.4.1.99999.0.0.0' }, 'SNMPMonitor::Plugin::Atest' ); $VAR2 = bless( { 'plugin_oid' => '1.1.1', 'name' => 'Btest', 'root_oid' => '.1.3.6.1.4.1.99999.', 'full_name' => 'SNMPMonitor::Plugin::Btest', 'full_oid' => '.1.3.6.1.4.1.99999.1.1.1' }, 'SNMPMonitor::Plugin::Btest' ); In Atest Print Test: 0Full Name: SNMPMonitor::Plugin::Btest Name: Btest OID: 1.1.1 Root OID: .1.3.6.1.4.1.99999. Full OID: .1.3.6.1.4.1.99999.1.1.1 In Btest Print Test: 0Full Name: SNMPMonitor::Plugin::Btest Name: Btest OID: 1.1.1 Root OID: .1.3.6.1.4.1.99999. Full OID: .1.3.6.1.4.1.99999.1.1.1

Here is the associated code:

Listing: snmp_monitor.pl
#!/usr/bin/perl use Data::Dumper; use strict; use warnings; use common::sense; use lib '/root/snmp_monitor/trunk'; use SNMPMonitor; use SNMPMonitor::Plugin; BEGIN { print STDERR "starting $0\n"; } # Private vars my $root_oid = '.1.3.6.1.4.1.99999.'; my $monitor = SNMPMonitor->new; my @plugins = $monitor->plugins(); #print "Found Plugins: \n" . Dumper @plugins; print STDERR "\nFound Plugins: \n"; print STDERR "Plugin: $_ \n" foreach @plugins; print STDERR "\n"; my @objects; push @objects, $_->new($root_oid) foreach @plugins; print STDERR "\n\n--> Plugins Dump:\n" . Dumper @plugins; print STDERR "\n\n--> Objects Dump:\n" . Dumper @objects; foreach my $obj (@objects) { print STDERR "Print Test; \n" . $obj->print_test; print STDERR "Full Name: " . $obj->full_name . "\n"; print STDERR "Name: " . $obj->name . "\n"; print STDERR "OID: " . $obj->plugin_oid . "\n"; print STDERR "Root OID: " . $obj->root_oid . "\n"; print STDERR "Full OID: " . $obj->full_oid . "\n\n"; }
listing SNMPMonitor.pm
package SNMPMonitor; use Module::Pluggable require => 1; sub new { my $class = shift; my $self = {}; bless $self, $class; } sub evaluate_plugin { } 1; __END__
listing SNMPMonitor/Plugin.pm
package SNMPMonitor::Plugin; use common::sense; #use NetSNMP::ASN (':all'); 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 => '', full_oid => '', }; bless $self, $class; # create accessor methods for defined parameters for my $datum (keys %{$self}) { no strict "refs"; *$datum = sub { shift; # XXX: ignore calling class/object $self->{$datum} = shift if @_; return $self->{$datum}; }; } $self->set_oid(); return $self; } # Set the OID using plugin defined OID. sub set_oid { my $self = shift; $self->plugin_oid($self->set_plugin_oid); $self->full_oid($self->root_oid . $self->plugin_oid); } 1; __END__
listing: SNMPMonitor/Plugin/Atest.pm
package SNMPMonitor::Plugin::Atest; use strict; use warnings; #use common::sense; use NetSNMP::ASN (':all'); # use Smart::Comments; my @ISA = qw(SNMPMonitor::Plugin); sub set_plugin_oid {'0.0.0'}; sub monitor { use Data::Dumper; print STDERR "--> In Object \nDumper: " . Dumper @_; my $class = shift; my ($handler, $registration_info, $request_info, $requests) = @_; my $request; my $this_request = $requests->next(); $requests->setValue(ASN_OCTET_STR, "Test Successful"); } sub print_test { my $self = shift; print STDERR "In " . $self->full_name . "\n"; } 1; __END__
for completeness, here is BTest, however, the only difference is the OID.
listing: SNMPMonitor/Plugin/Btest.pm
package SNMPMonitor::Plugin::Btest; use strict; use warnings; #use common::sense; use NetSNMP::ASN (':all'); # use Smart::Comments; my @ISA = qw(SNMPMonitor::Plugin); sub set_plugin_oid {'1.1.1'}; sub monitor { use Data::Dumper; print STDERR "--> In Object \nDumper: " . Dumper @_; my $class = shift; my ($handler, $registration_info, $request_info, $requests) = @_; my $request; # do { # print STDERR "refs: ",join(", ", ref($handler), ref($registra +tion_info), # ref($request_info), ref($requests)),"\n"; # print STDERR "processing a request of type " . $request_info- +>getMode() . "\n"; # }; my $this_request = $requests->next(); $requests->setValue(ASN_OCTET_STR, "BTest Successful"); } sub print_test { my $self = shift; print STDERR "In " . $self->{'name'} . "\n"; my $a = 0; ### @_; } 1; __END__

When built in the above structure I receive the posted output, when BTest.pm is named to BTest.pm.old the ATest object works correctly.

Replies are listed 'Best First'.
Re: Problem with Inheriting a Super Class
by chromatic (Archbishop) on Sep 19, 2011 at 20:21 UTC

    Here's at least one problem:

    my @ISA = qw(SNMPMonitor::Plugin);

    @ISA must be a package variable. Alternately (and more cleanly), use parent:

    use parent 'SNMPMonitor::Plugin';

    Improve your skills with Modern Perl: the free book.

      Hello Chromatic,

      Thanks for your reply. I tried both 'our @ISA' and 'my @ISA' without change to the apparent behavior. Now trying with use parent I am receiving the same results.

      What really blows my mind, is when I dump the object itself it shows the correct parameters, when I use the object methods to call the parameters is when I am seeing incorrect results.

      With that in mind, could this be what is causing my problems?

      # create accessor methods for defined parameters for my $datum (keys %{$self}) { no strict "refs"; *$datum = sub { shift; # XXX: ignore calling class/object $self->{$datum} = shift if @_; return $self->{$datum}; }; }

      Since this is loaded in the SNMPMonitor::Plugin package, but then inherited to SNMPMonitor::Plugin::Atest (and subsequently Btest) could Btest be overwriting the methods in the symbol table?
      After I just typed that I attempted to remove the line :

      shift; # XXX: ignore calling class/object
      which had no effect.

      Even better, (to add to what blows my mind), when I use the methods to access the data I am seeing this bug, when I access the variables directly, I do not have this issue.
      ...
      This must mean that it is an issue with my method of creating accessors for the object, right?

        With that in mind, could this be what is causing my problems?

        I skimmed that part the first time, but you're right. It's definitely causing you problems. Here's a hint; this is a closure:

        *$datum = sub { shift; # XXX: ignore calling class/object $self->{$datum} = shift if @_; return $self->{$datum}; };

        In other words, every time you create a new object, you create a new closure bound to that new $self. Every time you call an accessor or mutator on an existing object, that accessor or mutator uses the values of the most recently created object.

        The immediate fix is to look at the XXX comment and make that generated method not close over $self. A better fix overall is not to generate (and clobber previously generated!) accessor and mutator methods in your constructor.


        Improve your skills with Modern Perl: the free book.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://926797]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found