Vitaly Simul

NAME

Deep::Hash::Exists - Verify existence of keys hash.

SYNOPSIS

 use Deep::Hash::Exists ':all';
 
 my $hash_ref = {
         A => 'one',
         B => [ 'one', 'two' ],
         C => { 
             'one' => 1, 
             'two' => 2, 
         },
 };
 
 key_exists( $hash_ref, [ 'C', 'one' ] ); # return 1
 key_exists( $hash_ref, [ 'D' ] );        # return 0
 
 every_keys( $hash_ref, [ ['A'], ['B', 0], ['C', 'one'] ] ); # return 0
 every_keys( $hash_ref, [ ['A'], ['B'], ['C', 'one'] ] );    # return 1
 
 some_keys( $hash_ref, [ ['A'], ['B', 0], ['C', 'one'] ] ); # return 1
 some_keys( $hash_ref, [ ['D'], ['B', 0], ['C', 'six'] ] ); # return 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
 #                 }
 #        };

METHODS

The first argument in methods key_exists, every_keys and some_keys can be a hash reference and a blessed hash.

key_exists($$)

key_exists( $hash_ref, $array_ref_keys ) - Verify existence of keys hash.

Return:

If exist keys of hash then return 1, otherwise - 0.

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.

every_keys($$)

every_keys( $hash_ref, $list_array_ref ) - Verify list of hash keys for existence.

Return:

If exist all keys of hash of the submitted list then return 1, otherwise - 0.

Example:

 my $hash_ref = {
         A => 'one',
         B => [ 'one', 'two' ],
         C => { 
             'one' => 1, 
             'two' => 2, 
         },
 };
 
 printf "Output: %s", every_keys( $hash_ref, [ ['A'], ['B', 0], ['C', 'one'] ] );
 # Output: 0
 printf "Output: %s", every_keys( $hash_ref, [ ['A'], ['C', 'one'], ['C', 'two'] ] );
 # Output: 1

some_keys($$)

some_keys( $hash_ref, $list_array_ref ) - Verify list of hash keys for existence.

Return:

If exist at least one key of hash of the submitted list then return 1, otherwise - 0.

Example:

 my $hash_ref = {
         A => 'one',
         B => [ 'one', 'two' ],
         C => { 
             'one' => 1, 
             'two' => 2, 
         },
 };
 
 printf "Output: %s", some_keys( $hash_ref, [ ['A'], ['B', 0], ['C', 'one'] ] );
 # Output: 1
 printf "Output: %s", some_keys( $hash_ref, [ ['A', 'one'], ['B', 0], ['C', 'three'] ] );
 # Output: 0

SEE ALSO

Hash::Util, Deep::Hash::Utils

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>