B
Bryan Balfour
I'm loathe to post this as I'm sure the problem is my misunderstanding
of how perl handles references but I've been going round in circles
long enough to justify my plea for help.
I've reached the stage in learning perl where I want to use OOP
techniques and I seem to have fallen at the first hurdle. Basically I
wanted to create multiple instances of a class and store the addresses
of these instances in a hash. This hash is then sorted on key for use
later on in the processing to reference the data elements stored in
each instance.
The problem I'm getting is that, although the hash seem to contain the
correct references to the object, using these references to address the
relevant object always results in the information from the last object
created being returned.
The code below demonstrates the basics of what I'm attempting. Running
it at a DOS prompt produces the following output.......
Unsorted list...........
Name: Bryan, Age: 63, Object: Person=HASH(0x1847ac0)
Name: Anne, Age: 62, Object: Person=HASH(0x1882fc0)
Name: Kevin, Age: 39, Object: Person=HASH(0x1847afc)
Name: Gareth, Age: 35, Object: Person=HASH(0x1847af0)
Name: Siobhan, Age: 31, Object: Person=HASH(0x1847b50)
Name: Kathryn, Age: 28, Object: Person=HASH(0x1847b8c)
Sorted list...........
Hash{name: Anne, object: Person=HASH(0x1882fc0)} {Object{name:
Kathryn, Age: 28}
Hash{name: Bryan, object: Person=HASH(0x1847ac0)} {Object{name:
Kathryn, Age: 28}
Hash{name: Gareth, object: Person=HASH(0x1847af0)} {Object{name:
Kathryn, Age: 28}
Hash{name: Kathryn, object: Person=HASH(0x1847b8c)} {Object{name:
Kathryn, Age:28}
Hash{name: Kevin, object: Person=HASH(0x1847afc)} {Object{name:
Kathryn, Age: 28}
Hash{name: Siobhan, object: Person=HASH(0x1847b50)} {Object{name:
Kathryn, Age:28}
* * * * * * * * * * *
I'd really appreciate someone pointing out were I'm going wrong here as
it has got me stumped.
Regards,
Bryan Balfour
* * * * * * * * * * *
use warnings;
use strict;
use constant TRUE => 1;
use constant FALSE => 0;
use Tie::IxHash;
my(%people, %persons, $person, $name, $age, $object);
tie %people, "Tie::IxHash"; # Maintain loading order
%people = ("Bryan" , 63, "Anne" , 62, "Kevin", 39, "Gareth", 35,
"Siobhan", 31, "Kathryn", 28);
print "Unsorted list...........\n";
while(($name, $age) = each(%people))
{
$person = Person->new($name, $age); # Get new instance of Person
class and load name and age
$persons{$name} = $person; # New hash entry with name as
key and and new object as value
print "Name: " . $person->getName() . ", Age: " . $person->getAge() .
", Object: " . $person . "\n";
}
print "Sorted list...........\n";
foreach $name (sort(keys(%persons)))
{
$person = $persons{$name}; # Person object built for this
name
print "Hash{name: " . $name . ", object: " . $person . "}
{Object{name: " . $person->getName() .
", Age: " . $person->getAge() . "}\n";
}
exit(TRUE);
package Person;
use constant TRUE => 1;
use constant FALSE => 0;
my($thisName, $thisAge);
sub new
{
my($className) = shift(@_);
($thisName, $thisAge) = @_;
my($self) = {};
bless($self, $className);
return($self);
}
sub getName
{
my($self) = shift(@_);
return($thisName);
}
sub getAge
{
my($self) = shift(@_);
return($thisAge);
}
return(TRUE);
sub getAge
{
my($self) = shift(@_);
return($thisAge);
}
return(TRUE);
of how perl handles references but I've been going round in circles
long enough to justify my plea for help.
I've reached the stage in learning perl where I want to use OOP
techniques and I seem to have fallen at the first hurdle. Basically I
wanted to create multiple instances of a class and store the addresses
of these instances in a hash. This hash is then sorted on key for use
later on in the processing to reference the data elements stored in
each instance.
The problem I'm getting is that, although the hash seem to contain the
correct references to the object, using these references to address the
relevant object always results in the information from the last object
created being returned.
The code below demonstrates the basics of what I'm attempting. Running
it at a DOS prompt produces the following output.......
Unsorted list...........
Name: Bryan, Age: 63, Object: Person=HASH(0x1847ac0)
Name: Anne, Age: 62, Object: Person=HASH(0x1882fc0)
Name: Kevin, Age: 39, Object: Person=HASH(0x1847afc)
Name: Gareth, Age: 35, Object: Person=HASH(0x1847af0)
Name: Siobhan, Age: 31, Object: Person=HASH(0x1847b50)
Name: Kathryn, Age: 28, Object: Person=HASH(0x1847b8c)
Sorted list...........
Hash{name: Anne, object: Person=HASH(0x1882fc0)} {Object{name:
Kathryn, Age: 28}
Hash{name: Bryan, object: Person=HASH(0x1847ac0)} {Object{name:
Kathryn, Age: 28}
Hash{name: Gareth, object: Person=HASH(0x1847af0)} {Object{name:
Kathryn, Age: 28}
Hash{name: Kathryn, object: Person=HASH(0x1847b8c)} {Object{name:
Kathryn, Age:28}
Hash{name: Kevin, object: Person=HASH(0x1847afc)} {Object{name:
Kathryn, Age: 28}
Hash{name: Siobhan, object: Person=HASH(0x1847b50)} {Object{name:
Kathryn, Age:28}
* * * * * * * * * * *
I'd really appreciate someone pointing out were I'm going wrong here as
it has got me stumped.
Regards,
Bryan Balfour
* * * * * * * * * * *
use warnings;
use strict;
use constant TRUE => 1;
use constant FALSE => 0;
use Tie::IxHash;
my(%people, %persons, $person, $name, $age, $object);
tie %people, "Tie::IxHash"; # Maintain loading order
%people = ("Bryan" , 63, "Anne" , 62, "Kevin", 39, "Gareth", 35,
"Siobhan", 31, "Kathryn", 28);
print "Unsorted list...........\n";
while(($name, $age) = each(%people))
{
$person = Person->new($name, $age); # Get new instance of Person
class and load name and age
$persons{$name} = $person; # New hash entry with name as
key and and new object as value
print "Name: " . $person->getName() . ", Age: " . $person->getAge() .
", Object: " . $person . "\n";
}
print "Sorted list...........\n";
foreach $name (sort(keys(%persons)))
{
$person = $persons{$name}; # Person object built for this
name
print "Hash{name: " . $name . ", object: " . $person . "}
{Object{name: " . $person->getName() .
", Age: " . $person->getAge() . "}\n";
}
exit(TRUE);
package Person;
use constant TRUE => 1;
use constant FALSE => 0;
my($thisName, $thisAge);
sub new
{
my($className) = shift(@_);
($thisName, $thisAge) = @_;
my($self) = {};
bless($self, $className);
return($self);
}
sub getName
{
my($self) = shift(@_);
return($thisName);
}
sub getAge
{
my($self) = shift(@_);
return($thisAge);
}
return(TRUE);
sub getAge
{
my($self) = shift(@_);
return($thisAge);
}
return(TRUE);