Objects containing objects

S

Steve

I have a class that takes in an object in its constructor, and stores
it as a member variable as follows:

# INSIDE CONSTRUCTOR
....
$self->{MY_OBJECT} = shift;
....


I can make calls to this member object's methods inside the
constructor, and they work fine:

....
print $self->{MY_OBJECT}->myMethod();
....


I have a getter method in this top-level object that returns the member
object:

sub getMyObject {
my $self = shift;
return $self->{MY_OBJECT};
}


However, when other code retrieves this object using the getter method,
it is unable to make method calls against that object. For example,
this code:

$topObject->getMyObject()->myMethod();

... generates an error message like this:

Can't call method "myMethod" without a package or object reference at
myScript.pl line 30.


Can anyone point me in the right direction as to what I might be
missing here? Thanks in advance!
 
T

Todd Wade

        I have a getter method in this top-level object that returns the member
object:

sub getMyObject {
        my $self = shift;
        return $self->{MY_OBJECT};

}

        However, when other code retrieves this object using the getter method,
it is unable to make method calls against that object.  For example,
this code:

$topObject->getMyObject()->myMethod();

        ... generates an error message like this:

Can't call method "myMethod" without a package or object reference at
myScript.pl line 30.

        Can anyone point me in the right direction as to what I might be
missing here?  Thanks in advance!

What you're trying to do works fine so I'm guessing you aren't really
returning the marshalled object from getMyObject. This code works
fine:

use warnings;
use strict;

package MyObject;

sub new {
my $class = shift;
bless { }, $class;
}

sub myMethod {
print "called me!\n";
}

package MyProxy;

sub new {
my $class = shift;
bless { MY_OBJECT => shift }, $class;
}

sub getMyObject { shift->{MY_OBJECT} }

package main;

my $proxy = MyProxy->new( MyObject->new );

prints "called me!" to STDOUT
$proxy->getMyObject->myMethod;
 
P

Paul Lalli

        I have a class that takes in an object in its constructor,and stores
it as a member variable as follows:

# INSIDE CONSTRUCTOR
...
$self->{MY_OBJECT} = shift;
...

        I can make calls to this member object's methods inside the
constructor, and they work fine:

...
print $self->{MY_OBJECT}->myMethod();
...

        I have a getter method in this top-level object that returns the member
object:

sub getMyObject {
        my $self = shift;
        return $self->{MY_OBJECT};

}

        However, when other code retrieves this object using the getter method,
it is unable to make method calls against that object.  For example,
this code:

$topObject->getMyObject()->myMethod();

        ... generates an error message like this:

Can't call method "myMethod" without a package or object reference at
myScript.pl line 30.

        Can anyone point me in the right direction as to what I might be
missing here?  Thanks in advance!

Somewhere between assigning to $self->{MY_OBJECT} and returning $self-
{MY_OBJECT}, you've changed it. It's no longer an object.

I suggest you use Data::Dumper to figure out what $self actually
contains in the getMyObject subroutine, and then trace through your
code to figure out where you went wrong.

I also suggest you read the posting guidelines, which will instruct
you to (among other things) post a short-but-complete program to
post. Reducing your code to such a short-but-complete program often
reveals the answer directly.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top