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" | | | +----------+ | +--------------------+

In reply to Re: Why is this by ikegami
in thread Why is this by whapp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.