in reply to Re: Strange method behavior
in thread Strange method behavior

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

Replies are listed 'Best First'.
Re: Re: Re: Strange method behavior
by chipmunk (Parson) on Dec 17, 2000 at 22:58 UTC
    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