Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Accessing parent objects

Reply
Thread Tools

Accessing parent objects

 
 
Christoph Haas
Guest
Posts: n/a
 
      10-05-2003
Hi, group...

After hours of reading kilobytes of documentation I'm desperate enough
to bother you.

I have an object (child) created within another object (parent).
(See below for the "trivial" source code.) Methods of the parent package
can easily handle children objects like $parent->{'children'}. But is
there a way to do this vice-versa? Assume I'm a child and I want to know
what other children there are. I need a handle to "go up" the object
tree to check the parent object.

There are two possible solution I can think of:

a) Give each child a handle to its parents like
$parent->new_child($parent);
(This could break if the object is not a member of "parent"
and the children assume they have parent.)
b) Use global variables for children (then how do I access them
from inside of each package? can I "tie" them somehow and use
them simultaneously from every children object?)

What is the best way to handle this?

================================================
Parent.pm:
==========
package Parent;

use Child;

sub new
{
my $package = shift;
my $self = { };

my %children = ();
$self->{'children'} = \%children;

bless($self, $package);
return $self;
}

sub new_child
{
my $self = shift;
my $name = shift;
$self->{'children'}->{$name} = new Child;
}

=========
Child.pm:
=========
package Child;

sub new
{
my $package = shift;
my $self = { };

bless($self, $package);
return $self;
}
================================================

Thanks for your ideas...

Christoph

 
Reply With Quote
 
 
 
 
Christoph Haas
Guest
Posts: n/a
 
      10-05-2003
Update... (argh, Mozilla ate my first reply)

I just came across a nice tutorial on variable scopes at
http://perl.plover.com/FAQs/Namespac...urrent_Package
which describes very clearly how lexicals and packages work.

I'm now convinced that the best way is declaring global variables
in the main package and then just refer to them via $main:arent
(or short: $:arent) from within the "child" objects from the
"Child" package.

If other have fresh ideas - let me know.

Christoph



 
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
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Accessing an objects parent class eggie5@gmail.com Javascript 1 07-01-2007 04:02 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Accessing a container objects state from aggregated objects Derek Basch Perl Misc 4 08-16-2006 09:04 AM
What does sender.Parent.Parent.Cells() reference? Frustrating... Roy ASP .Net 2 02-11-2005 09:00 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