in reply to Strange method behavior

I think we would need to see your constructor and the way you build new objects to do a lot more debugging. In the absence of that information, here's where I'd focus my debugging:
foreach my $day (@days) { my $day_data = $self->{cell_format}[$day]; print STDERR "Changing hash $day_data for day $day!\n"; @$day_data{keys %$format} = values %$format; }
That way, you can compare the reference address for multiple accesses. (My bet is on something tricky in the constructor, though.)

Replies are listed 'Best First'.
Re: Re: Strange method behavior
by Trimbach (Curate) on Dec 17, 2000 at 08:18 UTC
    Well, I took your advice and spent some additional time with the constructor to see what was going on. I was using a hash with default values for cell_format that populated every date during construction. Problem was, I accidentally populated every date with a reference to the default hash (and the same reference at that.) For some reason format_cell2 was changing the value of my default hash which instantly changed all the places where that hash was referenced. Bad Trimbach. Bad.

    It's clear that's what happened, although I don't grok why that hash was getting changed at all, but hey, I fixed it and now everything works. Thanks Chromatic (and everyone else) for sparking the idea that led me to the solution... and I promise to play nicer with references in the future.

    Gary Blackburn
    Trained Killer

      The original hash was getting changed because all your references pointed to that hash. The thing to remember is that when you derefence a reference, you get the original data structure back, not a copy of the structure. To get a copy, you need to make the copy when you create the reference.
      my %hash = (snark => 'snark'); # hash my $ref = \%hash; # ref to hash surprise($ref); # pass ref to hash sub surprise { my $ref = shift; # copy ref to hash $ref->{snark} = 'boojum'; # change ref'ed hash } print "snark => $hash{snark}\n"; # print original hash __END__ snark => boojum