File Coverage

blib/lib/Data/Check/Structure.pm
Criterion Covered Total %
statement 96 96 100.0
branch 47 60 78.3
condition 37 50 74.0
subroutine 13 13 100.0
pod 10 10 100.0
total 203 229 88.6


line stmt bran cond sub pod time code
1             package Data::Check::Structure;
2              
3             our $DATE = '2014-07-14'; # DATE
4             our $VERSION = '0.03'; # VERSION
5              
6 1     1   21845 use 5.010001;
  1         4  
  1         41  
7 1     1   5 use strict;
  1         2  
  1         32  
8 1     1   9 use warnings;
  1         2  
  1         1024  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             is_aoa
14             is_aoaos
15             is_aoh
16             is_aohos
17             is_aos
18             is_hoa
19             is_hoaos
20             is_hoh
21             is_hohos
22             is_hos
23             );
24              
25             sub is_aos {
26 15     15 1 635 my ($data, $opts) = @_;
27 15   100     51 $opts //= {};
28 15         26 my $max = $opts->{max};
29              
30 15 100       46 return 0 unless ref($data) eq 'ARRAY';
31 14         36 for my $i (0..@$data-1) {
32 15 100 100     47 last if defined($max) && $i >= $max;
33 14 100       56 return 0 if ref($data->[$i]);
34             }
35 11         45 1;
36             }
37              
38             sub is_aoa {
39 4     4 1 1071 my ($data, $opts) = @_;
40 4   100     24 $opts //= {};
41 4         8 my $max = $opts->{max};
42              
43 4 50       14 return 0 unless ref($data) eq 'ARRAY';
44 4         13 for my $i (0..@$data-1) {
45 8 100 100     39 last if defined($max) && $i >= $max;
46 7 100       31 return 0 unless ref($data->[$i]) eq 'ARRAY';
47             }
48 3         17 1;
49             }
50              
51             sub is_aoaos {
52 4     4 1 1657 my ($data, $opts) = @_;
53 4   100     24 $opts //= {};
54 4         8 my $max = $opts->{max};
55              
56 4 50       19 return 0 unless ref($data) eq 'ARRAY';
57 4         10 my $aos_opts = {max=>$max};
58 4         16 for my $i (0..@$data-1) {
59 8 100 100     37 last if defined($max) && $i >= $max;
60 7 100       21 return 0 unless is_aos($data->[$i], $aos_opts);
61             }
62 3         20 1;
63             }
64              
65             sub is_aoh {
66 4     4 1 1740 my ($data, $opts) = @_;
67 4   100     24 $opts //= {};
68 4         9 my $max = $opts->{max};
69              
70 4 50       15 return 0 unless ref($data) eq 'ARRAY';
71 4         12 for my $i (0..@$data-1) {
72 8 100 100     42 last if defined($max) && $i >= $max;
73 7 100       27 return 0 unless ref($data->[$i]) eq 'HASH';
74             }
75 3         15 1;
76             }
77              
78             sub is_aohos {
79 4     4 1 1624 my ($data, $opts) = @_;
80 4   100     24 $opts //= {};
81 4         44 my $max = $opts->{max};
82              
83 4 50       16 return 0 unless ref($data) eq 'ARRAY';
84 4         12 my $hos_opts = {max=>$max};
85 4         13 for my $i (0..@$data-1) {
86 8 100 100     39 last if defined($max) && $i >= $max;
87 7 100       23 return 0 unless is_hos($data->[$i], $hos_opts);
88             }
89 3         15 1;
90             }
91              
92             sub is_hos {
93 14     14 1 1635 my ($data, $opts) = @_;
94 14   100     46 $opts //= {};
95 14         23 my $max = $opts->{max};
96              
97 14 100       46 return 0 unless ref($data) eq 'HASH';
98 13         17 my $i = 0;
99 13         42 for my $k (keys %$data) {
100 10 50 66     31 last if defined($max) && ++$i >= $max;
101 10 100       55 return 0 if ref($data->{$k});
102             }
103 10         48 1;
104             }
105              
106             sub is_hoa {
107 3     3 1 1161 my ($data, $opts) = @_;
108 3   50     16 $opts //= {};
109 3         6 my $max = $opts->{max};
110              
111 3 50       10 return 0 unless ref($data) eq 'HASH';
112 3         4 my $i = 0;
113 3         9 for my $k (keys %$data) {
114 2 50 33     8 last if defined($max) && ++$i >= $max;
115 2 100       12 return 0 unless ref($data->{$k}) eq 'ARRAY';
116             }
117 2         10 1;
118             }
119              
120             sub is_hoaos {
121 5     5 1 1179 my ($data, $opts) = @_;
122 5   50     26 $opts //= {};
123 5         9 my $max = $opts->{max};
124              
125 5 50       15 return 0 unless ref($data) eq 'HASH';
126 5         8 my $i = 0;
127 5         17 for my $k (keys %$data) {
128 4 50 33     13 last if defined($max) && ++$i >= $max;
129 4 100       11 return 0 unless is_aos($data->{$k});
130             }
131 3         593 1;
132             }
133              
134             sub is_hoh {
135 3     3 1 1257 my ($data, $opts) = @_;
136 3   50     18 $opts //= {};
137 3         5 my $max = $opts->{max};
138              
139 3 50       10 return 0 unless ref($data) eq 'HASH';
140 3         6 my $i = 0;
141 3         8 for my $k (keys %$data) {
142 2 50 33     6 last if defined($max) && ++$i >= $max;
143 2 100       13 return 0 unless ref($data->{$k}) eq 'HASH';
144             }
145 2         9 1;
146             }
147              
148             sub is_hohos {
149 5     5 1 1132 my ($data, $opts) = @_;
150 5   50     24 $opts //= {};
151 5         9 my $max = $opts->{max};
152              
153 5 50       15 return 0 unless ref($data) eq 'HASH';
154 5         7 my $i = 0;
155 5         15 for my $k (keys %$data) {
156 4 50 33     11 last if defined($max) && ++$i >= $max;
157 4 100       10 return 0 unless is_hos($data->{$k});
158             }
159 3         12 1;
160             }
161              
162             1;
163             # ABSTRACT: Check structure of data
164              
165             __END__
166              
167             =pod
168              
169             =encoding UTF-8
170              
171             =head1 NAME
172              
173             Data::Check::Structure - Check structure of data
174              
175             =head1 VERSION
176              
177             This document describes version 0.03 of Data::Check::Structure (from Perl distribution Data-Check-Structure), released on 2014-07-14.
178              
179             =head1 SYNOPSIS
180              
181             =head1 DESCRIPTION
182              
183             This small module provides several simple routines to check the structure of
184             data, e.g. whether data is an array of arrays ("aoa"), array of scalars ("aos"),
185             and so on.
186              
187             =head1 FUNCTIONS
188              
189             =head2 is_aos($data, \%opts) => bool
190              
191             Check that data is an array of scalars. Examples:
192              
193             is_aos([]); # true
194             is_aos(['a', 'b']); # true
195             is_aos(['a', []]); # false
196             is_aos([1,2,3, []], {max=>3}); # true
197              
198             Known options: C<max> (maximum number of items to check, undef means check all
199             items).
200              
201             =head2 is_aoa($data, \%opts) => bool
202              
203             Check that data is an array of arrays. Examples:
204              
205             is_aoa([]); # true
206             is_aoa([[1], [2]]); # true
207             is_aoa([[1], 'a']); # false
208             is_aoa([[1],[],[], 'a'], {max=>3}); # true
209              
210             Known options: C<max> (maximum number of items to check, undef means check all
211             items).
212              
213             =head2 is_aoaos($data, \%opts) => bool
214              
215             Check that data is an array of arrays of scalars. Examples:
216              
217             is_aoaos([]); # true
218             is_aoaos([[1], [2]]); # true
219             is_aoaos([[1], [{}]]); # false
220             is_aoaos([[1],[],[], [{}]], {max=>3}); # true
221              
222             Known options: C<max> (maximum number of items to check, undef means check all
223             items).
224              
225             =head2 is_aoh($data, \%opts) => bool
226              
227             Check that data is an array of hashes. Examples:
228              
229             is_aoh([]); # true
230             is_aoh([{}, {a=>1}]); # true
231             is_aoh([{}, 'a']); # false
232             is_aoh([{},{},{a=>1}, 'a'], {max=>3}); # true
233              
234             Known options: C<max> (maximum number of items to check, undef means check all
235             items).
236              
237             =head2 is_aohos($data, \%opts) => bool
238              
239             Check that data is an array of hashes of scalars. Examples:
240              
241             is_aohos([]); # true
242             is_aohos([{a=>1}, {}]); # true
243             is_aohos([{a=>1}, {b=>[]}]); # false
244             is_aohos([{a=>1},{},{}, {b=>[]}], {max=>3}); # true
245              
246             Known options: C<max> (maximum number of items to check, undef means check all
247             items).
248              
249             =head2 is_hos($data, \%opts) => bool
250              
251             Check that data is a hash of scalars. Examples:
252              
253             is_hos({}); # true
254             is_hos({a=>1, b=>2}); # true
255             is_hos({a=>1, b=>[]}); # false
256             is_hos({a=>1, b=>2, c=>3, d=>[]}, {max=>3}); # true (or false, depending on random hash key ordering)
257              
258             Known options: C<max> (maximum number of items to check, undef means check all
259             items).
260              
261             =head2 is_hoa($data, \%opts) => bool
262              
263             Check that data is a hash of arrays. Examples:
264              
265             is_hoa({}) ); # true
266             is_hoa({a=>[]}) ); # true
267             is_hoa({a=>1}) ); # false
268              
269             Known options: C<max> (maximum number of items to check, undef means check all
270             items).
271              
272             =head2 is_hoaos($data, \%opts) => bool
273              
274             Check that data is a hash of arrays of scalars. Examples:
275              
276             is_hoaos({}) ); # true
277             is_hoaos({a=>[]}) ); # true
278             is_hoaos({a=>[1]}) ); # true
279             is_hoaos({a=>1}) ); # false
280             is_hoaos({a=>[{}]}) ); # false
281              
282             Known options: C<max> (maximum number of items to check, undef means check all
283             items).
284              
285             =head2 is_hoh($data, \%opts) => bool
286              
287             Check that data is a hash of hashes. Examples:
288              
289             is_hoh({}) ); # true
290             is_hoh({a=>{}}) ); # true
291             is_hoh({a=>1}) ); # false
292              
293             Known options: C<max> (maximum number of items to check, undef means check all
294             items).
295              
296             =head2 is_hohos($data, \%opts) => bool
297              
298             Check that data is a hash of hashes of scalrs. Examples:
299              
300             is_hohos({}) ); # true
301             is_hohos({a=>{}}) ); # true
302             is_hohos({a=>{b=>1}}) ); # true
303             is_hohos({a=>1}) ); # false
304             is_hohos({a=>{b=>[]}}) ); # false
305              
306             Known options: C<max> (maximum number of items to check, undef means check all
307             items).
308              
309             =head1 HOMEPAGE
310              
311             Please visit the project's homepage at L<https://metacpan.org/release/Data-Check-Structure>.
312              
313             =head1 SOURCE
314              
315             Source repository is at L<https://github.com/sharyanto/perl-Data-Check-Structure>.
316              
317             =head1 BUGS
318              
319             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Check-Structure>
320              
321             When submitting a bug or request, please include a test-file or a
322             patch to an existing test-file that illustrates the bug or desired
323             feature.
324              
325             =head1 AUTHOR
326              
327             Steven Haryanto <stevenharyanto@gmail.com>
328              
329             =head1 COPYRIGHT AND LICENSE
330              
331             This software is copyright (c) 2014 by Steven Haryanto.
332              
333             This is free software; you can redistribute it and/or modify it under
334             the same terms as the Perl 5 programming language system itself.
335              
336             =cut