The constructor isn't copying the %default_values hash but is simply referencing it, and then modifying it. So when the second obejct is created and the constructor run again, it's pointing at the same hash, the contents of which are now different due to the modifications from the first run of the constructor. To fix, simply do this to make a copy first (i tested it and your tests will all pass):warn "default_values: " . \%default_values; my $self = ref($class) ? bless( \%default_values, ref($class) ) : bless( \%default_values, $class ); warn "self: " . $self;
Also, as a side note, note that this is identical logic w/o the repeated code:my %h = %default_values; my $self = ref($class) ? bless( \%h, ref($class) ) : bless( \%h, $class );
Update: Combining w/tlm's solution that does away w/the need for the temp var %h it can just be:my %h = %default_values; my $self = bless( \%h, ref($class) || $class );
In reply to Re: Wrong Error Message When Testing for Failure of Constructor
by davidrw
in thread Wrong Error Message When Testing for Failure of Constructor
by jkeenan1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |