File Coverage

blib/lib/Params/Check/Item.pm
Criterion Covered Total %
statement 44 44 100.0
branch 6 6 100.0
condition 8 9 88.8
subroutine 17 17 100.0
pod 10 10 100.0
total 85 86 98.8


line stmt bran cond sub pod time code
1             ##############################################################################
2             # Package/use statements
3             ##############################################################################
4              
5             package Params::Check::Item;
6              
7 3     3   146748 use 5.006;
  3         9  
8 3     3   13 use strict;
  3         6  
  3         83  
9 3     3   14 use warnings;
  3         9  
  3         81  
10              
11 3     3   1320 use Carp::Assert qw(assert);
  3         2736  
  3         15  
12 3     3   327 use Scalar::Util qw(blessed looks_like_number);
  3         5  
  3         239  
13              
14             BEGIN {
15 3     3   15 require Exporter;
16 3         21 our @ISA = qw(Exporter);
17 3         9 our @EXPORT = qw(
18             checkNumber
19             checkInteger
20             checkIndex
21             checkClass
22             checkNumEQ
23             checkNumNEQ
24             checkNumLT
25             checkNumLTE
26             checkOneOf
27             checkImpl
28             );
29 3         1526 our $VERSION = '0.02';
30             }
31              
32             ##############################################################################
33             # private definitions
34             ##############################################################################
35              
36             sub _report {
37 56     56   137 my ($res, $msg) = @_[0..1];
38 56 100       119 if($msg) { assert($res, $msg) if !$ENV{PARAMS_CHECK_ITEM_OFF}; }
  18 100       83  
39 38 100       128 else { assert($res) if !$ENV{PARAMS_CHECK_ITEM_OFF}; }
40 31         223 return 1;
41             }
42              
43             ##############################################################################
44             # public definitions
45             ##############################################################################
46              
47             sub checkNumber {
48 9     9 1 816 my ($a, $msg) = @_[0..1];
49 9         30 return _report(looks_like_number($a), $msg);
50             }
51              
52             sub checkInteger {
53 7     7 1 889 my ($a, $msg) = @_[0..1];
54 7   100     51 return _report(($a =~ /^-?\d+$/) || 0, $msg);
55             }
56              
57             sub checkIndex {
58 7     7 1 895 my ($a, $msg) = @_[0..1];
59 7   100     45 return _report(($a =~ /^\d+$/) || 0, $msg);
60             }
61              
62             sub checkClass {
63 7     7 1 991 my ($arg, $type, $msg) = @_[0..2];
64 7   66     44 _report((blessed($arg) && $arg->isa($type)), $msg);
65             }
66              
67             sub checkNumEQ {
68 4     4 1 978 my ($a, $b, $msg) = @_[0..2];
69 4         16 _report($a == $b, $msg);
70             }
71              
72             sub checkNumNEQ {
73 4     4 1 1589 my ($a, $b, $msg) = @_[0..2];
74 4         15 _report($a != $b, $msg);
75             }
76              
77             sub checkNumLT {
78 4     4 1 1534 my ($a, $b, $msg) = @_[0..2];
79 4         16 _report($a < $b, $msg);
80             }
81              
82             sub checkNumLTE {
83 5     5 1 1552 my ($a, $b, $msg) = @_[0..2];
84 5         18 _report($a <= $b, $msg);
85             }
86              
87             sub checkOneOf {
88 5     5 1 1607 my ($a, $listRef, $msg) = @_[0..2];
89 5         13 _report(scalar(grep(/^$a$/, @{$listRef})), $msg);
  5         68  
90             }
91              
92             sub checkImpl {
93 4   100 4 1 4545 my ($msg) = ($_[0] || "");
94 4         18 _report(0, "Not Implemented: $msg");
95             }
96              
97             1; # End of Params::Check::Item
98              
99             ##############################################################################
100             # Perldoc
101             ##############################################################################
102              
103             =head1 NAME
104              
105             Params::Check::Item - checks the type or value of an item at any point
106             during execution
107              
108             =head1 VERSION
109              
110             Version 0.02
111              
112             =head1 SYNOPSIS
113              
114             Params::Check::Items exports a bunch of utility functions for checking the
115             values of items through Carp::Assert. It is different than other parameter
116             checking modules since it has low configuration overhead and you are
117             expected to check one parameter at a time, instead of creating a giant hash
118             of all the valid values for all the parameters, such as Params::Check.
119             All exported functions optionally take in a message parameter at the end
120             that is displayed if Carp::Assert fails.
121              
122             If you would like to disable the checks, you must set the environment
123             variable PARAMS_CHECK_ITEM_OFF=1. Note that this can be toggled on and
124             off during runtime.
125              
126             An example:
127              
128             use Params::Check::Item;
129             sub myFunc {
130             my ($row, $col, $matrix) = @_[0..2];
131             checkIndex($row, "row not an index");
132             checkIndex($col, "col not an index");
133             checkClass($matrix, "My::Matrix::Type", "invalid matrix");
134             $matrix->get($row, $col);
135             }
136              
137             =head1 SUBROUTINES
138              
139             All exported subroutines take in an optional string message, [MSG],
140             to display on failure. Addtionally, the subroutines will not perform the
141             check if the environment variable PARAMS_CHECK_ITEM_OFF is set.
142              
143             =head2 checkNumber NUM,[MSG]
144              
145             checks that NUM is numeric type
146              
147             =head2 checkInteger NUM,[MSG]
148              
149             checks that NUM is an integer
150              
151             =head2 checkIndex NUM,[MSG]
152              
153             checks that NUM is a valid index (e.g., 0,1,2,3...)
154              
155             =head2 checkClass OBJECT,CLASSNAME,[MSG]
156              
157             checks that OBJECT is a blessed object from class of name CLASSNAME
158              
159             =head2 checkNumEQ NUM1,NUM2,[MSG]
160              
161             checks that NUM1 == NUM2
162              
163             =head2 checkNumNEQ NUM1,NUM2,[MSG]
164              
165             checks that NUM1 != NUM2
166              
167             =head2 checkNumLT NUM1,NUM2,[MSG]
168            
169             checks that NUM1 < NUM2
170              
171             =head2 checkNumLTE NUM1,NUM2,[MSG]
172              
173             checks that NUM1 <= NUM2
174              
175             =head2 checkOneOf ITEM,ARRAYREF,[MSG]
176              
177             checks that simple item ITEM is present in the array referenced by
178             ARRAYREF. This simply performs string comparison. It must be an exact
179             match with one of the items in the array to pass.
180              
181             =head2 checkImpl [MSG]
182              
183             simply fails and says "Not Implemented: [MSG]". good for stubbing out
184             functions.
185              
186             =head1 SEE ALSO
187              
188             Params::Check
189              
190             =head1 AUTHOR
191              
192             Samuel Steffl, C<sam@ssteffl.com>
193              
194             =head1 BUGS
195              
196             Please report any bugs or feature requests through the web interface at
197             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Check-Item>.
198             I will be notified, and then you'll automatically be notified of
199             progress on your bug as I make changes.
200              
201             =head1 SUPPORT
202              
203             You can find documentation for this module with the perldoc command.
204              
205             perldoc Params::Check::Item
206              
207             =head1 LICENSE AND COPYRIGHT
208              
209             Copyright 2017 Samuel Steffl.
210              
211             This is free software; you can redistribute it and/or modify it under the
212             same terms as the Perl 5 programming language system itself.
213