Namuna
Namuna

Reputation: 1030

Pass/Access Object Attributes to/from another Class

Issue: Unable to access parent object attributes

Verification.pm: (Parent class)

package Verification;
use Verification::Proid;

sub Proid
{
    my $self = shift;
    print Dumper($self);
    my $result = Verification::Proid->validate($self);

    return $result; 
}

Dumper result

$VAR1 = bless( {
    'event_name' => 'validate',
    'Verification_Type' => 'Proid',
    'Verification_Value' => 'ecmetric',
    'xml_request' => bless( do{\(my $o = 148410616)}, 'XML::LibXML::Document' ),
    'Verification_Options' => [
                                {
                                '2' => 'UNIX'
                                }
                              ],
    'Verification_ID' => '3'
    }, 'Verification' );

Proid.pm: (Child class)

package Verification::Proid;
our @ISA = qw(Verification);

sub validate 
{
    my $self = shift;
    print Dumper($self);
    my $result;

    foreach my $validation_type ( @$self->{Verification_Options} )
    {
        do stuff...
    }
}

Dumper result

$VAR1 = 'Verification::Proid';

What am I doing wrong that the child class isn't properly getting all the attributes from the object passed to it?

Thank you!

Upvotes: 1

Views: 475

Answers (2)

Namuna
Namuna

Reputation: 1030

Thanks for pointing out the flaws @hobbs, pretty obvious I need to read up more about OO Perl...Until I get a better handle on it, I figured out a functional workaround by utilizing Class::Singleton in Verification.pm

Verification.pm

package Verification;
use Verification::Proid;
use Class::Singleton;

#Instantiate the object as a singleton

sub Proid
{
    return Verification::Proid->validate();
}

Proid.pm

package Verification::Proid;
our @ISA = qw(Verification);

sub validate 
{
    my $self = Verification->instance;
    print Dumper($self);

    foreach my $validation_type ( @{$self->{Verification_Options}} )
    {
        do stuff...
    }
}

Dumper result is what I needed

$VAR1 = bless( {
    'event_name' => 'validate',
    'Verification_Type' => 'Proid',
    'Verification_Value' => 'ecmetric',
    'xml_request' => bless( do{\(my $o = 148410616)}, 'XML::LibXML::Document' ),
    'Verification_Options' => [
                                {
                                '2' => 'UNIX'
                                }
                              ],
    'Verification_ID' => '3'
    }, 'Verification' );

Upvotes: 0

hobbs
hobbs

Reputation: 239791

  1. your call syntax is wrong. Verification::Proid->validate($self) is calling a method on the class, not on $self.

  2. The concept is wrong. A parent class shouldn't be calling things in child classes by name; it completely defeats the purpose of having classes.

  3. Your object doesn't belong to the supposed child class; it's blessed into Verification, not Verification::Proid. If it was actually an instance of Verification::Proid you could just call $self->validate on it, even from within the parent class.

Upvotes: 3

Related Questions