in reply to Re^2: Test if a subhash in a referenced hash exists
in thread Test if a subhash in a referenced hash exists

...but note that exists does not test what you asked for in the subject line, i.e. if a subhash exists — it just tests whether the respective hash key exists, or more precisely, whether the respective hash element has ever been initialized (with whatever value, including undef).

$data->{foo} = ""; if ( exists $data->{foo} ) { print $data->{foo}{bar}; # 'Can't use string ("") as a HASH ref +...' }

or with undef as value (in which case the missing hash would be autovivfied)

$data->{foo} = undef; if ( exists $data->{foo} ) { print $data->{foo}{bar}; # 'Use of uninitialized value in print +...' }

Replies are listed 'Best First'.
Re^4: Test if a subhash in a referenced hash exists
by Henri (Novice) on May 29, 2010 at 13:17 UTC
    If I understand you right, with “exits” I am testing if keys at the top level of the anonymous subhash exist. If I wanted to test if the hash reference exists I would need to use “ref”. Correct?