This is sample code that I created merely to test simple
arrays of objects that I have created. Unfortunately,
each time I push a new object onto the array (thus giving
the array a new pointer to a newly created object) all other
pointers in the array become identical to the last one.
Here is the test code, below it is the test class.
#!/usr/bin/perl
use Class2;
$i = Class2->new();
print "Element1 Definition:\n";
$i->data("3.01.01", "Heloo");
print $i->getMe(), "\n";
$j = Class2->new();
print "Element2 Definition:\n";
$j->data("4.01.99", "Wow");
print $j->getMe(), "\n";
push (@stuff, $i);
push (@stuff, $j);
print "\nArray Contains:\n";
$tmp = @stuff[0];
$tmp->printClass();
$tmp_date = $tmp->getDate();
print "$tmp_date\n";
print "Element1: ", $tmp->getMe(), "\n";
$tmp = @stuff[1];
$tmp->printClass();
$tmp_date = $tmp->getDate();
print "$tmp_date\n";
print "Element2: ", $tmp->getMe(), "\n";
package Class2;
sub new
{
$class = shift;
my $self = {};
bless($self);
return $self;
}
sub data
{
@_ == 3 or die 'usage: Class2->data( DATE, SUBJ )';
($me, $date, $subj) = @_;
print "The Date is: $date\n";
print "The Subject is: $subj\n";
}
sub printClass
{
print "Your class type is $class\n";
print "The Date is: $date\n";
print "The Subject is: $subj\n";
}
sub getDate
{
return $date;
}
sub getMe
{
return $me;
}
return 1;
Thanks!