in reply to Why is this

You don't have variables named $h3 or $h4. You probably mean %h3 and %h4.

You don't change variable %h3. You change an element of %h1 using a reference stored in %h2 and another reference stored in %h3.

my %h1 = ( k1 => "v1" ); my %h2 = ( k2 => \%h1 ); my %h3 = ( k3 => \%h2 ); %h3 +--------------------+ | +----------+ | | k3 => | Ref ------------+ | +----------+ | | +--------------------+ | | v %h2 +--------------------+ | +----------+ | | k2 => | Ref -----------+ | +----------+ | | +--------------------+ | | v %h1 +--------------------+ | +----------+ | | k1 => | Str "v1" | | | +----------+ | +--------------------+
my %h4; %h3 +--------------------+ | +----------+ | | k3 => | Ref ------------+ | +----------+ | | +--------------------+ | | %h4 | +--------------------+ | | | | | | | | | | +--------------------+ | | v %h2 +--------------------+ | +----------+ | | k2 => | Ref -----------+ | +----------+ | | +--------------------+ | | v %h1 +--------------------+ | +----------+ | | k1 => | Str "v1" | | | +----------+ | +--------------------+
%h4 = %h3; %h3 +--------------------+ | +----------+ | | k3 => | Ref -------------+ | +----------+ | | +--------------------+ | | %h4 | +--------------------+ | | +----------+ | | | k3 => | Ref -----------+ | | +----------+ | | | +--------------------+ | | | | v v %h2 +--------------------+ | +----------+ | | k2 => | Ref -----------+ | +----------+ | | +--------------------+ | | v %h1 +--------------------+ | +----------+ | | k1 => | Str "v1" | | | +----------+ | +--------------------+
$h3{k3}{k2}{k1} = "v2"; %h3 +--------------------+ | +----------+ | | k3 => | Ref -------------+ | +----------+ | | +--------------------+ | | %h4 | +--------------------+ | | +----------+ | | | k3 => | Ref -----------+ | | +----------+ | | | +--------------------+ | | | | v v %h2 +--------------------+ | +----------+ | | k2 => | Ref -----------+ | +----------+ | | +--------------------+ | | v %h1 +--------------------+ | +----------+ | | k1 => | Str "v2" | | | +----------+ | +--------------------+

You can use Storable's dclone to do a "deep" (recursive) copy, in which case you'll end up with the following:

%h3 +--------------------+ | +----------+ | | k3 => | Ref ------------+ | +----------+ | | +--------------------+ | | v %h2 +--------------------+ | +----------+ | | k2 => | Ref -----------+ | +----------+ | | +--------------------+ | | v %h1 +--------------------+ | +----------+ | | k1 => | Str "v2" | | | +----------+ | +--------------------+ %h4 +--------------------+ | +----------+ | | k3 => | Ref ------------+ | +----------+ | | +--------------------+ | | v %anon +--------------------+ | +----------+ | | k2 => | Ref -----------+ | +----------+ | | +--------------------+ | | v %anon +--------------------+ | +----------+ | | k1 => | Str "v1" | | | +----------+ | +--------------------+

Replies are listed 'Best First'.
Re^2: Why is this
by whapp (Initiate) on Dec 09, 2011 at 02:34 UTC
    Thanks, I'm really impressed with the answer.