blib/lib/perl5i/2/Signatures.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 109 CC 8 SCAR 23.5
CC
8
Cov
0%
parse_proto
20.0
code_for
20.0
set_signature
6.0
CRAP 10.4
1 package perl5i::2::Signatures;
2 use strict;
3 use warnings;
4  
5 use perl5i::2::Signature;
6  
7 # Can't load full autoboxing or signatures would not be available to the
8 # autoboxed definitions
9 use perl5i::2::CODE;
10  
11 use base q/Devel::Declare::MethodInstaller::Simple/;
12 use Sub::Name;
13  
14 sub import {
15 my $class = shift;
16  
17 my %opts = @_;
18 $opts{into} ||= caller;
19 $opts{invocant} ||= '$self';
20  
21 my %def_opts = %opts;
22 delete $def_opts{invocant};
23  
24 # Define "method"
25 $class->install_methodhandler(
26 name => 'method',
27 %opts
28 );
29  
30 # Define "func"
31 $class->install_methodhandler(
32 name => 'func',
33 %def_opts
34 );
35 }
36  
37 sub parse_proto {
38 my $self = shift;
39 my ($proto) = @_;
40 $proto ||= '';
41  
42 # Save it for attaching to the code ref later
43 $self->{perl5i}{signature} = $proto;
44  
45 $proto =~ s/[\r\n]//g;
46 my $invocant = $self->{invocant};
47  
48 my $inject = '';
49 if( $invocant ) {
50 $invocant = $1 if $proto =~ s{^(\$\w+):\s*}{};
51 $inject .= "my ${invocant} = shift;";
52 }
53 $inject .= "my ($proto) = \@_;" if defined $proto and length $proto;
54  
55 return $inject;
56 }
57  
58  
59 sub code_for {
60 my ($self, $name) = @_;
61  
62 my $signature = $self->{perl5i}{signature};
63 my $is_method = $self->{invocant} ? 1 : 0;
64  
65 if (defined $name) {
66 my $pkg = $self->get_curstash_name;
67 $name = join( '::', $pkg, $name )
68 unless( $name =~ /::/ );
69 return sub (&) {
70 my $code = shift;
71 # So caller() gets the subroutine name
72 no strict 'refs';
73 *{$name} = subname $name => $code;
74  
75 $self->set_signature(
76 code => $code,
77 signature => $signature,
78 is_method => $is_method,
79 );
80  
81 return;
82 };
83 } else {
84 return sub (&) {
85 my $code = shift;
86  
87 $self->set_signature(
88 code => $code,
89 signature => $signature,
90 is_method => $is_method,
91 );
92 return $code;
93 };
94 }
95 }
96  
97  
98 sub set_signature {
99 my $self = shift;
100 my %args = @_;
101  
102 my $sig = perl5i::2::CODE::signature($args{code});
103 return $sig if $sig;
104  
105 $sig = perl5i::2::Signature->new(
106 signature => $args{signature},
107 is_method => $args{is_method},
108 );
109  
110 perl5i::2::CODE::__set_signature($args{code}, $sig);
111  
112 return $sig;
113 }
114  
115 1;