Skip to content

Instantly share code, notes, and snippets.

@sestaton
Last active August 29, 2015 14:05
Show Gist options
  • Save sestaton/894c1b4a1b8bc9d7056f to your computer and use it in GitHub Desktop.
Save sestaton/894c1b4a1b8bc9d7056f to your computer and use it in GitHub Desktop.
show the difference between defined and exists
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
my %hash = (
id => undef,
id2 => 2,
);
if (!$hash{id}) {
say "'id' is not defined...";
}
if (exists $hash{id}) {
say "but it exists in the hash!";
}
if (exists $hash{id2} && defined $hash{id2}){
say "'id2' exists and is defined.";
}
## running this prints:
# 'id' is not defined...
# but it exists in the hash!
# 'id2' exists and is defined.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment