Initialize Perl hash value as a hash impossible

The following script would fail

use strict;

my %hash = (-id => 10, -name => ‘Hongyu’);
my %hash2;

$hash2{hash} = %hash;
print $hash2{hash}{-id}, “\n”;

The problem here is that initializing a Perl hash value with a hash is impossible, need to use reference instead

use strict;

my %hash = (-id => 10, -name => ‘Hongyu’);
my %hash2;

$hash2{hash} = \%hash;
print $hash2{hash}->{-id}, “\n”;

Leave a Reply