File Coverage

blib/lib/perl5i/2/Signatures.pm
Criterion Covered Total %
statement 58 58 100.0
branch 13 14 92.8
condition 6 10 60.0
subroutine 11 11 100.0
pod 0 3 0.0
total 88 96 91.6


line stmt bran cond sub pod time code
1             package perl5i::2::Signatures;
2              
3 101     101   33728 use perl5i::2::Signature;
  101         218  
  101         2747  
4              
5             # Can't load full autoboxing or signatures would not be available to the
6             # autoboxed definitions
7 101     101   34650 use perl5i::2::CODE;
  101         240  
  101         2833  
8              
9 101     101   543 use base q/Devel::Declare::MethodInstaller::Simple/;
  101         133  
  101         54225  
10 101     101   1920420 use Sub::Name;
  101         216  
  101         32619  
11              
12             sub import {
13 312     312   19367 my $class = shift;
14              
15 312         834 my %opts = @_;
16 312   33     2272 $opts{into} ||= caller;
17 312   50     1473 $opts{invocant} ||= '$self';
18              
19 312         1112 my %def_opts = %opts;
20 312         844 delete $def_opts{invocant};
21              
22             # Define "method"
23 312         2213 $class->install_methodhandler(
24             name => 'method',
25             %opts
26             );
27              
28             # Define "func"
29 312         60401 $class->install_methodhandler(
30             name => 'func',
31             %def_opts
32             );
33             }
34              
35             sub parse_proto {
36 1961     1961 0 762491 my $self = shift;
37 1961         2681 my ($proto) = @_;
38 1961   100     4567 $proto ||= '';
39              
40             # Save it for attaching to the code ref later
41 1961         3270 $self->{perl5i}{signature} = $proto;
42              
43 1961         4217 $proto =~ s/[\r\n]//g;
44 1961         2373 my $invocant = $self->{invocant};
45              
46 1961         1754 my $inject = '';
47 1961 100       3290 if( $invocant ) {
48 1537 100       3406 $invocant = $1 if $proto =~ s{^(\$\w+):\s*}{};
49 1537         3132 $inject .= "my ${invocant} = shift;";
50             }
51 1961 100 66     9422 $inject .= "my ($proto) = \@_;" if defined $proto and length $proto;
52              
53 1961         5158 return $inject;
54             }
55              
56              
57             sub code_for {
58 1961     1961 0 65351 my ($self, $name) = @_;
59              
60 1961         2977 my $signature = $self->{perl5i}{signature};
61 1961 100       3324 my $is_method = $self->{invocant} ? 1 : 0;
62              
63 1961 100       2741 if (defined $name) {
64 1543         3402 my $pkg = $self->get_curstash_name;
65 1543 50       11689 $name = join( '::', $pkg, $name )
66             unless( $name =~ /::/ );
67             return sub (&) {
68 1543     1543   5327 my $code = shift;
69             # So caller() gets the subroutine name
70 101     101   555 no strict 'refs';
  101         148  
  101         21880  
71 1543         6770 *{$name} = subname $name => $code;
  1543         7428  
72              
73 1543         3240 $self->set_signature(
74             code => $code,
75             signature => $signature,
76             is_method => $is_method,
77             );
78              
79 1543         2183 return;
80 1543         11211 };
81             } else {
82             return sub (&) {
83 422     422   15784 my $code = shift;
84              
85 422         859 $self->set_signature(
86             code => $code,
87             signature => $signature,
88             is_method => $is_method,
89             );
90 422         752 return $code;
91 418         2583 };
92             }
93             }
94              
95              
96             sub set_signature {
97 1965     1965 0 1796 my $self = shift;
98 1965         4187 my %args = @_;
99              
100 1965         3951 my $sig = perl5i::2::CODE::signature($args{code});
101 1965 100       3375 return $sig if $sig;
102              
103 1964         5024 $sig = perl5i::2::Signature->new(
104             signature => $args{signature},
105             is_method => $args{is_method},
106             );
107              
108 1964         4066 perl5i::2::CODE::__set_signature($args{code}, $sig);
109              
110 1964         2587 return $sig;
111             }
112              
113             1;