The reason the last line gives an error is that it does the equivalent of$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 2 lines above it don't work if you take away the # at the begining of the lines.my $z = 1; $z->{'text'} = 2;
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
it's much more clear what's going on, though it's more typing.$box{$X}->{$Y}->{$z} = 1; $box{$X}->{$Y}->{$z}->{'text'} = 2;
Update:
Try doing this instead:
if you really want to create the 'depth' of that HoH.$box{$X}{$Y}{$z} = undef;
Update2:
Though you really don't need it:
on it's own (without the preceding 3 lines) works perfectly.$box{$X}{$Y}{$z}{'text'} = 2;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |