| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | #* | ||||||||||||||||||||||||||||
| 3 | #* Name: Params::Dry::Types | ||||||||||||||||||||||||||||
| 4 | #* Info: Types definitions module | ||||||||||||||||||||||||||||
| 5 | #* Author: Pawel Guspiel (neo77) <neo@cpan.org> | ||||||||||||||||||||||||||||
| 6 | #* | ||||||||||||||||||||||||||||
| 7 | #* This module keeps validation functions. You can of course add your modules which uses this and will add additional checks | ||||||||||||||||||||||||||||
| 8 | #* Build in types for Params::Dry | ||||||||||||||||||||||||||||
| 9 | #* | ||||||||||||||||||||||||||||
| 10 | package Params::Dry::Types; | ||||||||||||||||||||||||||||
| 11 | { | ||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # --- version --- | ||||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = 1.20_03; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #=------------------------------------------------------------------------ { use, constants } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use constant PASS => 1; # pass test | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use constant FAIL => 0; # test fail | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #=------------------------------------------------------------------------ { export } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use Exporter; # to export _ rq and opt | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 4 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | our @EXPORT_OK = qw(PASS FAIL); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | our %EXPORT_TAGS = ( const => [qw(PASS FAIL)], ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | #=------------------------------------------------------------------------ { module public functions } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | #=--------- | ||||||||||||||||||||||||||||
| 38 | # String | ||||||||||||||||||||||||||||
| 39 | #=--------- | ||||||||||||||||||||||||||||
| 40 | #* string type check (parameter sets max length) | ||||||||||||||||||||||||||||
| 41 | #* RETURN: PASS if test pass otherwise FAIL | ||||||||||||||||||||||||||||
| 42 | 17 | ▶ | sub String { | ||||||||||||||||||||||||||
|
Subroutine
String: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 17 | ▶ | ref( $_[0] ) and return FAIL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 16 | ▶ | $_[1] and length $_[0] > $_[1] and return FAIL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[1] and length $_[0] > $_[1]
MC/DC100%2/2
$_[1] and length $_[0] > $_[1]
$_[1]
length $_[0] > $_[1]
Truth table100%3/3
A and B
A= $_[1]B= length $_[0] > $_[1]
| |||||||||||||||||||||||||||||
| 45 | 12 | PASS; | |||||||||||||||||||||||||||
| 46 | } #+ end of: sub String | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | #=--------- | ||||||||||||||||||||||||||||
| 49 | # Object | ||||||||||||||||||||||||||||
| 50 | #=--------- | ||||||||||||||||||||||||||||
| 51 | #* Object type check, Object - just object, or Object(Params::Dry::Types) check if is Params::Dry::Types type | ||||||||||||||||||||||||||||
| 52 | #* RETURN: PASS if test pass otherwise FAIL | ||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub Object { | ||||||||||||||||||||||||||
|
Subroutine
Object: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 4 | my $class = blessed( $_[0] ); | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | return FAIL if !$class; # not an object | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | return FAIL if $_[1] and ( $_[1] ne $class ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[1] and $_[1] ne $class
MC/DC0%0/2
$_[1] and $_[1] ne $class
$_[1]
$_[1] ne $class
Truth table33%1/3
A and B
A= $_[1]B= $_[1] ne $class
| |||||||||||||||||||||||||||||
| 57 | 1 | PASS; | |||||||||||||||||||||||||||
| 58 | } #+ end of: sub Object | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | #=------ | ||||||||||||||||||||||||||||
| 61 | # Ref | ||||||||||||||||||||||||||||
| 62 | #=------ | ||||||||||||||||||||||||||||
| 63 | #* ref type check | ||||||||||||||||||||||||||||
| 64 | #* RETURN: PASS if test pass otherwise FAIL | ||||||||||||||||||||||||||||
| 65 | 24 | ▶ | sub Ref { | ||||||||||||||||||||||||||
|
Subroutine
Ref: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 24 | ▶ | my $ref = ref( $_[0] ) or return FAIL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 20 | ▶ | return FAIL if $_[1] and $ref ne $_[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[1] and $ref ne $_[1]
MC/DC100%2/2
$_[1] and $ref ne $_[1]
$_[1]
$ref ne $_[1]
Truth table100%3/3
A and B
A= $_[1]B= $ref ne $_[1]
| |||||||||||||||||||||||||||||
| 69 | 18 | PASS; | |||||||||||||||||||||||||||
| 70 | } #+ end of: sub Ref | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | #=---------- | ||||||||||||||||||||||||||||
| 73 | # Defined | ||||||||||||||||||||||||||||
| 74 | #=---------- | ||||||||||||||||||||||||||||
| 75 | #* Allows anything what is defined | ||||||||||||||||||||||||||||
| 76 | #* RETURN: PASS if defined | ||||||||||||||||||||||||||||
| 77 | 3 | ▶ | sub Defined { | ||||||||||||||||||||||||||
|
Subroutine
Defined: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 3 | ▶ | defined $_[0] ? PASS : FAIL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } #+ end of: sub Defined | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #=-------- | ||||||||||||||||||||||||||||
| 82 | # Value | ||||||||||||||||||||||||||||
| 83 | #=-------- | ||||||||||||||||||||||||||||
| 84 | #* Allows anything what is not a reference | ||||||||||||||||||||||||||||
| 85 | #* RETURN: PASS if defined | ||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub Value { | ||||||||||||||||||||||||||
|
Subroutine
Value: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | $_[0] and ref $_[0] ? FAIL : PASS; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | } #+ end of: sub Value | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 4 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | #+ Number - mapped types | ||||||||||||||||||||||||||||
| 94 | 4 | *Params::Dry::Types::Int = *Params::Dry::Types::Number::Int; | |||||||||||||||||||||||||||
| 95 | 4 | *Params::Dry::Types::Float = *Params::Dry::Types::Number::Float; | |||||||||||||||||||||||||||
| 96 | 4 | *Params::Dry::Types::Bool = *Params::Dry::Types::Number::Bool; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | #+ Ref - mapped types | ||||||||||||||||||||||||||||
| 99 | 4 | *Params::Dry::Types::Scalar = *Params::Dry::Types::Ref::Scalar; | |||||||||||||||||||||||||||
| 100 | 4 | *Params::Dry::Types::Array = *Params::Dry::Types::Ref::Array; | |||||||||||||||||||||||||||
| 101 | 4 | *Params::Dry::Types::Hash = *Params::Dry::Types::Ref::Hash; | |||||||||||||||||||||||||||
| 102 | 4 | *Params::Dry::Types::Code = *Params::Dry::Types::Ref::Code; | |||||||||||||||||||||||||||
| 103 | 4 | *Params::Dry::Types::Regexp = *Params::Dry::Types::Ref::Regexp; | |||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||
| 107 | 4 | 0115 && 0x4d; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | #+ End of Params::Dry::Types | ||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||