| 1 | package Sub::WrapInType; | ||||||||||||||
| 2 | 6 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 6 | ▶ | use parent 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 6 | ▶ | use Class::InsideOut qw( register readonly id ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use Types::Standard -types; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use Type::Params qw( signature ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 6 | ▶ | use Sub::Util qw( set_subname ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 6 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 6 | our $VERSION = '0.08'; | |||||||||||||
| 13 | 6 | our @EXPORT = qw( wrap_sub wrap_method install_sub install_method ); | |||||||||||||
| 14 | |||||||||||||||
| 15 | 6 | readonly params => my %params; | |||||||||||||
| 16 | 6 | readonly returns => my %returns; | |||||||||||||
| 17 | 6 | readonly code => my %code; | |||||||||||||
| 18 | 6 | readonly is_method => my %is_method; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 6 | my $TypeConstraint = HasMethods[qw( check get_message )]; | |||||||||||||
| 21 | 6 | my $ParamsTypes = $TypeConstraint | ArrayRef[$TypeConstraint] | Map[Str, $TypeConstraint]; | |||||||||||||
| 22 | 6 | my $ReturnTypes = $TypeConstraint | ArrayRef[$TypeConstraint]; | |||||||||||||
| 23 | 6 | my $Options = Dict[ | |||||||||||||
| 24 | skip_invocant => Optional[Bool], | ||||||||||||||
| 25 | check => Optional[Bool], | ||||||||||||||
| 26 | ]; | ||||||||||||||
| 27 | 6 | my $DEFAULT_OPTIONS = +{ | |||||||||||||
| 28 | skip_invocant => 0, | ||||||||||||||
| 29 | check => 1, | ||||||||||||||
| 30 | }; | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 34 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 33 | state $check = signature( | ||||||||||||||
| 34 | method => 1, | ||||||||||||||
| 35 | multi => [ | ||||||||||||||
| 36 | +{ | ||||||||||||||
| 37 | positional => [ | ||||||||||||||
| 38 | $ParamsTypes, | ||||||||||||||
| 39 | $ReturnTypes, | ||||||||||||||
| 40 | CodeRef, | ||||||||||||||
| 41 | 1 | ▶ | $Options, +{ default => sub { $DEFAULT_OPTIONS } } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | ], | ||||||||||||||
| 43 | }, | ||||||||||||||
| 44 | +{ | ||||||||||||||
| 45 | named_to_list => 1, | ||||||||||||||
| 46 | named => [ | ||||||||||||||
| 47 | params => $ParamsTypes, | ||||||||||||||
| 48 | isa => $ReturnTypes, | ||||||||||||||
| 49 | code => CodeRef, | ||||||||||||||
| 50 | 1 | ▶ | options => $Options, +{ default => sub { $DEFAULT_OPTIONS } }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 51 | 34 | ], | |||||||||||||
| 52 | }, | ||||||||||||||
| 53 | ], | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | 34 | my ($class, $params_types, $return_types, $code, $options) = $check->(@_); | |||||||||||||
| 56 | 34 | $options = +{ %$DEFAULT_OPTIONS, %$options }; | |||||||||||||
| 57 | |||||||||||||||
| 58 | my $typed_code = | ||||||||||||||
| 59 | $options->{check} | ||||||||||||||
| 60 | ? $class->_create_typed_code($params_types, $return_types, $code, $options) | ||||||||||||||
| 61 | 34 | ▶ | : sub { $code->(@_) }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | 34 | my $self = bless $typed_code, $class; | |||||||||||||
| 64 | 34 | register($self); | |||||||||||||
| 65 | |||||||||||||||
| 66 | { | ||||||||||||||
| 67 | 34 | my $addr = id $self; | |||||||||||||
| 68 | 34 | $params{$addr} = $params_types; | |||||||||||||
| 69 | 34 | $returns{$addr} = $return_types; | |||||||||||||
| 70 | 34 | $code{$addr} = $code; | |||||||||||||
| 71 | 34 | $is_method{$addr} = !!$options->{skip_invocant}; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 34 | $self; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 25 | ▶ | sub _create_typed_code { | ||||||||||||
|
Subroutine
_create_typed_code: called CC=7 SCAR=19.5
| |||||||||||||||
| 78 | 25 | my ($class, $params_types, $return_types, $code, $options) = @_; | |||||||||||||
| 79 | 25 | ▶ | my $params_types_checker = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 80 | ref $params_types eq 'ARRAY' ? signature(positional => $params_types) | ||||||||||||||
| 81 | : ref $params_types eq 'HASH' ? signature(named => [%$params_types], bless => 0) | ||||||||||||||
| 82 | : signature(positional => [$params_types]); | ||||||||||||||
| 83 | 25 | ▶ | my $return_types_checker = | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | ref $return_types eq 'ARRAY' ? signature(positional => $return_types) | ||||||||||||||
| 85 | : signature(positional => [$return_types]); | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 25 | ▶ | if ( ref $return_types eq 'ARRAY' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 3 | ▶ | if ( $options->{skip_invocant} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 1 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 90 | 1 | my @return_values = $code->( shift, $params_types_checker->(@_) ); | |||||||||||||
| 91 | 1 | $return_types_checker->(@return_values); | |||||||||||||
| 92 | 1 | @return_values; | |||||||||||||
| 93 | 1 | }; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | else { | ||||||||||||||
| 96 | 1 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 97 | 1 | my @return_values = $code->( $params_types_checker->(@_) ); | |||||||||||||
| 98 | 1 | $return_types_checker->(@return_values); | |||||||||||||
| 99 | 1 | @return_values; | |||||||||||||
| 100 | 2 | }; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | } | ||||||||||||||
| 103 | else { | ||||||||||||||
| 104 | 22 | ▶ | if ( $options->{skip_invocant} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 4 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
add: called CC=1 SCAR=0.0
| |||||||||||||||
| 106 | 4 | my $return_value = $code->( shift, $params_types_checker->(@_) ); | |||||||||||||
| 107 | 3 | $return_types_checker->($return_value); | |||||||||||||
| 108 | 2 | $return_value; | |||||||||||||
| 109 | 4 | }; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | else { | ||||||||||||||
| 112 | 11 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 113 | 11 | my $return_value = $code->( $params_types_checker->(@_) ); | |||||||||||||
| 114 | 9 | $return_types_checker->($return_value); | |||||||||||||
| 115 | 5 | $return_value; | |||||||||||||
| 116 | 18 | }; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | } | ||||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 24 | ▶ | sub _is_env_ndebug { | ||||||||||||
|
Subroutine
_is_env_ndebug: called CC=2 SCAR=6.9
| |||||||||||||||
| 122 | 24 | ▶ | $ENV{PERL_NDEBUG} || $ENV{NDEBUG}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 22 | ▶ | sub wrap_sub { | ||||||||||||
|
Subroutine
wrap_sub: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 126 | 22 | state $check = signature( | |||||||||||||
| 127 | message => << 'EOS', | ||||||||||||||
| 128 | USAGE: wrap_sub(\@parameter_types, $return_type, $subroutine) | ||||||||||||||
| 129 | or wrap_sub(params => \@params_types, returns => $return_types, code => $subroutine) | ||||||||||||||
| 130 | EOS | ||||||||||||||
| 131 | multi => [ | ||||||||||||||
| 132 | +{ positional => [ $ParamsTypes, $ReturnTypes, CodeRef ] }, | ||||||||||||||
| 133 | +{ | ||||||||||||||
| 134 | named_to_list => 1, | ||||||||||||||
| 135 | named => [ | ||||||||||||||
| 136 | params => $ParamsTypes, | ||||||||||||||
| 137 | isa => $ReturnTypes, | ||||||||||||||
| 138 | code => CodeRef, | ||||||||||||||
| 139 | ], | ||||||||||||||
| 140 | }, | ||||||||||||||
| 141 | ], | ||||||||||||||
| 142 | ); | ||||||||||||||
| 143 | 22 | my ($params_types, $return_types, $code) = $check->(@_); | |||||||||||||
| 144 | |||||||||||||||
| 145 | 17 | __PACKAGE__->new($params_types, $return_types, $code, +{ check => !_is_env_ndebug() }); | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | |||||||||||||||
| 148 | 7 | ▶ | sub wrap_method { | ||||||||||||
|
Subroutine
wrap_method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 149 | 7 | state $check = signature( | |||||||||||||
| 150 | message => << 'EOS', | ||||||||||||||
| 151 | USAGE: wrap_method(\@parameter_types, $return_type, $subroutine) | ||||||||||||||
| 152 | or wrap_method(params => \@params_types, returns => $return_types, code => $subroutine) | ||||||||||||||
| 153 | EOS | ||||||||||||||
| 154 | multi => [ | ||||||||||||||
| 155 | +{ positional => [ $ParamsTypes, $ReturnTypes, CodeRef ] }, | ||||||||||||||
| 156 | +{ | ||||||||||||||
| 157 | named_to_list => 1, | ||||||||||||||
| 158 | named => [ | ||||||||||||||
| 159 | params => $ParamsTypes, | ||||||||||||||
| 160 | isa => $ReturnTypes, | ||||||||||||||
| 161 | code => CodeRef, | ||||||||||||||
| 162 | ], | ||||||||||||||
| 163 | }, | ||||||||||||||
| 164 | ], | ||||||||||||||
| 165 | ); | ||||||||||||||
| 166 | 7 | my ($params_types, $return_types, $code) = $check->(@_); | |||||||||||||
| 167 | |||||||||||||||
| 168 | 7 | my $options = +{ | |||||||||||||
| 169 | skip_invocant => 1, | ||||||||||||||
| 170 | check => !_is_env_ndebug(), | ||||||||||||||
| 171 | }; | ||||||||||||||
| 172 | 7 | __PACKAGE__->new($params_types, $return_types, $code, $options); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | 2 | ▶ | sub install_sub { | ||||||||||||
|
Subroutine
install_sub: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 176 | 2 | state $check = signature( | |||||||||||||
| 177 | message => << 'EOS', | ||||||||||||||
| 178 | USAGE: install_sub($name, \@parameter_types, $return_type, $subroutine) | ||||||||||||||
| 179 | or install_sub(name => $name, params => \@params_types, returns => $return_types, code => $subroutine) | ||||||||||||||
| 180 | EOS | ||||||||||||||
| 181 | multi => [ | ||||||||||||||
| 182 | +{ positional => [ Str, $ParamsTypes, $ReturnTypes, CodeRef ] }, | ||||||||||||||
| 183 | +{ | ||||||||||||||
| 184 | named_to_list => 1, | ||||||||||||||
| 185 | named => [ | ||||||||||||||
| 186 | name => Str, | ||||||||||||||
| 187 | params => $ParamsTypes, | ||||||||||||||
| 188 | isa => $ReturnTypes, | ||||||||||||||
| 189 | code => CodeRef, | ||||||||||||||
| 190 | ], | ||||||||||||||
| 191 | }, | ||||||||||||||
| 192 | ], | ||||||||||||||
| 193 | ); | ||||||||||||||
| 194 | 2 | my ($name, $params_types, $return_types, $code) = $check->(@_); | |||||||||||||
| 195 | |||||||||||||||
| 196 | 2 | _install($name, wrap_sub($params_types, $return_types, $code), scalar caller); | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | |||||||||||||||
| 199 | 2 | ▶ | sub install_method { | ||||||||||||
|
Subroutine
install_method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 200 | 2 | state $check = signature( | |||||||||||||
| 201 | message => << 'EOS', | ||||||||||||||
| 202 | USAGE: install_method($name, \@parameter_types, $return_type, $subroutine) | ||||||||||||||
| 203 | or install_method(name => $name, params => \@params_types, returns => $return_types, code => $subroutine) | ||||||||||||||
| 204 | EOS | ||||||||||||||
| 205 | multi => [ | ||||||||||||||
| 206 | +{ positional => [ Str, $ParamsTypes, $ReturnTypes, CodeRef ] }, | ||||||||||||||
| 207 | +{ | ||||||||||||||
| 208 | named => [ | ||||||||||||||
| 209 | name => Str, | ||||||||||||||
| 210 | params => $ParamsTypes, | ||||||||||||||
| 211 | isa => $ReturnTypes, | ||||||||||||||
| 212 | code => CodeRef, | ||||||||||||||
| 213 | ], | ||||||||||||||
| 214 | named_to_list => 1, | ||||||||||||||
| 215 | }, | ||||||||||||||
| 216 | ], | ||||||||||||||
| 217 | ); | ||||||||||||||
| 218 | 2 | my ($name, $params_types, $return_types, $code) = $check->(@_); | |||||||||||||
| 219 | |||||||||||||||
| 220 | 2 | _install($name, wrap_method($params_types, $return_types, $code), scalar caller); | |||||||||||||
| 221 | } | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 4 | ▶ | sub _install { | ||||||||||||
|
Subroutine
_install: called CC=1 SCAR=0.0
| |||||||||||||||
| 224 | 4 | my ($name, $code, $pkg) = @_; | |||||||||||||
| 225 | 4 | my $fullname = "${pkg}::${name}"; | |||||||||||||
| 226 | { | ||||||||||||||
| 227 | 6 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 228 | 4 | *{$fullname} = $code; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | 4 | set_subname($fullname, $code); | |||||||||||||
| 231 | } | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 6 | 1; | |||||||||||||
| 234 | |||||||||||||||
| 235 | __END__ | ||||||||||||||