Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Problem with multiple instances of a module

Reply
Thread Tools

Problem with multiple instances of a module

 
 
hoeeg (at) post4.tele.dk
Guest
Posts: n/a
 
      04-20-2005
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
--------------------------------------------

When instance 2 is created, instance 1 takes on the identity of instance 2.

What have i overlooked?
 
Reply With Quote
 
 
 
 
Alan D. Salewski
Guest
Posts: n/a
 
      04-22-2005
On 2005-04-20, hoeeg (at) post4.tele.dk <"hoeeg (at) post4.tele.dk"> wrote:
> 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;

*snip*

> When instance 2 is created, instance 1 takes on the identity of instance 2.
>
> What have i overlooked?

*snip*

Hi hoeeg,

If you add 'use strict;' after the above package line in
Testobject.pm, perl will give you the info you seek.

HTH,
-Al

--
-----------------------------------------------------------------
a l a n d. s a l e w s k i http://www.velocityreviews.com/forums/(E-Mail Removed)
1024D/FA2C3588 EDFA 195F EDF1 0933 1002 6396 7C92 5CB3 FA2C 3588
-----------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why Python forbids multiple instances of one module? Ian Kelly Python 0 07-05-2010 05:42 AM
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
pulling multiple instances of a module into memory Gabriel Jiva Python 2 06-27-2005 10:45 PM
Problem with multiple instances of a module Kim H. Jensen Perl Misc 2 04-21-2005 10:08 AM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM



Advertisments