Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: OO Perl: after it creates 2nd object overwrites hash from the first

by Roy Johnson (Monsignor)
on Aug 31, 2007 at 17:09 UTC ( [id://636384]=note: print w/replies, xml ) Need Help??


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

_attribute_default returns the same references every time, as the default attributes of new objects. Every $self->{_hash} refers to the same hash by default. You reassign the _data element by providing an initial value, but you don't reassign the _hash element.

Watch it happen by adding some print statements to your constructor:

# Constructor sub new { my ( $class, %arg ) = @_; my $self = bless {}, $class; $self->_arg_exist(%arg); # Check if all given args are ok # Set the attributes for the provided arguments foreach my $attribute ( $self->_all_attributes() ) { my ($argument) = ( $attribute =~ /^_(.*)/ ); # Initilize to defaults $self->{$attribute} = $self->_attribute_default($attribute); # Override defaults with arguments if ( exists $arg{$argument} ) { print STDERR "Overriding $attribute with $argument ($arg{$argument})\n +"; $self->{$attribute} = $arg{$argument}; } } foreach my $k (keys %$self) { print STDERR "$k: $self->{$k}\n"; } # Empty the hash loci %{$self->{_hash}} = (); # Getting the data from the array # and storing the information in the hash $self->_get_hash (); return $self; }

Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://636384]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-18 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found