| 1 | package Type::Alias; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | our $VERSION = "0.06"; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use feature qw(state); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Types::Standard qw(Dict Tuple Undef Bool); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Types::Equal qw( Eq NumEq ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 9 | ▶ | use constant AVAILABLE_BUILTIN => $] >= 5.036; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | use constant True => Type::Tiny->new(name => 'True', parent => Bool, constraint => sub { $_ eq !!1 }); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | use constant False => Type::Tiny->new(name => 'False', parent => Bool, constraint => sub { $_ eq !!0 }); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | if (AVAILABLE_BUILTIN) { | |||||||||||||||||||||||||||
| 19 | 9 | ▶ | eval q! | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
created_as_number: called CC=1 SCAR=0.0
Subroutine
is_bool: called CC=1 SCAR=0.0
Pod
undocumented
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | use experimental qw(builtin); | ||||||||||||||||||||||||||||
| 21 | sub is_bool { builtin::is_bool($_[0]) } | ||||||||||||||||||||||||||||
| 22 | sub created_as_number { builtin::created_as_number($_[0]) } | ||||||||||||||||||||||||||||
| 23 | !; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | else { | ||||||||||||||||||||||||||||
| 26 | eval q! | ||||||||||||||||||||||||||||
| 27 | sub is_bool { die 'This perl version does not support builtin::is_bool' } | ||||||||||||||||||||||||||||
| 28 | sub created_as_number { die 'This perl version does not support builtin::created_as_number' } | ||||||||||||||||||||||||||||
| 29 | !; | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 7 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 7 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 7 | my $target_package = caller; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | $class->_define_type($target_package, $args{type}); | |||||||||||||||||||||||||||
| 38 | 7 | $class->_predefine_type_aliases($target_package, $args{'-alias'}); | |||||||||||||||||||||||||||
| 39 | 7 | $class->_predefine_type_functions($target_package, $args{'-fun'}); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 7 | ▶ | sub _define_type { | ||||||||||||||||||||||||||
|
Subroutine
_define_type: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 43 | 7 | my ($class, $target_package, $options) = @_; | |||||||||||||||||||||||||||
| 44 | 7 | ▶ | $options //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options //= {}
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||
| 45 | 7 | ▶ | my $type_name = $options->{'-as'} // 'type'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options->{"-as"} // "type"
MC/DC0%0/1
$options->{"-as"} // "type"
$options->{"-as"}
Truth table50%1/2
A
A= $options->{"-as"}
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 7 | ▶ | if ($target_package->can($type_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | croak "Alreay exists function '${target_package}::${type_name}'. Can specify another name: type => { -as => 'XXX' }."; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 9 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | no warnings qw(once); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | *{"${target_package}::${type_name}"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 9 | my ($alias_name, $type_args) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 9 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | no warnings qw(redefine); # Already define empty type alias at _import_type_aliases | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 9 | *{"${target_package}::${alias_name}"} = generate_type_alias($type_args); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 7 | } | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | ▶ | sub _predefine_type_aliases { | ||||||||||||||||||||||||||
|
Subroutine
_predefine_type_aliases: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 63 | 7 | my ($class, $target_package, $type_aliases) = @_; | |||||||||||||||||||||||||||
| 64 | 7 | ▶ | $type_aliases //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$type_aliases //= []
MC/DC100%1/1
$type_aliases //= []
$type_aliases
Truth table100%2/2
A
A= $type_aliases
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 7 | for my $alias_name (@$type_aliases) { | |||||||||||||||||||||||||||
| 67 | 5 | ▶ | if ($target_package->can($alias_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | croak "Cannot predeclare type alias '${target_package}::${alias_name}'."; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 9 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 5 | ▶ | *{"${target_package}::${alias_name}"} = sub :prototype() { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 73 | 0 | croak "You should define type alias '$alias_name' before using it." | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 5 | } | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 7 | ▶ | sub _predefine_type_functions { | ||||||||||||||||||||||||||
|
Subroutine
_predefine_type_functions: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 79 | 7 | my ($class, $target_package, $type_functions) = @_; | |||||||||||||||||||||||||||
| 80 | 7 | ▶ | $type_functions //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$type_functions //= []
MC/DC100%1/1
$type_functions //= []
$type_functions
Truth table100%2/2
A
A= $type_functions
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | for my $type_function (@$type_functions) { | |||||||||||||||||||||||||||
| 83 | 1 | ▶ | if ($target_package->can($type_function)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | croak "Cannot predeclare type function '${target_package}::${type_function}'."; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 9 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | *{"${target_package}::${type_function}"} = sub :prototype(;$) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 89 | 0 | croak "You should define type function '$type_function' before using it." | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | } | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 95 | ▶ | sub to_type { | ||||||||||||||||||||||||||
|
Subroutine
to_type: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 95 | my $v = shift; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 95 | ▶ | if (blessed($v)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 98 | 68 | _to_type_object($v); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | elsif (ref $v) { | ||||||||||||||||||||||||||||
| 101 | 19 | _to_type_reference($v); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 8 | _to_type_scalar($v); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 68 | ▶ | sub _to_type_object { | ||||||||||||||||||||||||||
|
Subroutine
_to_type_object: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 109 | 68 | my $v = $_[0]; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 68 | ▶ | if ($v->can('check') && $v->can('get_message')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$v->can("check") and $v->can("get_message")
MC/DC50%1/2
$v->can("check") and $v->can("get_message")
$v->can("check")
$v->can("get_message")
Truth table66%2/3
A and B
A= $v->can("check")B= $v->can("get_message")
| |||||||||||||||||||||||||||||
| 112 | 66 | return $v; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||
| 115 | 2 | croak 'This object is not supported: '. ref $v; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 19 | ▶ | sub _to_type_reference { | ||||||||||||||||||||||||||
|
Subroutine
_to_type_reference: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 120 | 19 | my $v = $_[0]; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 19 | ▶ | if (ref $v eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 123 | 8 | return Tuple[ map { to_type($_) } @$v ]; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | elsif (ref $v eq 'HASH') { | ||||||||||||||||||||||||||||
| 126 | return Dict[ | ||||||||||||||||||||||||||||
| 127 | 7 | map { $_ => to_type($v->{$_}) } sort { $a cmp $b } keys %$v | |||||||||||||||||||||||||||
| 128 | ]; | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | elsif (ref $v eq 'CODE') { | ||||||||||||||||||||||||||||
| 131 | 17 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 132 | 17 | my @args; | |||||||||||||||||||||||||||
| 133 | 17 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 15 | ▶ | unless (@_ == 1 && ref $_[0] eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 135 | 1 | croak 'This type requires an array reference'; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 14 | @args = map { to_type($_) } @{$_[0]}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 16 | to_type($v->(@args)); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 3 | } | |||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | 1 | croak 'This reference is not supported: ' . ref $v ; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 8 | ▶ | sub _to_type_scalar { | ||||||||||||||||||||||||||
|
Subroutine
_to_type_scalar: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 149 | 8 | my $v = $_[0]; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 8 | if (AVAILABLE_BUILTIN) { | |||||||||||||||||||||||||||
| 152 | 8 | ▶ | if (!defined $v) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 153 | 1 | return Undef; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | elsif (is_bool($v)) { | ||||||||||||||||||||||||||||
| 156 | 2 | ▶ | $v ? True : False; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | elsif (created_as_number($v)) { | ||||||||||||||||||||||||||||
| 159 | 2 | NumEq[$v]; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { # string | ||||||||||||||||||||||||||||
| 162 | 3 | Eq[$v]; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | else { | ||||||||||||||||||||||||||||
| 166 | if (!defined $v) { | ||||||||||||||||||||||||||||
| 167 | return Undef; | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { # string, number, bool | ||||||||||||||||||||||||||||
| 170 | Eq[$v]; | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 13 | ▶ | sub generate_type_alias { | ||||||||||||||||||||||||||
|
Subroutine
generate_type_alias: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 13 | my ($type_args) = @_; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 13 | ▶ | if ( (ref $type_args||'') eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $type_args || ''
MC/DC0%0/1
ref $type_args || ''
ref $type_args
Truth table50%1/2
A
A= ref $type_args
| |||||||||||||||||||||||||||||
| 179 | 12 | ▶ | return sub :prototype(;$) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 180 | 12 | state $type = to_type($type_args); | |||||||||||||||||||||||||||
| 181 | 12 | $type->(@_); | |||||||||||||||||||||||||||
| 182 | 2 | }; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | else { | ||||||||||||||||||||||||||||
| 185 | 33 | ▶ | return sub :prototype() { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 186 | 33 | state $type = to_type($type_args); | |||||||||||||||||||||||||||
| 187 | 33 | $type; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 11 | } | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 9 | 1; | |||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||