in reply to If hash not defined help

use strict; use warnings; use Data::Dumper; { my $current= { 'version' => '2.0', 'xmlns:media' => 'http://search.yahoo.com/mrss/', 'fileSet' => { 'time' => '1499387013', 'fileName' => 'Atlanta_Regency.gif', 'emailAddress' => 'johndoe@gmail.com', 'requesterName' => 'John Doe', 'showAbbreviation' => {}, 'zoneColor' => '0' } }; if (exists($current->{'fileSet'}->{'showAbbreviation'}) && defined ($current->{'fileSet'}->{'showAbbreviation'}) && ( scalar(keys %{$current->{'fileSet'}->{'showAbbreviation'}}) > +0) ) { my $showAbbreviation = $current->{'fileSet'}->{'showAbbreviation +'}; print "Yes: showAbbreviation\n"; print Dumper($showAbbreviation)."\n"; } else { print "No: showAbbreviation\n"; } } { my $current= { 'version' => '2.0', 'xmlns:media' => 'http://search.yahoo.com/mrss/', 'fileSet' => { 'time' => '1499387013', 'fileName' => 'Atlanta_Regency.gif', 'emailAddress' => 'johndoe@gmail.com', 'requesterName' => 'John Doe', 'showAbbreviation' => {a=>1}, 'zoneColor' => '0' } }; if (exists($current->{'fileSet'}->{'showAbbreviation'}) && defined ($current->{'fileSet'}->{'showAbbreviation'}) && ( scalar(keys %{$current->{'fileSet'}->{'showAbbreviation'}}) > +0) ) { my $showAbbreviation = $current->{'fileSet'}->{'showAbbreviation +'}; print "Yes: showAbbreviation\n"; print Dumper($showAbbreviation)."\n"; } else { print "No: showAbbreviation\n"; } }

Replies are listed 'Best First'.
Re^2: If hash not defined help
by AnomalousMonk (Archbishop) on Jul 07, 2017 at 02:57 UTC

    johnfl68: If  $current->{'fileSet'}{'showAbbreviation'} could exist and be defined and yet be something other than a hash reference, it might be wise to add a test for hash-refitude to the other tests proposed by huck (attempting to dereference a non-hashref as a hash is fatal under strict refs, which you should, of course, have enabled :) (untested | since tested):
        if (exists($current->{'fileSet'}->{'showAbbreviation'})
            && defined ($current->{'fileSet'}->{'showAbbreviation'})
            && 'HASH' eq ref($current->{'fileSet'}->{'showAbbreviation'})
            && ( scalar(keys %{$current->{'fileSet'}->{'showAbbreviation'}}) >0)
            ) {
            ...
            }

    Update: In fact, since ref returns the empty string for an undefined argument, the explicit defined test is not necessary, making the whole thing a bit less messy:
        if (exists($current->{'fileSet'}->{'showAbbreviation'})
            && 'HASH' eq ref($current->{'fileSet'}->{'showAbbreviation'})
            && ( scalar(keys %{$current->{'fileSet'}->{'showAbbreviation'}}) >0)
            ) {
            ...
            }


    Give a man a fish:  <%-{-{-{-<

Re^2: If hash not defined help
by johnfl68 (Scribe) on Jul 07, 2017 at 04:50 UTC

    Thank you. That helps now for when showAbbreviation is empty, but unfortunately if it has a value in it, for example 'Test', I get this error now...

    Can't use string ("Test") as a HASH ref while "strict refs" in use.

    I give up for tonight.

      Can't use string ("Test") as a HASH ref ...

      Maybe take a look at this.


      Give a man a fish:  <%-{-{-{-<