Hi All,

I am unsure if the following statement is going to do what I want.

I want to test if there is a value in $hash_ref{'someval'} NOT if the
key is defined. I'd also like to avoid un-sightly "undefined value"
errors.

So which of the following is best:


if (defined($hash_ref{'someval'}) {}

or is

if ($hash_ref{'someval'} =~ /\w+/) {} # I am expecting a string.

or

if (! $hash_ref{'someval'} ) {}


Thanks,
Dp.



use strict;
use warnings;
use Test::Simple tests => 3;

my $ref_one = {
        'reference'     => '',
        'expect'                        => 1,
        };

my $ref_two = {
        'reference'     => undef,
        'expect'                        => undef,
        };

my $ref_three = {
        'reference'     => 'some string',
        'expect'                        => 1,
        };

my @tests = ($ref_one, $ref_two, $ref_three);
foreach my $t (@tests) {
        my $got = defined( $t->{'reference'} );
        ok( $got eq $t->{'expect'}, 'Defined');
}

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to