Perlで2階層目のハッシュを参照するときは注意が必要

use strict;
use warnings;

my (%hash, $msg);
$msg = (exists $hash{key}) ? "exist" : "not exist";
print "$msg\n";  # not exist

# 1階層目を参照
my $tmp = $hash{key};
$msg = (exists $hash{key}) ? "exist" : "not exist";
print "$msg\n";  # not exist

# 2階層目を参照
$tmp = $hash{key}{sub_key};
$msg = (exists $hash{key}) ? "exist" : "not exist";
print "$msg\n";  # exist

warningすら出ません。
当然ですけど、existsではなくて、definedでも同じです。


$hash{key}{***}; で参照しただけのつもりが、
$hash{key} = {}; と同じようなことになっちゃいます。


warningくらい出してくれてもいい気がしますが。。。
他の言語だとどうなんでしょう。



ちなみに、javascriptはちゃんとエラーになります。

var hash = {};
var msg;
msg = (hash['key']) ? 'exist' : 'not exist';
alert(msg);  // not exist

var tmp = hash['key'];
msg = (hash['key']) ? 'exist' : 'not exist';
alert(msg);  // not exist

tmp = hash['key']['subKey'];  // TypeError: hash.key is undefined
msg = (hash['key']) ? 'exist' : 'not exist';
alert(msg);