Created
November 6, 2013 03:57
-
-
Save libitte/7330661 to your computer and use it in GitHub Desktop.
hash... * exists
* delete variable... * defined
* undef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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