J
julians
Here's an interesting simplified version of a problem I have:
I have parent objects that have a list of child objects.
I want the list of children that belong to each parent to belong to
that instance of the parent object only. What I'm getting is a shared
list among the parent objects and I don't want that.
Here's the code...
#Beginning of file Child.pm
package Child;
sub new {
my $invocant = shift;
my $self = bless({}, ref $invocant || $invocant);
$self->init();
return $self;
}
sub init {
my $self = shift;
$self->id("0");
$self->name("unnamed");
}
for my $field (qw(id name)) {
my $slot = __PACKAGE__ . "::$field";
no strict "refs";
*$field = sub {
my $self = shift;
$self->{$slot} = shift if @_;
return $self->{$slot};
};
}
1;
#End of file Child.pm
#Beginning of file Parent.pm
package Parent;
use Child;
sub new {
my $invocant = shift;
my $self = bless({}, ref $invocant || $invocant);
$self->init();
return $self;
}
sub init {
my $self = shift;
$self->id("0");
$self->name("unnamed");
$self->childList({});
#running this will destroy all the parent's children,
#not just this instances' children as it's supposed to.
#foreach my $childListIndex (keys %self::childList) {
# delete $self::childList{$childListIndex};
#
#}
}
sub addChild {
my ($self, $childToAdd) = @_;
$self::childList{$childToAdd->id} = {
"child" => $childToAdd,
};
}
sub displayChildList {
my $self = shift;
foreach my $childListIndex (keys %self::childList) {
my $thisChildOut = Child::->new;
$thisChildOut = $self::childList{$childListIndex}{child};
print("\tChild belonging to parent ID " . $self->id . "\n");
print("\tChild ID = " . $thisChildOut->id . "\n");
print("\tChild Name = " . $thisChildOut->name . "\n");
print("\n");
}
}
for my $field (qw(id name childList)) {
my $slot = __PACKAGE__ . "::$field";
no strict "refs";
*$field = sub {
my $self = shift;
$self->{$slot} = shift if @_;
return $self->{$slot};
};
}
1;
#End of file Parent.pm
#Beginning of file test.pl
use strict;
use warnings;
use Parent;
use Child;
my $child1 = Child::->new;
$child1->id("1");
$child1->name("Cecil");
my $child2 = Child::->new;
$child2->id("2");
$child2->name("Eddy");
my $child3 = Child::->new;
$child3->id("3");
$child3->name("Dolph");
my $child4 = Child::->new;
$child4->id("4");
$child4->name("Cyrus");
my $parent1 = Parent::->new;
$parent1->id("1");
$parent1->name("Thelma");
$parent1->addChild($child1);
$parent1->addChild($child4);
print("Children of " . $parent1->name . "\n");
$parent1->displayChildList();
my $parent2 = Parent::->new;
$parent2->id("2");
$parent2->name("Magdalene");
$parent2->addChild($child3);
$parent2->addChild($child2);
print("Children of " . $parent2->name . "\n");
$parent2->displayChildList();
print("Children of " . $parent1->name . "\n");
$parent1->displayChildList();
#End of file test.pl
ran with the command
perl -w test.pl
Output is as follows:
Children of Thelma
Child belonging to parent ID 1
Child ID = 4
Child Name = Cyrus
Child belonging to parent ID 1
Child ID = 1
Child Name = Cecil
Children of Magdalene
Child belonging to parent ID 2
Child ID = 4
Child Name = Cyrus
Child belonging to parent ID 2
Child ID = 1
Child Name = Cecil
Child belonging to parent ID 2
Child ID = 3
Child Name = Dolph
Child belonging to parent ID 2
Child ID = 2
Child Name = Eddy
Children of Thelma
Child belonging to parent ID 1
Child ID = 4
Child Name = Cyrus
Child belonging to parent ID 1
Child ID = 1
Child Name = Cecil
Child belonging to parent ID 1
Child ID = 3
Child Name = Dolph
Child belonging to parent ID 1
Child ID = 2
Child Name = Eddy
How do I fix this so I get seperate lists?
Thanks
Julian
I have parent objects that have a list of child objects.
I want the list of children that belong to each parent to belong to
that instance of the parent object only. What I'm getting is a shared
list among the parent objects and I don't want that.
Here's the code...
#Beginning of file Child.pm
package Child;
sub new {
my $invocant = shift;
my $self = bless({}, ref $invocant || $invocant);
$self->init();
return $self;
}
sub init {
my $self = shift;
$self->id("0");
$self->name("unnamed");
}
for my $field (qw(id name)) {
my $slot = __PACKAGE__ . "::$field";
no strict "refs";
*$field = sub {
my $self = shift;
$self->{$slot} = shift if @_;
return $self->{$slot};
};
}
1;
#End of file Child.pm
#Beginning of file Parent.pm
package Parent;
use Child;
sub new {
my $invocant = shift;
my $self = bless({}, ref $invocant || $invocant);
$self->init();
return $self;
}
sub init {
my $self = shift;
$self->id("0");
$self->name("unnamed");
$self->childList({});
#running this will destroy all the parent's children,
#not just this instances' children as it's supposed to.
#foreach my $childListIndex (keys %self::childList) {
# delete $self::childList{$childListIndex};
#
#}
}
sub addChild {
my ($self, $childToAdd) = @_;
$self::childList{$childToAdd->id} = {
"child" => $childToAdd,
};
}
sub displayChildList {
my $self = shift;
foreach my $childListIndex (keys %self::childList) {
my $thisChildOut = Child::->new;
$thisChildOut = $self::childList{$childListIndex}{child};
print("\tChild belonging to parent ID " . $self->id . "\n");
print("\tChild ID = " . $thisChildOut->id . "\n");
print("\tChild Name = " . $thisChildOut->name . "\n");
print("\n");
}
}
for my $field (qw(id name childList)) {
my $slot = __PACKAGE__ . "::$field";
no strict "refs";
*$field = sub {
my $self = shift;
$self->{$slot} = shift if @_;
return $self->{$slot};
};
}
1;
#End of file Parent.pm
#Beginning of file test.pl
use strict;
use warnings;
use Parent;
use Child;
my $child1 = Child::->new;
$child1->id("1");
$child1->name("Cecil");
my $child2 = Child::->new;
$child2->id("2");
$child2->name("Eddy");
my $child3 = Child::->new;
$child3->id("3");
$child3->name("Dolph");
my $child4 = Child::->new;
$child4->id("4");
$child4->name("Cyrus");
my $parent1 = Parent::->new;
$parent1->id("1");
$parent1->name("Thelma");
$parent1->addChild($child1);
$parent1->addChild($child4);
print("Children of " . $parent1->name . "\n");
$parent1->displayChildList();
my $parent2 = Parent::->new;
$parent2->id("2");
$parent2->name("Magdalene");
$parent2->addChild($child3);
$parent2->addChild($child2);
print("Children of " . $parent2->name . "\n");
$parent2->displayChildList();
print("Children of " . $parent1->name . "\n");
$parent1->displayChildList();
#End of file test.pl
ran with the command
perl -w test.pl
Output is as follows:
Children of Thelma
Child belonging to parent ID 1
Child ID = 4
Child Name = Cyrus
Child belonging to parent ID 1
Child ID = 1
Child Name = Cecil
Children of Magdalene
Child belonging to parent ID 2
Child ID = 4
Child Name = Cyrus
Child belonging to parent ID 2
Child ID = 1
Child Name = Cecil
Child belonging to parent ID 2
Child ID = 3
Child Name = Dolph
Child belonging to parent ID 2
Child ID = 2
Child Name = Eddy
Children of Thelma
Child belonging to parent ID 1
Child ID = 4
Child Name = Cyrus
Child belonging to parent ID 1
Child ID = 1
Child Name = Cecil
Child belonging to parent ID 1
Child ID = 3
Child Name = Dolph
Child belonging to parent ID 1
Child ID = 2
Child Name = Eddy
How do I fix this so I get seperate lists?
Thanks
Julian