H
hoeeg (at) post4.tele.dk
Hi, i thought i had this OO thing figured out, but apparantly not.
I have a module i want to make multiple instances of.
All goes well with the first instance until i create a second instance,
then the first instance seems to be overwritten.
I have made a small test module to demonstrate the problem
--------------------------------------------
# perl module Testobject.pm
package Testobject;
sub new
{
my $class=shift;
my $self={};
$self{'id'}=shift;
bless ($self, $class);
print "New $self{'id'} - Self: $self\n";
return $self;
}
sub showid
{
my $self=shift;
my $caller=shift;
print "ID: $self{'id'} - Caller: $caller - Self: $self\n";
}
sub DESTROY
{
print "Bye-bye $self{'id'}\n";
}
1;
--------------------------------------------
And a small test program
--------------------------------------------
#!/usr/bin/perl -w
use Testobject;
$inst_1=Testobject->new("Inst-1");
$inst_1->showid("Inst_1");
$inst_2=Testobject->new("Inst-2");
$inst_1->showid("Inst_1");
$inst_2->showid("Inst_2");
undef($inst_2);
$inst_1->showid("Inst_1");
--------------------------------------------
When i run the program it outputs:
--------------------------------------------
New Inst-1 - Self: Testobject=HASH(0x2850e0)
ID: Inst-1 - Caller: Inst_1 - Self: Testobject=HASH(0x2850e0)
New Inst-2 - Self: Testobject=HASH(0x2851a0)
ID: Inst-2 - Caller: Inst_1 - Self: Testobject=HASH(0x2850e0)
ID: Inst-2 - Caller: Inst_2 - Self: Testobject=HASH(0x2851a0)
Bye-bye Inst-2
ID: Inst-2 - Caller: Inst_1 - Self: Testobject=HASH(0x2850e0)
Bye-bye Inst-2
I have a module i want to make multiple instances of.
All goes well with the first instance until i create a second instance,
then the first instance seems to be overwritten.
I have made a small test module to demonstrate the problem
--------------------------------------------
# perl module Testobject.pm
package Testobject;
sub new
{
my $class=shift;
my $self={};
$self{'id'}=shift;
bless ($self, $class);
print "New $self{'id'} - Self: $self\n";
return $self;
}
sub showid
{
my $self=shift;
my $caller=shift;
print "ID: $self{'id'} - Caller: $caller - Self: $self\n";
}
sub DESTROY
{
print "Bye-bye $self{'id'}\n";
}
1;
--------------------------------------------
And a small test program
--------------------------------------------
#!/usr/bin/perl -w
use Testobject;
$inst_1=Testobject->new("Inst-1");
$inst_1->showid("Inst_1");
$inst_2=Testobject->new("Inst-2");
$inst_1->showid("Inst_1");
$inst_2->showid("Inst_2");
undef($inst_2);
$inst_1->showid("Inst_1");
--------------------------------------------
When i run the program it outputs:
--------------------------------------------
New Inst-1 - Self: Testobject=HASH(0x2850e0)
ID: Inst-1 - Caller: Inst_1 - Self: Testobject=HASH(0x2850e0)
New Inst-2 - Self: Testobject=HASH(0x2851a0)
ID: Inst-2 - Caller: Inst_1 - Self: Testobject=HASH(0x2850e0)
ID: Inst-2 - Caller: Inst_2 - Self: Testobject=HASH(0x2851a0)
Bye-bye Inst-2
ID: Inst-2 - Caller: Inst_1 - Self: Testobject=HASH(0x2850e0)
Bye-bye Inst-2