473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with multiple instances of a module

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?
Jul 19 '05 #1
1 3615
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 sa******@worldnet.att.net
1024D/FA2C3588 EDFA 195F EDF1 0933 1002 6396 7C92 5CB3 FA2C 3588
-----------------------------------------------------------------
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Daniel Pryde | last post by:
Hi there. I'm currently trying to learn Python, my aim being to create my final year project at university using only Python instead of Java. I've run into a problem though while trying to make...
12
by: (Pete Cresswell) | last post by:
I know I can open many instances of a given form, but I've never done it. Now I'm analyzing an application where that seems like just the ticket: Many investment funds, *lots* of data points for...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
4
by: striker | last post by:
I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example: one, two, three,,,,four,five,,,,six one, two,...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
4
by: Gus | last post by:
We create reports in MSWord 2000 from within our Access 2002 application. The report template uses a lot of tables and nested tables. Over time, many hundreds of reports, Word has become...
6
by: Savante | last post by:
I have been writing a datalogging application. It reads in double's into a database. I want to be able to click on a row in a database (holds name of variable and also current value of variable)...
3
by: Lawrence D'Oliveiro | last post by:
In message <mailman.109.1216158151.922.python-list@python.org>, Cyril Bazin wrote: What's the error message? This should be in Apache's error_log file.
13
by: Rafe | last post by:
Hi, I am in a situation where I feel I am being forced to abandon a clean module structure in favor of a large single module. If anyone can save my sanity here I would be forever grateful. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.