NAME
Deep::Hash::Exists - verify existence of keys hash
SYNOPSIS
use Deep::Hash::Exists qw( key_exists );
my $hash_ref = {
A => 'one',
B => [ 'one', 'two' ],
C => {
'one' => 1,
'two' => 2,
},
};
if ( key_exists( $hash_ref, [ 'C', 'one' ] ) ) {
printf "Output: %d", key_exists( $hash_ref, [ 'C', 'one' ] );
}
# Output: 1
if ( key_exists( $hash_ref, [ 'D' ] ) ) {
printf "Output: %d", key_exists( $hash_ref, [ 'D' ] );
}
# Output: 0
DESCRIPTION
Exists hash:
my $hash_ref = {
A => 'one',
B => [ 'one', 'two' ],
C => {
'one' => 1,
'two' => 2,
},
};
If verify existence of keys standard way, will be created non existent keys:
exists $hash_ref->{C}{three}{PI}{0};
print Dumper $hash_ref;
Output:
# $VAR1 = {
# 'A' => 'one',
# 'B' => [
# 'one',
# 'two'
# ]
# 'C' => {
# 'one' => 1,
# 'two' => 2,
# 'three' => {
# 'PI' => {}
# }
# },
# };
Subroutine key_exists
does not create new keys:
key_exists( $hash_ref, [ 'C', 'three', 'PI', 0 ] );
print Dumper $hash_ref;
Output:
# $VAR1 = {
# 'A' => 'one',
# 'B' => [
# 'one',
# 'two'
# ],
# 'C' => {
# 'one' => 1,
# 'two' => 2
# }
# };
key_exists($$)
key_exists( $hash_ref, $array_ref_keys ) - verify existence of keys hash
Return:
1 - if exists hash key
0 - if not exists hash key
Example:
my $hash_ref = {
A => 'one',
B => [ 'one', 'two' ],
C => {
'one' => 1,
'two' => 2,
},
};
printf "Output: %s", key_exists( $hash_ref, [ 'A' ] );
# Output: 1
printf "Output: %s", key_exists( $hash_ref, [ 'B' ] );
# Output: 1
printf "Output: %s", key_exists( $hash_ref, [ 'B', 0 ] );
# Output: 0
printf "Output: %s", key_exists( $hash_ref, [ 'C', 'one' ] );
# Output: 1
printf "Output: %s", key_exists( $hash_ref, [ 'C', 'three' ] );
# Output: 0
printf "Output: %s", key_exists( $hash_ref, [ 'C', 'three', 'PI', 0 ] );
# Output: 0
# Subroutine does not create new keys.
SEE ALSO
COPYRIGHT AND LICENSE
Copyright (C) 2015 Vitaly Simul <vitalysimul@gmail.com>
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
AUTHOR
Vitaly Simul <vitalysimul@gmail.com>