Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Distinguish x=undef vs x=''

Distinguish x=undef vs x=''

Distinguish x=undef vs x=''

Sample codes:


use Data::Dumper;

my %h; 
$h{a} = undef;
$h{b} = ''; 
$h{c} = 'z';
print Dumper(\%h); # Dumper can distinguish the difference between $h{a} and $h{b}
foreach my $k (keys(%h)) {
    # The following block cannot distinguish the difference between $h{a} and $h{b} 
    if(!$h{$k}) {
        print "Undefined hash value: Key - $k\n";
    else {
        print "Key: $k, Val: $h{$k}\n";

Sample run:


% ./tt.pl 
$VAR1 = {
          'c' => 'z',
          'a' => undef,
          'b' => ''
Key: c, Val: z
Undefined hahs value: Key - a
Undefined hahs value: Key - b 

Could someone show me how to distinguish them? Thanks.

RE: Distinguish x=undef vs x=''

Thank you, @prex1.

But I thought define() is obsolete?

RE: Distinguish x=undef vs x=''

'defined' not 'define()' is a perlfunc keyword operator not a method / sub, so no parentheses.

Direct quote from perl docs.


Use of defined on aggregates (hashes and arrays) is deprecated.


It all depends on what you are trying to test.


my @arr;
print defined @arr;


my %hash;
print defined %hash; 
is deprecated.

if you want to know if a key exists in a hash use exists


print exists $hash{key}; 

if you want to know whether a key has been assigned a value, use defined


print defined $hash{key}; 

defined is really for testing scalar values or if a subroutine is defined.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close