File Coverage

blib/lib/Types/TypedCodeRef.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 1 0.0
total 32 34 94.1


line stmt bran cond sub pod time code
1             package Types::TypedCodeRef;
2 4     4   760488 use 5.010001;
  4         30  
3 4     4   19 use strict;
  4         8  
  4         86  
4 4     4   18 use warnings;
  4         7  
  4         105  
5 4     4   671 use utf8;
  4         18  
  4         18  
6              
7             our $VERSION = "0.06";
8              
9             use Type::Library (
10 4         41 -base,
11             -declare => qw( TypedCodeRef ),
12 4     4   1541 );
  4         87447  
13              
14 4     4   5514 use Types::TypedCodeRef::Factory;
  4         14  
  4         683  
15              
16             {
17             my $factory =
18             Types::TypedCodeRef::Factory->new(sub_meta_finders => [\&get_sub_meta_from_sub_wrap_in_type]);
19             __PACKAGE__->add_type($factory->create());
20             }
21              
22             sub get_sub_meta_from_sub_wrap_in_type {
23 16     16 0 22 my $typed_code_ref = shift;
24 16 100 66     93 if ( Scalar::Util::blessed($typed_code_ref) && $typed_code_ref->isa('Sub::WrapInType') ) {
25 11         35 return Sub::Meta->new(
26             args => $typed_code_ref->params,
27             returns => $typed_code_ref->returns,
28             );
29             }
30 5         12 return;
31             }
32              
33             __PACKAGE__->meta->make_immutable;
34              
35             1;
36              
37             __END__
38              
39             =encoding utf-8
40              
41             =head1 NAME
42              
43             Types::TypedCodeRef - Type constraint for any typed subroutine.
44              
45             =head1 SYNOPSIS
46              
47             use Test2::V0;
48             use Types::TypedCodeRef -types;
49             use Types::Standard -types;
50             use Sub::WrapInType qw( wrap_sub );
51            
52             my $type = TypedCodeRef[ [Int, Int] => Int ];
53             ok $type->check(wrap_sub [Int, Int] => Int, sub { $_[0] + $_[1] });
54             ok !$type->check(0);
55             ok !$type->check([]);
56             ok !$type->check(sub {});
57              
58             =head1 DESCRIPTION
59              
60             Types::TypedCodeRef is type constraint for any typed subroutine (example, generated by Sub::WrapInType).
61              
62             =head1 TYPES
63              
64             =head2 TypedCodeRef[`p, `r]
65              
66             Only accepts typed subroutines.
67             A typed subroutine is a code reference in which the type of the parameter and the type of the return value are defined.
68             Types::TypedCodeRef is designed to inspect typed subroutines generated by Sub::WrapInType, but it is extensible enough to inspect typed subroutines created in other ways as well.
69              
70             The first type parameter is the subroutine parameters type, and the second type parameter is the subroutine return values type.
71              
72             =head1 LICENSE
73              
74             Copyright (C) ybrliiu.
75              
76             This library is free software; you can redistribute it and/or modify
77             it under the same terms as Perl itself.
78              
79             =head1 AUTHOR
80              
81             ybrliiu E<lt>raian@reeshome.orgE<gt>
82              
83             =head1 SEE ALSO
84              
85             L<Sub::WrapInType>
86              
87             L<Type::Tiny>
88              
89             =cut
90