Problem with multiple instances of a module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hoeeg (at) post4.tele.dk

    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'}=shi ft;
    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=Testobj ect->new("Inst-1");
    $inst_1->showid("Inst_1 ");

    $inst_2=Testobj ect->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?
  • Alan D. Salewski

    #2
    Re: Problem with multiple instances of a module

    On 2005-04-20, hoeeg (at) post4.tele.dk <"hoeeg (at) post4.tele.dk"> wrote:[color=blue]
    > 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;[/color]
    *snip*
    [color=blue]
    > When instance 2 is created, instance 1 takes on the identity of instance 2.
    >
    > What have i overlooked?[/color]
    *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 salewski@worldn et.att.net
    1024D/FA2C3588 EDFA 195F EDF1 0933 1002 6396 7C92 5CB3 FA2C 3588
    -----------------------------------------------------------------

    Comment

    Working...