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

Moritz, your reply helped me find the problem. After your suggestion did not work either I rechecked the subroutine and found that I had not passed $coordinates into it. Doing so, now your suggestion and my original line both work. Also, I thought about exist, defined and true and think I should use exist, because it is a prerequisite to defined and true. Thanks a lot for your help! Henri
  • Comment on Re^2: Test if a subhash in a referenced hash exists

Replies are listed 'Best First'.
Re^3: Test if a subhash in a referenced hash exists
by almut (Canon) on May 28, 2010 at 10:37 UTC

    ...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 +...' }
      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?