C
Christoph Haas
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
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