Skip to content

Instantly share code, notes, and snippets.

@libitte
Created November 6, 2013 03:57
Show Gist options
  • Save libitte/7330661 to your computer and use it in GitHub Desktop.
Save libitte/7330661 to your computer and use it in GitHub Desktop.
hash... * exists * delete variable... * defined * undef
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (
key1 => 'value1',
key2 => 'value2',
key3 => 'value3',
);
print "-------------------\n";
print "undef\n";
print "-------------------\n";
{
undef $hash{key2};
#{
# 'key2' => undef,
# 'key1' => 'value1',
# 'key3' => 'value3'
#};
if (defined $hash{key2}) {
print "key2 is defined\n";
}
else {
print "key2 is NOT defined\n";
}
if (exists $hash{key2}) {
print "key2 is exists\n";
}
else {
print "key2 is exists\n";
}
print Dumper \%hash;
}
print "-------------------\n";
print "delete\n";
print "-------------------\n";
{
delete $hash{key2};
#{
# 'key1' => 'value1',
# 'key3' => 'value3'
#};
if (defined $hash{key2}) {
print "key2 is defined\n";
}
else {
print "key2 is NOT defined\n";
}
if (exists $hash{key2}) {
print "key2 is exists\n";
}
else {
print "key2 is exists\n";
}
print Dumper \%hash;
}
__END__
hash
exists
delete
variable
defined
undef
-------------------
undef
-------------------
key2 is NOT defined
key2 is exists
$VAR1 = {
'key2' => undef,
'key1' => 'value1',
'key3' => 'value3'
};
-------------------
delete
-------------------
key2 is NOT defined
key2 is exists
$VAR1 = {
'key1' => 'value1',
'key3' => 'value3'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment