File Coverage

blib/lib/Mo/utils/Hash.pm
Criterion Covered Total %
statement 36 36 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 60 60 100.0


line stmt bran cond sub pod time code
1             package Mo::utils::Hash;
2              
3 4     4   192121 use base qw(Exporter);
  4         8  
  4         597  
4 4     4   43 use strict;
  4         9  
  4         101  
5 4     4   17 use warnings;
  4         7  
  4         236  
6              
7 4     4   2092 use Error::Pure qw(err);
  4         26078  
  4         99  
8 4     4   296 use Readonly;
  4         9  
  4         1388  
9              
10             Readonly::Array our @EXPORT_OK => qw(check_hash check_hash_keys);
11              
12             our $VERSION = 0.02;
13              
14             sub check_hash {
15 5     5 1 410679 my ($self, $key) = @_;
16              
17 5 100       23 if (! exists $self->{$key}) {
18 1         5 return;
19             }
20              
21 4 100       18 if (ref $self->{$key} ne 'HASH') {
22             err "Parameter '$key' isn't hash reference.",
23 3         21 'Reference', (ref $self->{$key}),
24             ;
25             }
26              
27 1         8 return;
28             }
29              
30             sub check_hash_keys {
31 6     6 1 305128 my ($self, $key, @hash_keys) = @_;
32              
33 6 100       23 if (! exists $self->{$key}) {
34 1         4 return;
35             }
36              
37 5 100       17 if (! @hash_keys) {
38 1         4 err "Expected keys doesn't exists.";
39             }
40              
41 4         13 my $hash = $self->{$key};
42 4         11 my $printable_keys = '';
43 4         10 foreach my $hash_key (@hash_keys) {
44 7 100       19 if ($printable_keys) {
45 3         7 $printable_keys .= '.';
46             }
47 7         15 $printable_keys .= $hash_key;
48 7 100 100     40 if (ref $hash ne 'HASH' || ! exists $hash->{$hash_key}) {
49 3         14 err "Parameter '$key' doesn't contain expected keys.",
50             'Keys', $printable_keys,
51             ;
52             }
53 4         10 $hash = $hash->{$hash_key};
54             }
55              
56 1         4 return;
57             }
58              
59             1;
60              
61             __END__
62              
63             =pod
64              
65             =encoding utf8
66              
67             =head1 NAME
68              
69             Mo::utils::Hash - Mo hash utilities.
70              
71             =head1 SYNOPSIS
72              
73             use Mo::utils::Hash qw(check_hash);
74              
75             check_hash($self, $key);
76             check_hash_keys($self, $key, @keys);
77              
78             =head1 DESCRIPTION
79              
80             Utilities for checking of hash values.
81              
82             =head1 SUBROUTINES
83              
84             =head2 C<check_hash>
85              
86             check_hash($self, $key);
87              
88             I<Since version 0.01.>
89              
90             Check parameter defined by C<$key> which is reference to hash.
91              
92             Put error if check isn't ok.
93              
94             Returns undef.
95              
96             =head2 C<check_hash_keys>
97              
98             check_hash_keys($self, $key, @keys);
99              
100             I<Since version 0.02.>
101              
102             Check parameter defined by C<$key> which contain hash keys defined by C<@keys>.
103              
104             Put error if check isn't ok.
105              
106             Returns undef.
107              
108             =head1 ERRORS
109              
110             check_hash():
111             Parameter '%s' isn't hash reference.
112             Reference: %s
113              
114             check_hash_keys():
115             Expected keys doesn't exists.
116             Parameter '%s' doesn't contain expected keys.
117             Keys: %s
118              
119              
120             =head1 EXAMPLE1
121              
122             =for comment filename=check_hash_ok.pl
123              
124             use strict;
125             use warnings;
126              
127             use Mo::utils::Hash qw(check_hash);
128              
129             my $self = {
130             'key' => {},
131             };
132             check_hash($self, 'key');
133              
134             # Print out.
135             print "ok\n";
136              
137             # Output:
138             # ok
139              
140             =head1 EXAMPLE2
141              
142             =for comment filename=check_hash_fail.pl
143              
144             use strict;
145             use warnings;
146              
147             use Error::Pure;
148             use Mo::utils::Hash qw(check_hash);
149              
150             $Error::Pure::TYPE = 'Error';
151              
152             my $self = {
153             'key' => 'bad',
154             };
155             check_hash($self, 'key');
156              
157             # Print out.
158             print "ok\n";
159              
160             # Output like:
161             # #Error [..Hash.pm:?] Parameter 'key' isn't hash reference.
162              
163             =head1 EXAMPLE3
164              
165             =for comment filename=check_hash_keys_ok.pl
166              
167             use strict;
168             use warnings;
169              
170             use Mo::utils::Hash 0.02 qw(check_hash_keys);
171              
172             my $self = {
173             'key' => {
174             'first' => {
175             'second' => 'value',
176             },
177             },
178             };
179             check_hash_keys($self, 'key', 'first', 'second');
180              
181             # Print out.
182             print "ok\n";
183              
184             # Output:
185             # ok
186              
187             =head1 EXAMPLE4
188              
189             =for comment filename=check_hash_keys_fail.pl
190              
191             use strict;
192             use warnings;
193              
194             use Error::Pure;
195             use Mo::utils::Hash 0.02 qw(check_hash_keys);
196              
197             $Error::Pure::TYPE = 'Error';
198              
199             my $self = {
200             'key' => {
201             'first' => {
202             'second_typo' => 'value',
203             }
204             },
205             };
206             check_hash_keys($self, 'key', 'first', 'second');
207              
208             # Print out.
209             print "ok\n";
210              
211             # Output like:
212             # #Error [..Hash.pm:?] Parameter 'key' doesn't contain expected keys.
213              
214             =head1 DEPENDENCIES
215              
216             L<Exporter>,
217             L<Error::Pure>,
218             L<Readonly>.
219              
220             =head1 SEE ALSO
221              
222             =over
223              
224             =item L<Mo::utils>
225              
226             Mo utilities.
227              
228             =back
229              
230             =head1 REPOSITORY
231              
232             L<https://github.com/michal-josef-spacek/Mo-utils-Hash>
233              
234             =head1 AUTHOR
235              
236             Michal Josef Špaček L<mailto:skim@cpan.org>
237              
238             L<http://skim.cz>
239              
240             =head1 LICENSE AND COPYRIGHT
241              
242             © 2025 Michal Josef Špaček
243              
244             BSD 2-Clause License
245              
246             =head1 VERSION
247              
248             0.02
249              
250             =cut