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 103     103   91083 use perl5i::2::Signature;
  103         325  
  103         3709  
4              
5             # Can't load full autoboxing or signatures would not be available to the
6             # autoboxed definitions
7 103     103   86931 use perl5i::2::CODE;
  103         337  
  103         3824  
8              
9 103     103   758 use base q/Devel::Declare::MethodInstaller::Simple/;
  103         263  
  103         146936  
10 103     103   3917642 use Sub::Name;
  103         312  
  103         62932  
11              
12             sub import {
13 318     318   908 my $class = shift;
14              
15 318         4108 my %opts = @_;
16 318   33     3290 $opts{into} ||= caller;
17 318   50     2466 $opts{invocant} ||= '$self';
18              
19 318         1667 my %def_opts = %opts;
20 318         975 delete $def_opts{invocant};
21              
22             # Define "method"
23 318         2799 $class->install_methodhandler(
24             name => 'method',
25             %opts
26             );
27              
28             # Define "func"
29 318         93976 $class->install_methodhandler(
30             name => 'func',
31             %def_opts
32             );
33             }
34              
35             sub parse_proto {
36 1999     1999 0 1605926 my $self = shift;
37 1999         5140 my ($proto) = @_;
38 1999   100     6504 $proto ||= '';
39              
40             # Save it for attaching to the code ref later
41 1999         4919 $self->{perl5i}{signature} = $proto;
42              
43 1999         4380 $proto =~ s/[\r\n]//g;
44 1999         4143 my $invocant = $self->{invocant};
45              
46 1999         2557 my $inject = '';
47 1999 100       19222 if( $invocant ) {
48 1567 100       4483 $invocant = $1 if $proto =~ s{^(\$\w+):\s*}{};
49 1567         4128 $inject .= "my ${invocant} = shift;";
50             }
51 1999 100 66     12104 $inject .= "my ($proto) = \@_;" if defined $proto and length $proto;
52              
53 1999         7710 return $inject;
54             }
55              
56              
57             sub code_for {
58 1999     1999 0 100641 my ($self, $name) = @_;
59              
60 1999         6088 my $signature = $self->{perl5i}{signature};
61 1999 100       4609 my $is_method = $self->{invocant} ? 1 : 0;
62              
63 1999 100       3887 if (defined $name) {
64 1573         5364 my $pkg = $self->get_curstash_name;
65 1573 50       17336 $name = join( '::', $pkg, $name )
66             unless( $name =~ /::/ );
67             return sub (&) {
68 1573     1573   8261 my $code = shift;
69             # So caller() gets the subroutine name
70 103     103   960 no strict 'refs';
  103         268  
  103         35125  
71 1573         13784 *{$name} = subname $name => $code;
  1573         11755  
72              
73 1573         4896 $self->set_signature(
74             code => $code,
75             signature => $signature,
76             is_method => $is_method,
77             );
78              
79 1573         3305 return;
80 1573         12154 };
81             } else {
82             return sub (&) {
83 430     430   28659 my $code = shift;
84              
85 430         1197 $self->set_signature(
86             code => $code,
87             signature => $signature,
88             is_method => $is_method,
89             );
90 430         1258 return $code;
91 426         3347 };
92             }
93             }
94              
95              
96             sub set_signature {
97 2003     2003 0 2759 my $self = shift;
98 2003         8611 my %args = @_;
99              
100 2003         6293 my $sig = perl5i::2::CODE::signature($args{code});
101 2003 100       5061 return $sig if $sig;
102              
103 2002         7845 $sig = perl5i::2::Signature->new(
104             signature => $args{signature},
105             is_method => $args{is_method},
106             );
107              
108 2002         6078 perl5i::2::CODE::__set_signature($args{code}, $sig);
109              
110 2002         4518 return $sig;
111             }
112              
113             1;