in reply to OO Perl: after it creates 2nd object overwrites hash from the first

Change
%{$self->{_hash}} = ();

to

$self->{_hash} = {};

Output:

this is cat data cat:a,b,c,d,e,fcat2:g,h,i,j,k,l,m this is cat keys catcat2 this is dog data dog1:1,2,3,4,5,6,7dog2:21,22,23,24,25 this is dog keys dog1dog2 but....after creates the second object this is cat data cat:a,b,c,d,e,fcat2:g,h,i,j,k,l,m this should be cat keys catcat2 this is dog data dog1:1,2,3,4,5,6,7dog2:21,22,23,24,25 this is dog keys dog1dog2

Replies are listed 'Best First'.
Re^2: OO Perl: after it creates 2nd object overwrites hash from the first
by flope004 (Acolyte) on Aug 31, 2007 at 18:04 UTC
     Thanks a lot!! It works fine!!
     Now I have to understand what's making the difference 
      Your version doesn't create a new hashref for each object. It just reuses the same one:
      my $self1 = {}; %{ $self1->{hash} } = (); print "$self1->{hash}\n"; %{ $self1->{hash} } = (); print "$self1->{hash}\n"; #Output: HASH(0x504f80) HASH(0x504f80)

      My use of {} creates a new hashref for both objects:

      my $self2; $self2->{hash} = {}; print "$self2->{hash}\n"; $self2->{hash} = {}; print "$self2->{hash}\n"; #Output: HASH(0x622bb0) HASH(0x504160)