| 1 | ############################################################################## | ||||||||||||||||||||
| 2 | # Package/use statements | ||||||||||||||||||||
| 3 | ############################################################################## | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | package Params::Check::Item; | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 3 | ▶ | use Carp::Assert qw(assert); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 3 | ▶ | use Scalar::Util qw(blessed looks_like_number); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 3 | require Exporter; | |||||||||||||||||||
| 16 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 17 | 3 | 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 | our $VERSION = '0.02'; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | ############################################################################## | ||||||||||||||||||||
| 33 | # private definitions | ||||||||||||||||||||
| 34 | ############################################################################## | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 56 | ▶ | sub _report { | ||||||||||||||||||
|
Subroutine
_report: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 37 | 56 | my ($res, $msg) = @_[0..1]; | |||||||||||||||||||
| 38 | 56 | ▶ | if($msg) { assert($res, $msg) if !$ENV{PARAMS_CHECK_ITEM_OFF}; } | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 39 | 38 | ▶ | else { assert($res) if !$ENV{PARAMS_CHECK_ITEM_OFF}; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 31 | return 1; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | ############################################################################## | ||||||||||||||||||||
| 44 | # public definitions | ||||||||||||||||||||
| 45 | ############################################################################## | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 9 | ▶ | sub checkNumber { | ||||||||||||||||||
|
Subroutine
checkNumber: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 48 | 9 | my ($a, $msg) = @_[0..1]; | |||||||||||||||||||
| 49 | 9 | return _report(looks_like_number($a), $msg); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 7 | ▶ | sub checkInteger { | ||||||||||||||||||
|
Subroutine
checkInteger: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 53 | 7 | my ($a, $msg) = @_[0..1]; | |||||||||||||||||||
| 54 | 7 | ▶ | return _report(($a =~ /^-?\d+$/) || 0, $msg); | ||||||||||||||||||
|
Condition100%2/2
$a =~ /^-?\d+$/ || 0
MC/DC100%1/1
$a =~ /^-?\d+$/ || 0
$a =~ /^-?\d+$/
Truth table100%2/2
A
A= $a =~ /^-?\d+$/
| |||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 7 | ▶ | sub checkIndex { | ||||||||||||||||||
|
Subroutine
checkIndex: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 58 | 7 | my ($a, $msg) = @_[0..1]; | |||||||||||||||||||
| 59 | 7 | ▶ | return _report(($a =~ /^\d+$/) || 0, $msg); | ||||||||||||||||||
|
Condition100%2/2
$a =~ /^\d+$/ || 0
MC/DC100%1/1
$a =~ /^\d+$/ || 0
$a =~ /^\d+$/
Truth table100%2/2
A
A= $a =~ /^\d+$/
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 7 | ▶ | sub checkClass { | ||||||||||||||||||
|
Subroutine
checkClass: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 63 | 7 | my ($arg, $type, $msg) = @_[0..2]; | |||||||||||||||||||
| 64 | 7 | ▶ | _report((blessed($arg) && $arg->isa($type)), $msg); | ||||||||||||||||||
|
Condition66%2/3
builtin::blessed($arg) && $arg->isa($type)
MC/DC50%1/2
builtin::blessed($arg) && $arg->isa($type)
builtin::blessed($arg)
$arg->isa($type)
Truth table66%2/3
A && B
A= builtin::blessed($arg)B= $arg->isa($type)
| |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 4 | ▶ | sub checkNumEQ { | ||||||||||||||||||
|
Subroutine
checkNumEQ: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 68 | 4 | my ($a, $b, $msg) = @_[0..2]; | |||||||||||||||||||
| 69 | 4 | _report($a == $b, $msg); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 4 | ▶ | sub checkNumNEQ { | ||||||||||||||||||
|
Subroutine
checkNumNEQ: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 73 | 4 | my ($a, $b, $msg) = @_[0..2]; | |||||||||||||||||||
| 74 | 4 | _report($a != $b, $msg); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 4 | ▶ | sub checkNumLT { | ||||||||||||||||||
|
Subroutine
checkNumLT: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 78 | 4 | my ($a, $b, $msg) = @_[0..2]; | |||||||||||||||||||
| 79 | 4 | _report($a < $b, $msg); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 5 | ▶ | sub checkNumLTE { | ||||||||||||||||||
|
Subroutine
checkNumLTE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 83 | 5 | my ($a, $b, $msg) = @_[0..2]; | |||||||||||||||||||
| 84 | 5 | _report($a <= $b, $msg); | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 5 | ▶ | sub checkOneOf { | ||||||||||||||||||
|
Subroutine
checkOneOf: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 88 | 5 | my ($a, $listRef, $msg) = @_[0..2]; | |||||||||||||||||||
| 89 | 5 | _report(scalar(grep(/^$a$/, @{$listRef})), $msg); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 4 | ▶ | sub checkImpl { | ||||||||||||||||||
|
Subroutine
checkImpl: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 93 | 4 | ▶ | my ($msg) = ($_[0] || ""); | ||||||||||||||||||
|
Condition100%2/2
$_[0] || ''
MC/DC100%1/1
$_[0] || ''
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||
| 94 | 4 | _report(0, "Not Implemented: $msg"); | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 3 | 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 | |||||||||||||||||||||