Problem with multiple instances of a module

Discussion in 'Perl' started by hoeeg (at) post4.tele.dk, Apr 20, 2005.

  1. 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?
    hoeeg (at) post4.tele.dk, Apr 20, 2005
    #1
    1. Advertising

  2. 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
    1024D/FA2C3588 EDFA 195F EDF1 0933 1002 6396 7C92 5CB3 FA2C 3588
    -----------------------------------------------------------------
    Alan D. Salewski, Apr 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  2. Gabriel  Jiva
    Replies:
    2
    Views:
    393
    infidel
    Jun 27, 2005
  3. Replies:
    8
    Views:
    460
    James Stroud
    Jan 29, 2009
  4. Ian Kelly
    Replies:
    0
    Views:
    421
    Ian Kelly
    Jul 5, 2010
  5. Kim H. Jensen

    Problem with multiple instances of a module

    Kim H. Jensen, Apr 20, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    140
    Kim H. Jensen
    Apr 21, 2005
Loading...

Share This Page