$box{$X}{$Y}{$z} = 1; # DOES work # $box{$X}{$Y}{$z}{'rect'} = 1 ; # 'works' because it's comme +nted out # $box{$X}{$Y}{$z}{'rect'} = undef ; # see above $box{$X}{$Y}{$z}{'text'} = 2; # refs error
The reason the last line gives an error is that it does the equivalent of
my $z = 1; $z->{'text'} = 2;
The 2 lines above it don't work if you take away the # at the begining of the lines.

A hash of hashes can be automatically created, but perl will not convert a scalar value in it to a hash unless it's undefined. By setting $box{$X}{$Y}{$z} = 1, you prevent $box{$X}{$Y}{$z} from being a hash.

Personally I always find that if you write this as

$box{$X}->{$Y}->{$z} = 1; $box{$X}->{$Y}->{$z}->{'text'} = 2;
it's much more clear what's going on, though it's more typing.

Update:
Try doing this instead:

$box{$X}{$Y}{$z} = undef;
if you really want to create the 'depth' of that HoH.

Update2:
Though you really don't need it:

$box{$X}{$Y}{$z}{'text'} = 2;
on it's own (without the preceding 3 lines) works perfectly.

See also perlref


In reply to Re: The "no strict refs" problem revisited. by Joost
in thread The "no strict refs" problem revisited. by zentara

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.