Objects containing objects

Objects containing objects

am 13.01.2008 06:53:13 von 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!

Re: Objects containing objects

am 13.01.2008 12:02:15 von Todd Wade

On Jan 13, 2:53=A0pm, Steve wrote:
> =A0 =A0 =A0 =A0 I have a getter method in this top-level object that retur=
ns the member
> object:
>
> sub getMyObject {
> =A0 =A0 =A0 =A0 my $self =3D shift;
> =A0 =A0 =A0 =A0 return $self->{MY_OBJECT};
>
> }
>
> =A0 =A0 =A0 =A0 However, when other code retrieves this object using the g=
etter method,
> it is unable to make method calls against that object. =A0For example,
> this code:
>
> $topObject->getMyObject()->myMethod();
>
> =A0 =A0 =A0 =A0 ... generates an error message like this:
>
> Can't call method "myMethod" without a package or object reference at
> myScript.pl line 30.
>
> =A0 =A0 =A0 =A0 Can anyone point me in the right direction as to what I mi=
ght be
> missing here? =A0Thanks 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 =3D shift;
bless { }, $class;
}

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

package MyProxy;

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

sub getMyObject { shift->{MY_OBJECT} }

package main;

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

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

Re: Objects containing objects

am 13.01.2008 13:39:51 von Paul Lalli

On Jan 13, 12:53=A0am, Steve wrote:
> =A0 =A0 =A0 =A0 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} =3D shift;
> ...
>
> =A0 =A0 =A0 =A0 I can make calls to this member object's methods inside th=
e
> constructor, and they work fine:
>
> ...
> print $self->{MY_OBJECT}->myMethod();
> ...
>
> =A0 =A0 =A0 =A0 I have a getter method in this top-level object that retur=
ns the member
> object:
>
> sub getMyObject {
> =A0 =A0 =A0 =A0 my $self =3D shift;
> =A0 =A0 =A0 =A0 return $self->{MY_OBJECT};
>
> }
>
> =A0 =A0 =A0 =A0 However, when other code retrieves this object using the g=
etter method,
> it is unable to make method calls against that object. =A0For example,
> this code:
>
> $topObject->getMyObject()->myMethod();
>
> =A0 =A0 =A0 =A0 ... generates an error message like this:
>
> Can't call method "myMethod" without a package or object reference at
> myScript.pl line 30.
>
> =A0 =A0 =A0 =A0 Can anyone point me in the right direction as to what I mi=
ght be
> missing here? =A0Thanks 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

Re: Objects containing objects

am 13.01.2008 13:46:02 von Frank Silvermann

"Todd Wade" wrote in message
news:4b92486b-dd1d-4e36-b24f-94ab68ccb84e@21g2000hsj.googleg roups.com...
On Jan 13, 2:53 pm, Steve wrote:
> 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;
}

#snip
I can't find this syntax anywhere. Not only am I weak on what shift does,
but also with the bless, followed by the curly braces. What happens with
this sub?


--
Reagan Revision

"We are being told that a competent, trustworthy president is someone
who brandishes his religion like a neon sign, loads a gun and goes out
hunting for beautiful winged creatures, and tries to imitate a past
president who, by the way, never shot a bird or felt the need to imitate
anybody."

~~ Patti Davis Is Not Flattered by GOP Candidates' Pale Imitations of
Her Father



----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----