Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Calling a method of the calling object ...

Reply
Thread Tools

Calling a method of the calling object ...

 
 
why-em-jay
Guest
Posts: n/a
 
      09-15-2005
Hi,
My title is probably not clear, here it goes:

I have package A and I have created an object from that package in my
main prog.
This object has properties, and one of them is a new object from
package B.
Afaik, this is the HAS-A relationship.

Now my issue is to call a method from package A from within a method in
package B...
I could give the object (created off package A) as an argument, but I
guess there might be a nicer way to achieve that ? or my design is
crappy ??

Ex:

in main prog:
--------------
$obj = Server->new();
$obj->createSession();
$obj->doSomething();


in package Server:
-------------------
sub createSession {
my $self=shift;
$self->{session} = new Session();

}

sub doSomething {
my $self=shift;
$self->{session}->doTask();
}

sub returnServerDetails {
my $self = shift;
# here i return a property of the created object ($obj)
return $self->{one_property};
}


in package B:
--------------
sub doTask {
my $self =shift;
# here i want to call server::returnServerDetails, but i want it
to be
# aware that i'm talking about the object that created me ($obj)
# because i need some properties about the Server that
# created me... and I need to call several methods from package
Server
# which will return info about it.
}



Thanks for your help !
regards,
why-em-jay

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-15-2005
why-em-jay wrote:
> Hi,
> My title is probably not clear, here it goes:
>
> I have package A and I have created an object from that package in my
> main prog.
> This object has properties, and one of them is a new object from
> package B.
> Afaik, this is the HAS-A relationship.
>
> Now my issue is to call a method from package A from within a method in
> package B...
> I could give the object (created off package A) as an argument, but I
> guess there might be a nicer way to achieve that ? or my design is
> crappy ??


I'm not sure I would call it "crappy", but your design does need some
rethinking.

The Session object has absolutely no idea that a reference to it is
stored inside a Server object. There's no way for it to have that
knowledge. Consider:
my $server = new Server;
my $server2 = new Server;
$server->createSession();
# $server->{session} is a Session object.
$server2->{session} = $server->{session}.

Now you've got a reference to a single Session object stored in two
different Server objects. Under your design, which Server would the
Session object's doTask method want to be working with?

The central issue here is that while there is a "has a" relationship
(as you called it) from the Server object to the Session object, there
is no corresponding backwards relationship from the Session to the
Server. No object of any kind has any intrinsic way of knowing where
every reference to it is stored.

The obvious way to get the results you're looking for is the method you
suggested:

$server->{session}->doTask($server);

Paul Lalli

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      09-15-2005
"why-em-jay" <> wrote:

> Hi,
> My title is probably not clear, here it goes:
>
> I have package A and I have created an object from that package in my
> main prog.
> This object has properties, and one of them is a new object from
> package B.
> Afaik, this is the HAS-A relationship.


correct.

> Now my issue is to call a method from package A from within a method in
> package B...


The only clear way I can think of this is to have A register a call back
with B.

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-15-2005
Paul Lalli <> wrote in comp.lang.perl.misc:
> why-em-jay wrote:
> > Hi,
> > My title is probably not clear, here it goes:
> >
> > I have package A and I have created an object from that package in my
> > main prog.
> > This object has properties, and one of them is a new object from
> > package B.
> > Afaik, this is the HAS-A relationship.
> >
> > Now my issue is to call a method from package A from within a method in
> > package B...
> > I could give the object (created off package A) as an argument, but I
> > guess there might be a nicer way to achieve that ? or my design is
> > crappy ??

>
> I'm not sure I would call it "crappy", but your design does need some
> rethinking.
>
> The Session object has absolutely no idea that a reference to it is
> stored inside a Server object. There's no way for it to have that
> knowledge. Consider:
> my $server = new Server;
> my $server2 = new Server;
> $server->createSession();
> # $server->{session} is a Session object.
> $server2->{session} = $server->{session}.
>
> Now you've got a reference to a single Session object stored in two
> different Server objects. Under your design, which Server would the
> Session object's doTask method want to be working with?


Well, the intent seems to be that a session is handled by only one
server. Then the program logic would be so that the case you mention
doesn't happen.

> The central issue here is that while there is a "has a" relationship
> (as you called it) from the Server object to the Session object, there
> is no corresponding backwards relationship from the Session to the
> Server. No object of any kind has any intrinsic way of knowing where
> every reference to it is stored.


This, of course, is right, no matter what the program logic.

> The obvious way to get the results you're looking for is the method you
> suggested:
>
> $server->{session}->doTask($server);


One could give the Session object a field that points back to the "its"
server. Since Session->new is called from a Server method, that is
easily done:

sub createSession {
my $server = shift;
$server->{ session} = Session->new( $server);
# ...
$server;
}

Session->new simply stores the server away for later use in

$server->{ session}->doTask;

which now doesn't need the parameter.

The circular reference that is created may pose a problem. In a long-
running program that processes many servers and sessions, it may be
necessary to weaken one of the refs and have a DESTROY method in
Session and/or Server. That lessens the fun somewhat.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb question: in documentation, why Object#method, and not Object.method ? Elf M. Sternberg Ruby 15 07-29-2009 01:20 AM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
problem calling method from parent of an object if object relayson template arguments of current template class? =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?= C++ 2 03-02-2006 05:45 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Problems with JNI: calling a Java method from native method. Jabel D. Morales - VMan of Mana Java 1 08-01-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57