line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Method::Signatures; |
2
|
|
|
|
|
|
|
|
3
|
62
|
|
|
62
|
|
1159866
|
use strict; |
|
62
|
|
|
|
|
83
|
|
|
62
|
|
|
|
|
1426
|
|
4
|
62
|
|
|
62
|
|
196
|
use warnings; |
|
62
|
|
|
|
|
64
|
|
|
62
|
|
|
|
|
1205
|
|
5
|
|
|
|
|
|
|
|
6
|
62
|
|
|
62
|
|
27132
|
use Lexical::SealRequireHints; |
|
62
|
|
|
|
|
34100
|
|
|
62
|
|
|
|
|
296
|
|
7
|
62
|
|
|
62
|
|
2029
|
use base 'Devel::Declare::MethodInstaller::Simple'; |
|
62
|
|
|
|
|
86
|
|
|
62
|
|
|
|
|
30230
|
|
8
|
62
|
|
|
62
|
|
977123
|
use Method::Signatures::Utils; |
|
62
|
|
|
|
|
463
|
|
|
62
|
|
|
|
|
2927
|
|
9
|
62
|
|
|
62
|
|
21114
|
use Method::Signatures::Parameter; |
|
62
|
|
|
|
|
122
|
|
|
62
|
|
|
|
|
1757
|
|
10
|
62
|
|
|
62
|
|
21818
|
use Method::Signatures::Signature; |
|
62
|
|
|
|
|
124
|
|
|
62
|
|
|
|
|
134675
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '20160608.0051_002'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $DEBUG = $ENV{METHOD_SIGNATURES_DEBUG} || 0; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $INF = ( 0 + "inf" ) == 0 ? 9e9999 : "inf"; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# copied from Devel::Pragma |
19
|
|
|
|
|
|
|
sub my_hints() { |
20
|
324
|
|
|
324
|
0
|
574
|
$^H |= 0x20000; |
21
|
324
|
|
|
|
|
449
|
return \%^H; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Method::Signatures - method and function declarations with signatures and no source filter |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=for readme plugin version |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 SYNOPSIS |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package Foo; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use Method::Signatures; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
method new (%args) { |
38
|
|
|
|
|
|
|
return bless {%args}, $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=for readme stop |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
method get ($key) { |
44
|
|
|
|
|
|
|
return $self->{$key}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
method set ($key, $val) { |
48
|
|
|
|
|
|
|
return $self->{$key} = $val; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=for readme start |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Can also get type checking if you like: |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
method set (Str $key, Int $val) { |
56
|
|
|
|
|
|
|
return $self->{$key} = $val; # now you know $val is always an integer |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=for readme stop |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
func hello($greeting, $place) { |
62
|
|
|
|
|
|
|
print "$greeting, $place!\n"; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=for readme start |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 DESCRIPTION |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Provides two new keywords, C<func> and C<method>, so that you can write |
70
|
|
|
|
|
|
|
subroutines with signatures instead of having to spell out |
71
|
|
|
|
|
|
|
C<my $self = shift; my($thing) = @_> |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
C<func> is like C<sub> but takes a signature where the prototype would |
74
|
|
|
|
|
|
|
normally go. This takes the place of C<my($foo, $bar) = @_> and does |
75
|
|
|
|
|
|
|
a whole lot more. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
C<method> is like C<func> but specifically for making methods. It will |
78
|
|
|
|
|
|
|
automatically provide the invocant as C<$self> (L<by default|/invocant>). |
79
|
|
|
|
|
|
|
No more C<my $self = shift>. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=begin :readme |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 INSTALLATION |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
This module sources are hosted on github |
86
|
|
|
|
|
|
|
https://github.com/evalEmpire/method-signatures.git |
87
|
|
|
|
|
|
|
and uses C<Module::Build> to generate the distribution. It can be |
88
|
|
|
|
|
|
|
istalled: |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=over |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item directly |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
cpanm git://github.com/evalEmpire/method-signatures.git |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item from CPAN |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
cpan Method::Signatures |
99
|
|
|
|
|
|
|
cpanm Method::Signatures |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item maualy cloninig the repository: |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
git clone https://github.com/evalEmpire/method-signatures.git |
104
|
|
|
|
|
|
|
cd method-signatures |
105
|
|
|
|
|
|
|
perl Build.PL |
106
|
|
|
|
|
|
|
./Build install |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=back |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=for readme plugin requires |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=end :readme |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=for readme stop |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Also allows signatures, very similar to Perl 6 signatures. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Also does type checking, understanding all the types that Moose (or Mouse) |
119
|
|
|
|
|
|
|
would understand. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
And it does all this with B<no source filters>. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 Signature syntax |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
func echo($message) { |
127
|
|
|
|
|
|
|
print "$message\n"; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
is equivalent to: |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub echo { |
133
|
|
|
|
|
|
|
my($message) = @_; |
134
|
|
|
|
|
|
|
print "$message\n"; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
except the original line numbering is preserved and the arguments are |
138
|
|
|
|
|
|
|
checked to make sure they match the signature. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Similarly |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
method foo($bar, $baz) { |
143
|
|
|
|
|
|
|
$self->wibble($bar, $baz); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
is equivalent to: |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub foo { |
149
|
|
|
|
|
|
|
my $self = shift; |
150
|
|
|
|
|
|
|
my($bar, $baz) = @_; |
151
|
|
|
|
|
|
|
$self->wibble($bar, $baz); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
again with checks to make sure the arguments passed in match the |
155
|
|
|
|
|
|
|
signature. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
The full signature syntax for each parameter is: |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Int|Str \:$param! where $SM_EXPR is ro = $AS_EXPR when $SM_EXPR |
160
|
|
|
|
|
|
|
\_____/ ^^\____/^ \____________/ \___/ \________/ \___________/ |
161
|
|
|
|
|
|
|
| || | | | | | | |
162
|
|
|
|
|
|
|
Type_/ || | | | | | | |
163
|
|
|
|
|
|
|
Aliased?___/ | | | | | | | |
164
|
|
|
|
|
|
|
Named?______/ | | | | | | |
165
|
|
|
|
|
|
|
Parameter var___/ | | | | | |
166
|
|
|
|
|
|
|
Required?__________/ | | | | |
167
|
|
|
|
|
|
|
Parameter constraint(s)_____/ | | | |
168
|
|
|
|
|
|
|
Parameter trait(s)______________________/ | | |
169
|
|
|
|
|
|
|
Default value____________________________________/ | |
170
|
|
|
|
|
|
|
When default value should be applied_________________________/ |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Every component except the parameter name (with sigil) is optional. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
C<$SM_EXPR> is any expression that is valid as the RHS of a smartmatch, |
175
|
|
|
|
|
|
|
or else a raw block of code. See L<"Value constraints">. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
C<$AS_EXPR> is any expression that is valid as the RHS of an |
178
|
|
|
|
|
|
|
assignment operator. See L<"Defaults">. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head3 C<@_> |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Other than removing C<$self>, C<@_> is left intact. You are free to |
184
|
|
|
|
|
|
|
use C<@_> alongside the arguments provided by Method::Signatures. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head3 Named parameters |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Parameters can be passed in named, as a hash, using the C<:$arg> syntax. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
method foo(:$arg) { |
192
|
|
|
|
|
|
|
... |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
$object->foo( arg => 42 ); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Named parameters are optional by default. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Required positional parameters and named parameters can be mixed, but |
200
|
|
|
|
|
|
|
the named params must come last. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
method foo( $a, $b, :$c ) # legal |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Named parameters are passed in as a hash after all positional arguments. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
method display( $text, :$justify = 'left', :$enchef = 0 ) { |
207
|
|
|
|
|
|
|
... |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# $text = "Some stuff", $justify = "right", $enchef = 0 |
211
|
|
|
|
|
|
|
$obj->display( "Some stuff", justify => "right" ); |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
You cannot mix optional positional params with named params, as that |
214
|
|
|
|
|
|
|
leads to ambiguities. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
method foo( $a, $b?, :$c ) # illegal |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Is this $a = 'c', $b = 42 or $c = 42? |
219
|
|
|
|
|
|
|
$obj->foo( c => 42 ); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head3 Aliased references |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
A signature of C<\@arg> will take an array reference but allow it to |
225
|
|
|
|
|
|
|
be used as C<@arg> inside the method. C<@arg> is an alias to the |
226
|
|
|
|
|
|
|
original reference. Any changes to C<@arg> will affect the original |
227
|
|
|
|
|
|
|
reference. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
package Stuff; |
230
|
|
|
|
|
|
|
method add_one(\@foo) { |
231
|
|
|
|
|
|
|
$_++ for @foo; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
my @bar = (1,2,3); |
235
|
|
|
|
|
|
|
Stuff->add_one(\@bar); # @bar is now (2,3,4) |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
This feature requires L<Data::Alias> to be installed. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head3 Invocant parameter |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
The method invocant (i.e. C<$self>) can be changed as the first |
244
|
|
|
|
|
|
|
parameter on a per-method basis. Put a colon after it instead of a comma: |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
method foo($class:) { |
247
|
|
|
|
|
|
|
$class->bar; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
method stuff($class: $arg, $another) { |
251
|
|
|
|
|
|
|
$class->things($arg, $another); |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
C<method> has an implied default invocant of C<$self:>, though that is |
255
|
|
|
|
|
|
|
configurable by setting the L<invocant parameter|/invocant> on the |
256
|
|
|
|
|
|
|
C<use Method::Signatures> line. |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
C<func> has no invocant, as it is intended for creating subs that will not |
259
|
|
|
|
|
|
|
be invoked on an object. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head3 Defaults |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Each parameter can be given a default with the C<$arg = EXPR> syntax. |
265
|
|
|
|
|
|
|
For example, |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
method add($this = 23, $that = 42) { |
268
|
|
|
|
|
|
|
return $this + $that; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Almost any expression can be used as a default. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
method silly( |
274
|
|
|
|
|
|
|
$num = 42, |
275
|
|
|
|
|
|
|
$string = q[Hello, world!], |
276
|
|
|
|
|
|
|
$hash = { this => 42, that => 23 }, |
277
|
|
|
|
|
|
|
$code = sub { $num + 4 }, |
278
|
|
|
|
|
|
|
@nums = (1,2,3), |
279
|
|
|
|
|
|
|
) |
280
|
|
|
|
|
|
|
{ |
281
|
|
|
|
|
|
|
... |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Normally, defaults will only be used if the argument is not passed in at all. |
285
|
|
|
|
|
|
|
Passing in C<undef> will override the default. That means ... |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Class->add(); # $this = 23, $that = 42 |
288
|
|
|
|
|
|
|
Class->add(99); # $this = 99, $that = 42 |
289
|
|
|
|
|
|
|
Class->add(99, undef); # $this = 99, $that = undef |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
However, you can specify additional conditions under which a default is |
292
|
|
|
|
|
|
|
also to be used, using a trailing C<when>. For example: |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# Use default if no argument passed |
295
|
|
|
|
|
|
|
method get_results($how_many = 1) {...} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is undef |
298
|
|
|
|
|
|
|
method get_results($how_many = 1 when undef) {...} |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is empty string |
301
|
|
|
|
|
|
|
method get_results($how_many = 1 when "") {...} |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is zero |
304
|
|
|
|
|
|
|
method get_results($how_many = 1 when 0) {...} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is zero or less |
307
|
|
|
|
|
|
|
method get_results($how_many = 1 when sub{ $_[0] <= 0 }) {...} |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is invalid |
310
|
|
|
|
|
|
|
method get_results($how_many = 1 when sub{ !valid($_[0]) }) {...} |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
In other words, if you include a C<when I<value>> after the default, |
313
|
|
|
|
|
|
|
the default is still used if the argument is missing, but is also |
314
|
|
|
|
|
|
|
used if the argument is provided but smart-matches the specified I<value>. |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
Note that the final two examples above use anonymous subroutines to |
317
|
|
|
|
|
|
|
conform their complex tests to the requirements of the smartmatch |
318
|
|
|
|
|
|
|
operator. Because this is useful, but syntactically clumsy, there is |
319
|
|
|
|
|
|
|
also a short-cut for this behaviour. If the test after C<when> consists |
320
|
|
|
|
|
|
|
of a block, the block is executed as the defaulting test, with the |
321
|
|
|
|
|
|
|
actual argument value aliased to C<$_> (just like in a C<grep> block). |
322
|
|
|
|
|
|
|
So the final two examples above could also be written: |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is zero or less |
325
|
|
|
|
|
|
|
method get_results($how_many = 1 when {$_ <= 0}) {...} |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is invalid |
328
|
|
|
|
|
|
|
method get_results($how_many = 1 when {!valid($_)}) } {...} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
The most commonly used form of C<when> modifier is almost |
331
|
|
|
|
|
|
|
certainly C<when undef>: |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is undef |
334
|
|
|
|
|
|
|
method get_results($how_many = 1 when undef) {...} |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
which covers the common case where an uninitialized variable is passed |
337
|
|
|
|
|
|
|
as an argument, or where supplying an explicit undefined value is |
338
|
|
|
|
|
|
|
intended to indicate: "use the default instead." |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
This usage is sufficiently common that a short-cut is provided: |
341
|
|
|
|
|
|
|
using the C<//=> operator (instead of the regular assignment operator) |
342
|
|
|
|
|
|
|
to specify the default. Like so: |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
# Use default if no argument passed OR argument is undef |
345
|
|
|
|
|
|
|
method get_results($how_many //= 1) {...} |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
Earlier parameters may be used in later defaults. |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
method copy_cat($this, $that = $this) { |
351
|
|
|
|
|
|
|
return $that; |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
Any variable that has a default is considered optional. |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
=head3 Type Constraints |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
Parameters can also be given type constraints. If they are, the value |
360
|
|
|
|
|
|
|
passed in will be validated against the type constraint provided. |
361
|
|
|
|
|
|
|
Types are provided by L<Any::Moose> which will load L<Mouse> if |
362
|
|
|
|
|
|
|
L<Moose> is not already loaded. |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
Type constraints can be a type, a role or a class. Each will be |
365
|
|
|
|
|
|
|
checked in turn until one of them passes. |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
* First, is the $value of that type declared in Moose (or Mouse)? |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
* Then, does the $value have that role? |
370
|
|
|
|
|
|
|
$value->DOES($type); |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
* Finally, is the $value an object of that class? |
373
|
|
|
|
|
|
|
$value->isa($type); |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
The set of default types that are understood can be found in |
376
|
|
|
|
|
|
|
L<Mouse::Util::TypeConstraints> (or L<Moose::Util::TypeConstraints>; |
377
|
|
|
|
|
|
|
they are generally the same, but there may be small differences). |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
# avoid "argument isn't numeric" warnings |
380
|
|
|
|
|
|
|
method add(Int $this = 23, Int $that = 42) { |
381
|
|
|
|
|
|
|
return $this + $that; |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
L<Mouse> and L<Moose> also understand some parameterized types; see |
385
|
|
|
|
|
|
|
their documentation for more details. |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
method add(Int $this = 23, Maybe[Int] $that) { |
388
|
|
|
|
|
|
|
# $this will definitely be defined |
389
|
|
|
|
|
|
|
# but $that might be undef |
390
|
|
|
|
|
|
|
return defined $that ? $this + $that : $this; |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
You may also use disjunctions, which means that you are willing to |
394
|
|
|
|
|
|
|
accept a value of either type. |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
method add(Int $this = 23, Int|ArrayRef[Int] $that) { |
397
|
|
|
|
|
|
|
# $that could be a single number, |
398
|
|
|
|
|
|
|
# or a reference to an array of numbers |
399
|
|
|
|
|
|
|
use List::Util qw<sum>; |
400
|
|
|
|
|
|
|
my @ints = ($this); |
401
|
|
|
|
|
|
|
push @ints, ref $that ? @$that : $that; |
402
|
|
|
|
|
|
|
return sum(@ints); |
403
|
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
If the value does not validate against the type, a run-time exception |
406
|
|
|
|
|
|
|
is thrown. |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
# Error will be: |
409
|
|
|
|
|
|
|
# In call to Class::add : the 'this' parameter ("cow") is not of type Int |
410
|
|
|
|
|
|
|
Class->add('cow', 'boy'); # make a cowboy! |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
You cannot declare the type of the invocant. |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
# this generates a compile-time error |
415
|
|
|
|
|
|
|
method new(ClassName $class:) { |
416
|
|
|
|
|
|
|
... |
417
|
|
|
|
|
|
|
} |
418
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
=head3 Value Constraints |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
In addition to a type, each parameter can also be specified with one or |
423
|
|
|
|
|
|
|
more additional constraints, using the C<$arg where CONSTRAINT> syntax. |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
method set_name($name where qr{\S+ \s+ \S+}x) { |
426
|
|
|
|
|
|
|
... |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
method set_rank($rank where \%STD_RANKS) { |
430
|
|
|
|
|
|
|
... |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
method set_age(Int $age where [17..75] ) { |
434
|
|
|
|
|
|
|
... |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
method set_rating($rating where { $_ >= 0 } where { $_ <= 100 } ) { |
438
|
|
|
|
|
|
|
... |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
method set_serial_num(Int $snum where {valid_checksum($snum)} ) { |
442
|
|
|
|
|
|
|
... |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
The C<where> keyword must appear immediately after the parameter name |
446
|
|
|
|
|
|
|
and before any L<trait|"Parameter traits"> or L<default|"Defaults">. |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
Each C<where> constraint is smartmatched against the value of the |
449
|
|
|
|
|
|
|
corresponding parameter, and an exception is thrown if the value does |
450
|
|
|
|
|
|
|
not satisfy the constraint. |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
Any of the normal smartmatch arguments (numbers, strings, regexes, |
453
|
|
|
|
|
|
|
undefs, hashrefs, arrayrefs, coderefs) can be used as a constraint. |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
In addition, the constraint can be specified as a raw block. This block |
456
|
|
|
|
|
|
|
can then refer to the parameter variable directly by name (as in the |
457
|
|
|
|
|
|
|
definition of C<set_serial_num()> above), or else as C<$_> (as in the |
458
|
|
|
|
|
|
|
definition of C<set_rating()>. |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
Unlike type constraints, value constraints are tested I<after> any |
461
|
|
|
|
|
|
|
default values have been resolved, and in the same order as they were |
462
|
|
|
|
|
|
|
specified within the signature. |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
=head3 Placeholder parameters |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
A positional argument can be ignored by using a bare C<$> sigil as its name. |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
method foo( $a, $, $c ) { |
470
|
|
|
|
|
|
|
... |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
The argument's value doesn't get stored in a variable, but the caller must |
474
|
|
|
|
|
|
|
still supply it. Value and type constraints can be applied to placeholders. |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
method bar( Int $ where { $_ < 10 } ) { |
477
|
|
|
|
|
|
|
... |
478
|
|
|
|
|
|
|
} |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
=head3 Parameter traits |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
Each parameter can be assigned a trait with the C<$arg is TRAIT> syntax. |
484
|
|
|
|
|
|
|
|
485
|
|
|
|
|
|
|
method stuff($this is ro) { |
486
|
|
|
|
|
|
|
... |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
Any unknown trait is ignored. |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
Most parameters have a default traits of C<is rw is copy>. |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
=over 4 |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
=item B<ro> |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
Read-only. Assigning or modifying the parameter is an error. This trait |
498
|
|
|
|
|
|
|
requires L<Const::Fast> to be installed. |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
=item B<rw> |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
Read-write. It's ok to read or write the parameter. |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
This is a default trait. |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=item B<copy> |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
The parameter will be a copy of the argument (just like C<< my $arg = shift >>). |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
This is a default trait except for the C<\@foo> parameter (see L<Aliased references>). |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
=item B<alias> |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
The parameter will be an alias of the argument. Any changes to the |
515
|
|
|
|
|
|
|
parameter will be reflected in the caller. This trait requires |
516
|
|
|
|
|
|
|
L<Data::Alias> to be installed. |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
This is a default trait for the C<\@foo> parameter (see L<Aliased references>). |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
=back |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=head3 Mixing value constraints, traits, and defaults |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
As explained in L<Signature syntax>, there is a defined order when including |
525
|
|
|
|
|
|
|
multiple trailing aspects of a parameter: |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
=over 4 |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
=item * Any value constraint must immediately follow the parameter name. |
530
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
=item * Any trait must follow that. |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
=item * Any default must come last. |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=back |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
For instance, to have a parameter which has all three aspects: |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
method echo($message where { length <= 80 } is ro = "what?") { |
540
|
|
|
|
|
|
|
return $message |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
Think of C<$message where { length <= 80 }> as being the left-hand side of the |
544
|
|
|
|
|
|
|
trait, and C<$message where { length <= 80 } is ro> as being the left-hand side |
545
|
|
|
|
|
|
|
of the default assignment. |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
=head3 Slurpy parameters |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
A "slurpy" parameter is a list or hash parameter that "slurps up" all |
551
|
|
|
|
|
|
|
remaining arguments. Since any following parameters can't receive values, |
552
|
|
|
|
|
|
|
there can be only one slurpy parameter. |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
Slurpy parameters must come at the end of the signature and they must |
555
|
|
|
|
|
|
|
be positional. |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
Slurpy parameters are optional by default. |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
=head3 The "yada yada" marker |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
The restriction that slurpy parameters must be positional, and must |
562
|
|
|
|
|
|
|
appear at the end of the signature, means that they cannot be used in |
563
|
|
|
|
|
|
|
conjunction with named parameters. |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
This is frustrating, because there are many situations (in particular: |
566
|
|
|
|
|
|
|
during object initialization, or when creating a callback) where it |
567
|
|
|
|
|
|
|
is extremely handy to be able to ignore extra named arguments that don't |
568
|
|
|
|
|
|
|
correspond to any named parameter. |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
While it would be theoretically possible to allow a slurpy parameter to |
571
|
|
|
|
|
|
|
come after named parameters, the current implementation does not support |
572
|
|
|
|
|
|
|
this (see L<"Slurpy parameter restrictions">). |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
Instead, there is a special syntax (colloquially known as the "yada yada") |
575
|
|
|
|
|
|
|
that tells a method or function to simply ignore any extra arguments |
576
|
|
|
|
|
|
|
that are passed to it: |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
# Expect name, age, gender, and simply ignore anything else |
579
|
|
|
|
|
|
|
method BUILD (:$name, :$age, :$gender, ...) { |
580
|
|
|
|
|
|
|
$self->{name} = uc $name; |
581
|
|
|
|
|
|
|
$self->{age} = min($age, 18); |
582
|
|
|
|
|
|
|
$self->{gender} = $gender // 'unspecified'; |
583
|
|
|
|
|
|
|
} |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
# Traverse tree with node-printing callback |
586
|
|
|
|
|
|
|
# (Callback only interested in nodes, ignores any other args passed to it) |
587
|
|
|
|
|
|
|
$tree->traverse( func($node, ...) { $node->print } ); |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
The C<...> may appear as a separate "pseudo-parameter" anywhere in the |
590
|
|
|
|
|
|
|
signature, but is normally placed at the very end. It has no other |
591
|
|
|
|
|
|
|
effect except to disable the usual "die if extra arguments" test that |
592
|
|
|
|
|
|
|
the module sets up within each method or function. |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
This means that a "yada yada" can also be used to ignore positional |
595
|
|
|
|
|
|
|
arguments (as the second example above indicates). So, instead of: |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
method verify ($min, $max, @etc) { |
598
|
|
|
|
|
|
|
return $min <= $self->{val} && $self->{val} <= $max; |
599
|
|
|
|
|
|
|
} |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
you can just write: |
602
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
method verify ($min, $max, ...) { |
604
|
|
|
|
|
|
|
return $min <= $self->{val} && $self->{val} <= $max; |
605
|
|
|
|
|
|
|
} |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
This is also marginally more efficient, as it does not have to allocate, |
608
|
|
|
|
|
|
|
initialize, or deallocate the unused slurpy parameter C<@etc>. |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
The bare C<@> sigil is a synonym for C<...>. A bare C<%> sigil is also a |
611
|
|
|
|
|
|
|
synonym for C<...>, but requires that there must be an even number of extra |
612
|
|
|
|
|
|
|
arguments, such as would be assigned to a hash. |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=head3 Required and optional parameters |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
Parameters declared using C<$arg!> are explicitly I<required>. |
618
|
|
|
|
|
|
|
Parameters declared using C<$arg?> are explicitly I<optional>. These |
619
|
|
|
|
|
|
|
declarations override all other considerations. |
620
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
A parameter is implicitly I<optional> if it is a named parameter, has a |
622
|
|
|
|
|
|
|
default, or is slurpy. All other parameters are implicitly |
623
|
|
|
|
|
|
|
I<required>. |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
# $greeting is optional because it is named |
626
|
|
|
|
|
|
|
method hello(:$greeting) { ... } |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
# $greeting is required because it is positional |
629
|
|
|
|
|
|
|
method hello($greeting) { ... } |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
# $greeting is optional because it has a default |
632
|
|
|
|
|
|
|
method hello($greeting = "Gruezi") { ... } |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
# $greeting is required because it is explicitly declared using ! |
635
|
|
|
|
|
|
|
method hello(:$greeting!) { ... } |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
# $greeting is required, even with the default, because it is |
638
|
|
|
|
|
|
|
# explicitly declared using ! |
639
|
|
|
|
|
|
|
method hello(:$greeting! = "Gruezi") { ... } |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
=head3 The C<@_> signature |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
The @_ signature is a special case which only shifts C<$self>. It |
645
|
|
|
|
|
|
|
leaves the rest of C<@_> alone. This way you can get $self but do the |
646
|
|
|
|
|
|
|
rest of the argument handling manually. |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
Note that a signature of C<(@_)> is exactly equivalent to a signature |
649
|
|
|
|
|
|
|
of C<(...)>. See L<"The yada yada marker">. |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
=head3 The empty signature |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
If a method is given the signature of C<< () >> or no signature at |
655
|
|
|
|
|
|
|
all, it takes no arguments. |
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
=head2 Anonymous Methods |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
An anonymous method can be declared just like an anonymous sub. |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
my $method = method ($arg) { |
663
|
|
|
|
|
|
|
return $self->foo($arg); |
664
|
|
|
|
|
|
|
}; |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
$obj->$method(42); |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
=head2 Options |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
Method::Signatures takes some options at `use` time of the form |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
use Method::Signatures { option => "value", ... }; |
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
=head3 invocant |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
In some cases it is desirable for the invocant to be named something other |
678
|
|
|
|
|
|
|
than C<$self>, and specifying it in the signature of every method is tedious |
679
|
|
|
|
|
|
|
and prone to human-error. When this option is set, methods that do not specify |
680
|
|
|
|
|
|
|
the invocant variable in their signatures will use the given variable name. |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
use Method::Signatures { invocant => '$app' }; |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
method main { $app->config; $app->run; $app->cleanup; } |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
Note that the leading sigil I<must> be provided, and the value must be a single |
687
|
|
|
|
|
|
|
token that would be valid as a perl variable. Currently only scalar invocant |
688
|
|
|
|
|
|
|
variables are supported (eg, the sigil must be a C<$>). |
689
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
This option only affects the packages in which it is used. All others will |
691
|
|
|
|
|
|
|
continue to use C<$self> as the default invocant variable. |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
=head3 compile_at_BEGIN |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
By default, named methods and funcs are evaluated at compile time, as |
696
|
|
|
|
|
|
|
if they were in a BEGIN block, just like normal Perl named subs. That |
697
|
|
|
|
|
|
|
means this will work: |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
echo("something"); |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
# This function is compiled first |
702
|
|
|
|
|
|
|
func echo($msg) { print $msg } |
703
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
You can turn this off lexically by setting compile_at_BEGIN to a false value. |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
use Method::Signatures { compile_at_BEGIN => 0 }; |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
compile_at_BEGIN currently causes some issues when used with Perl 5.8. |
709
|
|
|
|
|
|
|
See L<Earlier Perl versions>. |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=head3 debug |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
When true, turns on debugging messages about compiling methods and |
714
|
|
|
|
|
|
|
funcs. See L<DEBUGGING>. The flag is currently global, but this may |
715
|
|
|
|
|
|
|
change. |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
=head2 Differences from Perl 6 |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
Method::Signatures is mostly a straight subset of Perl 6 signatures. |
720
|
|
|
|
|
|
|
The important differences... |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=head3 Restrictions on named parameters |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
As noted above, there are more restrictions on named parameters than |
725
|
|
|
|
|
|
|
in Perl 6. |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
=head3 Named parameters are just hashes |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
Perl 5 lacks all the fancy named parameter syntax for the caller. |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=head3 Parameters are copies. |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
In Perl 6, parameters are aliases. This makes sense in Perl 6 because |
734
|
|
|
|
|
|
|
Perl 6 is an "everything is an object" language. Perl 5 is not, so |
735
|
|
|
|
|
|
|
parameters are much more naturally passed as copies. |
736
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
You can alias using the "alias" trait. |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
=head3 Can't use positional params as named params |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
Perl 6 allows you to use any parameter as a named parameter. Perl 5 |
742
|
|
|
|
|
|
|
lacks the named parameter disambiguating syntax so it is not allowed. |
743
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
=head3 Addition of the C<\@foo> reference alias prototype |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
In Perl 6, arrays and hashes don't get flattened, and their |
747
|
|
|
|
|
|
|
referencing syntax is much improved. Perl 5 has no such luxury, so |
748
|
|
|
|
|
|
|
Method::Signatures added a way to alias references to normal variables |
749
|
|
|
|
|
|
|
to make them easier to work with. |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
=head3 Addition of the C<@_> prototype |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
Method::Signatures lets you punt and use @_ like in regular Perl 5. |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
=cut |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
sub import { |
758
|
122
|
|
|
122
|
|
39655
|
my $class = shift; |
759
|
122
|
|
|
|
|
205
|
my $caller = caller; |
760
|
|
|
|
|
|
|
# default values |
761
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
# default invocant var - end-user can change with 'invocant' option. |
763
|
122
|
|
|
|
|
714
|
my $inv_var = '$self'; |
764
|
|
|
|
|
|
|
|
765
|
122
|
|
|
|
|
179
|
my $hints = my_hints; |
766
|
122
|
|
|
|
|
395
|
$hints->{METHOD_SIGNATURES_compile_at_BEGIN} = 1; # default to on |
767
|
|
|
|
|
|
|
|
768
|
122
|
|
|
|
|
128
|
my $arg = shift; |
769
|
122
|
100
|
|
|
|
282
|
if (defined $arg) { |
770
|
34
|
50
|
|
|
|
64
|
if (ref $arg) { |
|
|
0
|
|
|
|
|
|
771
|
34
|
50
|
|
|
|
112
|
$DEBUG = $arg->{debug} if exists $arg->{debug}; |
772
|
34
|
100
|
|
|
|
58
|
$caller = $arg->{into} if exists $arg->{into}; |
773
|
|
|
|
|
|
|
$hints->{METHOD_SIGNATURES_compile_at_BEGIN} = $arg->{compile_at_BEGIN} |
774
|
34
|
100
|
|
|
|
58
|
if exists $arg->{compile_at_BEGIN}; |
775
|
34
|
100
|
|
|
|
53
|
if (exists $arg->{invocant}) { |
776
|
27
|
|
|
|
|
25
|
$inv_var = $arg->{invocant}; |
777
|
|
|
|
|
|
|
# ensure (for now) the specified value is a valid variable |
778
|
|
|
|
|
|
|
# name (with '$' sigil) and nothing more. |
779
|
27
|
100
|
|
|
|
76
|
if ($inv_var !~ m{ \A \$ [^\W\d]\w* \z }x) { |
780
|
25
|
|
|
|
|
91
|
require Carp; |
781
|
25
|
|
|
|
|
2526
|
Carp::croak("Invalid invocant name: '$inv_var'"); |
782
|
|
|
|
|
|
|
} |
783
|
|
|
|
|
|
|
} |
784
|
|
|
|
|
|
|
} |
785
|
|
|
|
|
|
|
elsif ($arg eq ':DEBUG') { |
786
|
0
|
|
|
|
|
0
|
$DEBUG = 1; |
787
|
|
|
|
|
|
|
} |
788
|
|
|
|
|
|
|
else { |
789
|
0
|
|
|
|
|
0
|
require Carp; |
790
|
0
|
|
|
|
|
0
|
Carp::croak("Invalid Module::Signatures argument $arg"); |
791
|
|
|
|
|
|
|
} |
792
|
|
|
|
|
|
|
} |
793
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
$class->install_methodhandler( |
795
|
97
|
|
|
|
|
619
|
into => $caller, |
796
|
|
|
|
|
|
|
name => 'method', |
797
|
|
|
|
|
|
|
invocant => $inv_var, |
798
|
|
|
|
|
|
|
); |
799
|
|
|
|
|
|
|
|
800
|
97
|
|
|
|
|
17627
|
$class->install_methodhandler( |
801
|
|
|
|
|
|
|
into => $caller, |
802
|
|
|
|
|
|
|
name => 'func', |
803
|
|
|
|
|
|
|
); |
804
|
|
|
|
|
|
|
|
805
|
97
|
|
|
|
|
11502
|
DEBUG("import for $caller done\n"); |
806
|
97
|
|
|
|
|
248
|
DEBUG("method invocant is '$inv_var'\n"); |
807
|
|
|
|
|
|
|
} |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
# Inject special code to make named functions compile at BEGIN time. |
811
|
|
|
|
|
|
|
# Otherwise we leave injection to Devel::Declare. |
812
|
|
|
|
|
|
|
sub inject_if_block |
813
|
|
|
|
|
|
|
{ |
814
|
207
|
|
|
207
|
0
|
292
|
my ($self, $inject, $before) = @_; |
815
|
|
|
|
|
|
|
|
816
|
207
|
|
|
|
|
288
|
my $name = $self->{function_name}; |
817
|
207
|
|
100
|
|
|
666
|
my $attrs = $self->{attributes} || ''; |
818
|
|
|
|
|
|
|
|
819
|
207
|
|
|
|
|
560
|
DEBUG( "attributes: $attrs\n" ); |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
# Named function compiled at BEGIN time |
822
|
207
|
100
|
100
|
|
|
618
|
if( defined $name && $self->_do_compile_at_BEGIN ) { |
823
|
|
|
|
|
|
|
# Devel::Declare needs the code ref which has been generated. |
824
|
|
|
|
|
|
|
# Fortunately, "sub foo {...}" happens at compile time, so we |
825
|
|
|
|
|
|
|
# can use \&foo at runtime even if it comes before the sub |
826
|
|
|
|
|
|
|
# declaration in the code! |
827
|
189
|
|
|
|
|
388
|
$before = qq[\\&$name; sub $name $attrs ]; |
828
|
|
|
|
|
|
|
} |
829
|
|
|
|
|
|
|
|
830
|
207
|
|
|
|
|
551
|
DEBUG( "inject: $inject\n" ); |
831
|
207
|
|
|
|
|
482
|
DEBUG( "before: $before\n" ); |
832
|
207
|
50
|
|
|
|
346
|
DEBUG( "linestr before: ".$self->get_linestr."\n" ) if $DEBUG; |
833
|
207
|
|
|
|
|
584
|
my $ret = $self->SUPER::inject_if_block($inject, $before); |
834
|
207
|
50
|
|
|
|
5735
|
DEBUG( "linestr after: ". $self->get_linestr."\n" ) if $DEBUG; |
835
|
|
|
|
|
|
|
|
836
|
207
|
|
|
|
|
233
|
return $ret; |
837
|
|
|
|
|
|
|
} |
838
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
# Check if compile_at_BEGIN is set in this scope. |
841
|
|
|
|
|
|
|
sub _do_compile_at_BEGIN { |
842
|
202
|
|
|
202
|
|
329
|
my $hints = my_hints; |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
# Default to on. |
845
|
202
|
100
|
|
|
|
399
|
return 1 if !exists $hints->{METHOD_SIGNATURES_compile_at_BEGIN}; |
846
|
|
|
|
|
|
|
|
847
|
201
|
|
|
|
|
595
|
return $hints->{METHOD_SIGNATURES_compile_at_BEGIN}; |
848
|
|
|
|
|
|
|
} |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
# Sometimes a compilation error will happen but not throw an error causing the |
852
|
|
|
|
|
|
|
# code to continue compiling and producing an unrelated error down the road. |
853
|
|
|
|
|
|
|
# |
854
|
|
|
|
|
|
|
# A symptom of this is that eval STRING no longer works. So we detect if the |
855
|
|
|
|
|
|
|
# parser is a dead man walking. |
856
|
|
|
|
|
|
|
sub _parser_is_fucked { |
857
|
233
|
|
|
233
|
|
222
|
local $@; |
858
|
233
|
100
|
|
|
|
10751
|
return eval 42 ? 0 : 1; |
859
|
|
|
|
|
|
|
} |
860
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
# Largely copied from Devel::Declare::MethodInstaller::Simple::parser() |
863
|
|
|
|
|
|
|
# The original expects things in this order: |
864
|
|
|
|
|
|
|
# <keyword> name ($$@) :attr1 :attr2 { |
865
|
|
|
|
|
|
|
# * name |
866
|
|
|
|
|
|
|
# * prototype |
867
|
|
|
|
|
|
|
# * attributes |
868
|
|
|
|
|
|
|
# * an open brace |
869
|
|
|
|
|
|
|
# We want to support the prototype coming after the attributes as well as before, |
870
|
|
|
|
|
|
|
# but D::D::strip_attrs() looks for the open brace, and gets into an endless |
871
|
|
|
|
|
|
|
# loop if it doesn't find one. Meanwhile, D::D::strip_proto() doesn't find anything |
872
|
|
|
|
|
|
|
# if the attributes are before the prototype. |
873
|
|
|
|
|
|
|
sub parser { |
874
|
226
|
|
|
226
|
0
|
66316
|
my $self = shift; |
875
|
226
|
|
|
|
|
841
|
$self->init(@_); |
876
|
|
|
|
|
|
|
|
877
|
226
|
|
|
|
|
1881
|
$self->skip_declarator; |
878
|
226
|
|
|
|
|
4494
|
my $name = $self->strip_name; |
879
|
|
|
|
|
|
|
|
880
|
226
|
|
|
|
|
398
|
my $linestr = Devel::Declare::get_linestr; |
881
|
|
|
|
|
|
|
|
882
|
226
|
|
|
|
|
179
|
my($proto, $attrs); |
883
|
226
|
|
|
|
|
1114
|
my($char) = $linestr =~ m/(\(|:)/; |
884
|
226
|
100
|
100
|
|
|
1077
|
if (defined($char) and $char eq '(') { |
885
|
207
|
|
|
|
|
630
|
$proto = $self->strip_proto; |
886
|
207
|
|
|
|
|
5791
|
$attrs = $self->strip_attrs; |
887
|
|
|
|
|
|
|
} else { |
888
|
19
|
|
|
|
|
39
|
$attrs = $self->strip_attrs; |
889
|
19
|
|
|
|
|
53
|
$proto = $self->strip_proto; |
890
|
|
|
|
|
|
|
} |
891
|
|
|
|
|
|
|
|
892
|
226
|
|
|
|
|
604
|
my @decl = $self->parse_proto($proto); |
893
|
207
|
|
|
|
|
850
|
my $inject = $self->inject_parsed_proto(@decl); |
894
|
207
|
100
|
|
|
|
657
|
if (defined $name) { |
895
|
202
|
|
|
|
|
673
|
$inject = $self->scope_injector_call() . $inject; |
896
|
|
|
|
|
|
|
} |
897
|
207
|
100
|
|
|
|
1632
|
$self->inject_if_block($inject, $attrs ? "sub ${attrs} " : ''); |
898
|
|
|
|
|
|
|
|
899
|
207
|
|
|
|
|
575
|
$self->install( $name ); |
900
|
|
|
|
|
|
|
|
901
|
207
|
|
|
|
|
9430
|
return; |
902
|
|
|
|
|
|
|
} |
903
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
# Capture the function name |
906
|
|
|
|
|
|
|
sub strip_name { |
907
|
226
|
|
|
226
|
0
|
212
|
my $self = shift; |
908
|
|
|
|
|
|
|
|
909
|
226
|
|
|
|
|
627
|
my $name = $self->SUPER::strip_name(@_); |
910
|
226
|
|
|
|
|
4558
|
$self->{function_name} = $name; |
911
|
|
|
|
|
|
|
|
912
|
226
|
|
|
|
|
282
|
return $name; |
913
|
|
|
|
|
|
|
} |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
# Capture the attributes |
917
|
|
|
|
|
|
|
# A copy of the method of the same name from Devel::Declare::Context::Simple::strip_attrs() |
918
|
|
|
|
|
|
|
# The only change is that the while() loop now terminates if it finds an open brace _or_ |
919
|
|
|
|
|
|
|
# open paren. This is necessary to allow the function signature to come after the attributes. |
920
|
|
|
|
|
|
|
sub strip_attrs { |
921
|
226
|
|
|
226
|
0
|
210
|
my $self = shift; |
922
|
|
|
|
|
|
|
|
923
|
226
|
|
|
|
|
390
|
$self->skipspace; |
924
|
|
|
|
|
|
|
|
925
|
226
|
|
|
|
|
1125
|
my $linestr = Devel::Declare::get_linestr; |
926
|
226
|
|
|
|
|
215
|
my $attrs = ''; |
927
|
|
|
|
|
|
|
|
928
|
226
|
100
|
|
|
|
373
|
if (substr($linestr, $self->offset, 1) eq ':') { |
929
|
8
|
|
100
|
|
|
33
|
while (substr($linestr, $self->offset, 1) ne '{' |
930
|
|
|
|
|
|
|
and substr($linestr, $self->offset, 1) ne '(' |
931
|
|
|
|
|
|
|
) { |
932
|
26
|
100
|
|
|
|
212
|
if (substr($linestr, $self->offset, 1) eq ':') { |
933
|
14
|
|
|
|
|
48
|
substr($linestr, $self->offset, 1) = ''; |
934
|
14
|
|
|
|
|
42
|
Devel::Declare::set_linestr($linestr); |
935
|
|
|
|
|
|
|
|
936
|
14
|
|
|
|
|
11
|
$attrs .= ':'; |
937
|
|
|
|
|
|
|
} |
938
|
|
|
|
|
|
|
|
939
|
26
|
|
|
|
|
73
|
$self->skipspace; |
940
|
26
|
|
|
|
|
122
|
$linestr = Devel::Declare::get_linestr(); |
941
|
|
|
|
|
|
|
|
942
|
26
|
100
|
|
|
|
33
|
if (my $len = Devel::Declare::toke_scan_word($self->offset, 0)) { |
943
|
14
|
|
|
|
|
54
|
my $name = substr($linestr, $self->offset, $len); |
944
|
14
|
|
|
|
|
41
|
substr($linestr, $self->offset, $len) = ''; |
945
|
14
|
|
|
|
|
35
|
Devel::Declare::set_linestr($linestr); |
946
|
|
|
|
|
|
|
|
947
|
14
|
|
|
|
|
12
|
$attrs .= " ${name}"; |
948
|
|
|
|
|
|
|
|
949
|
14
|
100
|
|
|
|
18
|
if (substr($linestr, $self->offset, 1) eq '(') { |
950
|
6
|
|
|
|
|
22
|
my $length = Devel::Declare::toke_scan_str($self->offset); |
951
|
6
|
|
|
|
|
25
|
my $arg = Devel::Declare::get_lex_stuff(); |
952
|
6
|
|
|
|
|
7
|
Devel::Declare::clear_lex_stuff(); |
953
|
6
|
|
|
|
|
8
|
$linestr = Devel::Declare::get_linestr(); |
954
|
6
|
|
|
|
|
8
|
substr($linestr, $self->offset, $length) = ''; |
955
|
6
|
|
|
|
|
19
|
Devel::Declare::set_linestr($linestr); |
956
|
|
|
|
|
|
|
|
957
|
6
|
|
|
|
|
15
|
$attrs .= "(${arg})"; |
958
|
|
|
|
|
|
|
} |
959
|
|
|
|
|
|
|
} |
960
|
|
|
|
|
|
|
} |
961
|
|
|
|
|
|
|
|
962
|
8
|
|
|
|
|
66
|
$linestr = Devel::Declare::get_linestr(); |
963
|
|
|
|
|
|
|
} |
964
|
|
|
|
|
|
|
|
965
|
226
|
|
|
|
|
923
|
$self->{attributes} = $attrs; |
966
|
|
|
|
|
|
|
|
967
|
226
|
|
|
|
|
349
|
return $attrs; |
968
|
|
|
|
|
|
|
} |
969
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
|
971
|
|
|
|
|
|
|
# Overriden method from D::D::MS |
972
|
|
|
|
|
|
|
sub parse_proto { |
973
|
233
|
|
|
233
|
0
|
2692
|
my $self = shift; |
974
|
233
|
|
|
|
|
200
|
my $proto = shift; |
975
|
|
|
|
|
|
|
|
976
|
|
|
|
|
|
|
# Before we try to compile signatures, make sure there isn't a hidden compilation error. |
977
|
233
|
100
|
|
|
|
318
|
die $@ if _parser_is_fucked; |
978
|
|
|
|
|
|
|
|
979
|
|
|
|
|
|
|
$self->{signature} = Method::Signatures::Signature->new( |
980
|
|
|
|
|
|
|
signature_string => defined $proto ? $proto : "", |
981
|
|
|
|
|
|
|
invocant => $self->{invocant}, |
982
|
|
|
|
|
|
|
pre_invocant => $self->{pre_invocant} |
983
|
231
|
100
|
|
|
|
3735
|
); |
984
|
|
|
|
|
|
|
|
985
|
|
|
|
|
|
|
# Then turn it into Perl code |
986
|
214
|
|
|
|
|
1417
|
my $inject = $self->inject_from_signature(); |
987
|
|
|
|
|
|
|
|
988
|
214
|
|
|
|
|
406
|
return $inject; |
989
|
|
|
|
|
|
|
} |
990
|
|
|
|
|
|
|
|
991
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
# Turn the parsed signature into Perl code |
993
|
|
|
|
|
|
|
sub inject_from_signature { |
994
|
214
|
|
|
214
|
0
|
10461
|
my $self = shift; |
995
|
214
|
|
33
|
|
|
524
|
my $class = ref $self || $self; |
996
|
214
|
|
|
|
|
226
|
my $signature = $self->{signature}; |
997
|
|
|
|
|
|
|
|
998
|
214
|
|
|
|
|
322
|
$self->{line_number} = 1; |
999
|
|
|
|
|
|
|
|
1000
|
214
|
|
|
|
|
199
|
my @code; |
1001
|
214
|
50
|
|
|
|
542
|
push @code, "my @{[$signature->pre_invocant]} = shift;" if $signature->pre_invocant; |
|
0
|
|
|
|
|
0
|
|
1002
|
214
|
100
|
|
|
|
525
|
push @code, "my @{[$signature->invocant]} = shift;" if $signature->invocant; |
|
161
|
|
|
|
|
539
|
|
1003
|
|
|
|
|
|
|
|
1004
|
214
|
|
|
|
|
231
|
for my $sig (@{$signature->positional_parameters}) { |
|
214
|
|
|
|
|
444
|
|
1005
|
224
|
|
|
|
|
415
|
push @code, $self->inject_for_sig($sig); |
1006
|
|
|
|
|
|
|
} |
1007
|
|
|
|
|
|
|
|
1008
|
214
|
100
|
|
|
|
190
|
if( @{$signature->named_parameters} ) { |
|
214
|
|
|
|
|
529
|
|
1009
|
24
|
|
|
|
|
25
|
my $first_named_idx = @{$signature->positional_parameters}; |
|
24
|
|
|
|
|
45
|
|
1010
|
24
|
100
|
|
|
|
25
|
if (grep { $_->is_ref_alias or $_->traits->{alias} } @{$signature->named_parameters}) |
|
42
|
100
|
|
|
|
185
|
|
|
24
|
|
|
|
|
45
|
|
1011
|
|
|
|
|
|
|
{ |
1012
|
4
|
|
|
|
|
18
|
require Data::Alias; |
1013
|
4
|
|
|
|
|
8
|
push @code, "Data::Alias::alias( my (\%args) = \@_[$first_named_idx..\$#_] );"; |
1014
|
|
|
|
|
|
|
} |
1015
|
|
|
|
|
|
|
else |
1016
|
|
|
|
|
|
|
{ |
1017
|
20
|
|
|
|
|
51
|
push @code, "my (\%args) = \@_[$first_named_idx..\$#_];"; |
1018
|
|
|
|
|
|
|
} |
1019
|
|
|
|
|
|
|
|
1020
|
24
|
|
|
|
|
29
|
for my $sig (@{$signature->named_parameters}) { |
|
24
|
|
|
|
|
50
|
|
1021
|
42
|
|
|
|
|
85
|
push @code, $self->inject_for_sig($sig); |
1022
|
|
|
|
|
|
|
} |
1023
|
|
|
|
|
|
|
|
1024
|
24
|
100
|
66
|
|
|
93
|
push @code, $class . '->named_param_error(\%args) if keys %args;' |
1025
|
|
|
|
|
|
|
if $signature->num_named && !$signature->num_yadayada; |
1026
|
|
|
|
|
|
|
} |
1027
|
|
|
|
|
|
|
|
1028
|
214
|
|
|
|
|
409
|
my $max_argv = $signature->max_argv_size; |
1029
|
214
|
|
|
|
|
299
|
my $max_args = $signature->max_args; |
1030
|
214
|
100
|
|
|
|
825
|
push @code, qq[$class->too_many_args_error($max_args) if scalar(\@_) > $max_argv; ] |
1031
|
|
|
|
|
|
|
unless $max_argv == $INF; |
1032
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
# Add any additional trailing newlines so the body is on the right line. |
1034
|
214
|
|
|
|
|
857
|
push @code, $self->inject_newlines( $signature->num_lines - $self->{line_number} ); |
1035
|
|
|
|
|
|
|
|
1036
|
|
|
|
|
|
|
# All on one line. |
1037
|
214
|
|
|
|
|
681
|
return join ' ', @code; |
1038
|
|
|
|
|
|
|
} |
1039
|
|
|
|
|
|
|
|
1040
|
|
|
|
|
|
|
|
1041
|
|
|
|
|
|
|
sub too_many_args_error { |
1042
|
9
|
|
|
9
|
1
|
3451
|
my($class, $max_args) = @_; |
1043
|
|
|
|
|
|
|
|
1044
|
9
|
|
|
|
|
85
|
$class->signature_error("was given too many arguments; it expects $max_args"); |
1045
|
|
|
|
|
|
|
} |
1046
|
|
|
|
|
|
|
|
1047
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
sub odd_number_args_error { |
1049
|
1
|
|
|
1
|
0
|
1189
|
my($class) = @_; |
1050
|
|
|
|
|
|
|
|
1051
|
1
|
|
|
|
|
2
|
$class->signature_error('was given an odd number of arguments for a placeholder hash'); |
1052
|
|
|
|
|
|
|
} |
1053
|
|
|
|
|
|
|
|
1054
|
|
|
|
|
|
|
|
1055
|
|
|
|
|
|
|
sub named_param_error { |
1056
|
3
|
|
|
3
|
1
|
15918
|
my ($class, $args) = @_; |
1057
|
3
|
|
|
|
|
8
|
my @keys = keys %$args; |
1058
|
|
|
|
|
|
|
|
1059
|
3
|
|
|
|
|
19
|
$class->signature_error("does not take @keys as named argument(s)"); |
1060
|
|
|
|
|
|
|
} |
1061
|
|
|
|
|
|
|
|
1062
|
|
|
|
|
|
|
# Regex to determine if a where clause is a block. |
1063
|
|
|
|
|
|
|
my $when_block_re = qr{ |
1064
|
|
|
|
|
|
|
^ |
1065
|
|
|
|
|
|
|
\s* |
1066
|
|
|
|
|
|
|
\{ |
1067
|
|
|
|
|
|
|
(?: |
1068
|
|
|
|
|
|
|
.* ; .* | # statements separated by semicolons |
1069
|
|
|
|
|
|
|
(?:(?! => ). )+ # doesn't look like a hash with fat commas |
1070
|
|
|
|
|
|
|
) |
1071
|
|
|
|
|
|
|
\} |
1072
|
|
|
|
|
|
|
\s* |
1073
|
|
|
|
|
|
|
$ |
1074
|
|
|
|
|
|
|
}xs; |
1075
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
sub inject_for_sig { |
1077
|
266
|
|
|
266
|
0
|
226
|
my $self = shift; |
1078
|
266
|
|
33
|
|
|
482
|
my $class = ref $self || $self; |
1079
|
266
|
|
|
|
|
204
|
my $sig = shift; |
1080
|
|
|
|
|
|
|
|
1081
|
266
|
100
|
|
|
|
952
|
return if $sig->is_at_underscore; |
1082
|
|
|
|
|
|
|
|
1083
|
262
|
|
|
|
|
202
|
my @code; |
1084
|
|
|
|
|
|
|
|
1085
|
|
|
|
|
|
|
# Add any necessary leading newlines so line numbers are preserved. |
1086
|
262
|
|
|
|
|
715
|
push @code, $self->inject_newlines($sig->first_line_number - $self->{line_number}); |
1087
|
|
|
|
|
|
|
|
1088
|
262
|
100
|
|
|
|
782
|
if( $sig->is_hash_yadayada ) { |
1089
|
1
|
|
|
|
|
4
|
my $is_odd = $sig->position % 2; |
1090
|
1
|
|
|
|
|
3
|
push @code, qq[$class->odd_number_args_error() if scalar(\@_) % 2 != $is_odd;]; |
1091
|
1
|
|
|
|
|
2
|
return @code; |
1092
|
|
|
|
|
|
|
} |
1093
|
|
|
|
|
|
|
|
1094
|
261
|
|
|
|
|
408
|
my $sigil = $sig->sigil; |
1095
|
261
|
|
|
|
|
337
|
my $name = $sig->variable_name; |
1096
|
261
|
|
|
|
|
339
|
my $idx = $sig->position; |
1097
|
261
|
|
|
|
|
327
|
my $var = $sig->variable; |
1098
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
# These are the defaults. |
1100
|
261
|
|
|
|
|
369
|
my $lhs = "my $var"; |
1101
|
261
|
|
|
|
|
199
|
my ($rhs, $deletion_target); |
1102
|
|
|
|
|
|
|
|
1103
|
261
|
100
|
|
|
|
485
|
if( $sig->is_named ) { |
1104
|
42
|
|
|
|
|
113
|
$sig->passed_in("\$args{$name}"); |
1105
|
42
|
|
|
|
|
69
|
$rhs = $deletion_target = $sig->passed_in; |
1106
|
42
|
100
|
|
|
|
94
|
$rhs = "${sigil}{$rhs}" if $sig->is_ref_alias; |
1107
|
|
|
|
|
|
|
} |
1108
|
|
|
|
|
|
|
else { |
1109
|
219
|
100
|
|
|
|
964
|
$rhs = $sig->is_ref_alias ? "${sigil}{\$_[$idx]}" : |
|
|
100
|
|
|
|
|
|
1110
|
|
|
|
|
|
|
$sig->sigil =~ /^[@%]$/ ? "\@_[$idx..\$#_]" : |
1111
|
|
|
|
|
|
|
"\$_[$idx]" ; |
1112
|
219
|
|
|
|
|
492
|
$sig->passed_in($rhs); |
1113
|
|
|
|
|
|
|
} |
1114
|
|
|
|
|
|
|
|
1115
|
261
|
100
|
|
|
|
590
|
my $check_exists = $sig->is_named ? "exists \$args{$name}" : "( scalar(\@_) > $idx)"; |
1116
|
261
|
|
|
|
|
477
|
$sig->check_exists($check_exists); |
1117
|
|
|
|
|
|
|
|
1118
|
261
|
|
|
|
|
367
|
my $default = $sig->default; |
1119
|
261
|
|
|
|
|
348
|
my $when = $sig->default_when; |
1120
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
# Handle a default value |
1122
|
261
|
100
|
|
|
|
525
|
if( defined $when ) { |
|
|
100
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
# Handle default with 'when { block using $_ }' |
1124
|
54
|
100
|
|
|
|
216
|
if ($when =~ $when_block_re) { |
1125
|
6
|
|
|
|
|
13
|
$rhs = "!$check_exists ? ($default) : do{ no warnings; my \$arg = $rhs; (grep $when \$arg) ? ($default) : \$arg}"; |
1126
|
|
|
|
|
|
|
} |
1127
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
# Handle default with 'when anything_else' |
1129
|
|
|
|
|
|
|
else { |
1130
|
48
|
|
|
|
|
118
|
$rhs = "!$check_exists ? ($default) : do{ no warnings; my \$arg = $rhs; \$arg ~~ ($when) ? ($default) : \$arg }"; |
1131
|
|
|
|
|
|
|
} |
1132
|
|
|
|
|
|
|
} |
1133
|
|
|
|
|
|
|
# Handle simple defaults |
1134
|
|
|
|
|
|
|
elsif( defined $default ) { |
1135
|
34
|
|
|
|
|
59
|
$rhs = "$check_exists ? ($rhs) : ($default)"; |
1136
|
|
|
|
|
|
|
} |
1137
|
|
|
|
|
|
|
|
1138
|
261
|
100
|
|
|
|
535
|
if( $sig->is_required ) { |
1139
|
124
|
100
|
|
|
|
247
|
if( $sig->is_placeholder ) { |
1140
|
3
|
|
|
|
|
8
|
push @code, qq[${class}->required_placeholder_arg('$idx') unless $check_exists; ]; |
1141
|
|
|
|
|
|
|
} else { |
1142
|
121
|
|
|
|
|
310
|
push @code, qq[${class}->required_arg('$var') unless $check_exists; ]; |
1143
|
|
|
|
|
|
|
} |
1144
|
|
|
|
|
|
|
} |
1145
|
|
|
|
|
|
|
|
1146
|
|
|
|
|
|
|
# Handle \@foo |
1147
|
261
|
100
|
100
|
|
|
1345
|
if ( $sig->is_ref_alias or $sig->traits->{alias} ) { |
|
|
100
|
|
|
|
|
|
1148
|
13
|
|
|
|
|
42
|
require Data::Alias; |
1149
|
13
|
|
|
|
|
47
|
push @code, sprintf 'Data::Alias::alias(%s = %s);', $lhs, $rhs; |
1150
|
|
|
|
|
|
|
} |
1151
|
|
|
|
|
|
|
# Handle "is ro" |
1152
|
|
|
|
|
|
|
elsif ( $sig->traits->{ro} ) { |
1153
|
16
|
|
|
|
|
3703
|
require Const::Fast; |
1154
|
16
|
|
|
|
|
5876
|
push @code, "Const::Fast::const( $lhs => $rhs );"; |
1155
|
|
|
|
|
|
|
} else { |
1156
|
232
|
|
|
|
|
444
|
push @code, "$lhs = $rhs;"; |
1157
|
|
|
|
|
|
|
} |
1158
|
|
|
|
|
|
|
|
1159
|
261
|
100
|
|
|
|
534
|
if( $sig->type ) { |
1160
|
54
|
|
|
|
|
109
|
push @code, $self->inject_for_type_check($sig); |
1161
|
|
|
|
|
|
|
} |
1162
|
|
|
|
|
|
|
|
1163
|
|
|
|
|
|
|
# Named arg has been handled, so don't pass to error handler |
1164
|
261
|
100
|
|
|
|
472
|
push @code, "delete( $deletion_target );" if $deletion_target; |
1165
|
|
|
|
|
|
|
|
1166
|
|
|
|
|
|
|
# Handle 'where' constraints (after defaults are resolved) |
1167
|
261
|
|
|
|
|
191
|
for my $constraint ( @{$sig->where} ) { |
|
261
|
|
|
|
|
556
|
|
1168
|
|
|
|
|
|
|
# Handle 'where { block using $_ }' |
1169
|
8
|
100
|
|
|
|
36
|
my $constraint_impl = |
1170
|
|
|
|
|
|
|
$constraint =~ m{^ \s* \{ (?: .* ; .* | (?:(?! => ). )* ) \} \s* $}xs |
1171
|
|
|
|
|
|
|
? "sub $constraint" |
1172
|
|
|
|
|
|
|
: $constraint; |
1173
|
|
|
|
|
|
|
|
1174
|
8
|
100
|
|
|
|
21
|
my( $error_reporter, $var_name ) = |
1175
|
|
|
|
|
|
|
$sig->is_placeholder |
1176
|
|
|
|
|
|
|
? ( 'placeholder_where_error', $sig->position ) |
1177
|
|
|
|
|
|
|
: ( 'where_error', $var ); |
1178
|
8
|
|
|
|
|
22
|
my $error = sprintf q{ %s->%s(%s, '%s', '%s') }, $class, $error_reporter, $var, $var_name, $constraint; |
1179
|
8
|
|
|
|
|
24
|
push @code, "$error unless do { no if \$] >= 5.017011, warnings => 'experimental::smartmatch'; grep { \$_ ~~ $constraint_impl } $var }; "; |
1180
|
|
|
|
|
|
|
} |
1181
|
|
|
|
|
|
|
|
1182
|
261
|
100
|
|
|
|
515
|
if( $sig->is_placeholder ) { |
1183
|
4
|
|
|
|
|
5
|
unshift @code, 'do {'; |
1184
|
4
|
|
|
|
|
7
|
push @code, '};'; |
1185
|
|
|
|
|
|
|
} |
1186
|
|
|
|
|
|
|
|
1187
|
|
|
|
|
|
|
# Record the current line number for the next injection. |
1188
|
261
|
|
|
|
|
390
|
$self->{line_number} = $sig->first_line_number; |
1189
|
|
|
|
|
|
|
|
1190
|
261
|
|
|
|
|
657
|
return @code; |
1191
|
|
|
|
|
|
|
} |
1192
|
|
|
|
|
|
|
|
1193
|
0
|
|
|
0
|
|
0
|
sub __magic_newline() { die "newline() should never be called"; } |
1194
|
|
|
|
|
|
|
|
1195
|
|
|
|
|
|
|
# Devel::Declare cannot normally inject multiple lines. |
1196
|
|
|
|
|
|
|
# This is a way to trick it, the parser will continue through |
1197
|
|
|
|
|
|
|
# a function call with a newline in the argument list. |
1198
|
|
|
|
|
|
|
sub inject_newlines { |
1199
|
476
|
|
|
476
|
0
|
375
|
my $self = shift; |
1200
|
476
|
|
|
|
|
342
|
my $num_newlines = shift; |
1201
|
|
|
|
|
|
|
|
1202
|
476
|
100
|
|
|
|
872
|
return if $num_newlines == 0; |
1203
|
|
|
|
|
|
|
|
1204
|
33
|
|
|
|
|
124
|
return sprintf q[ Method::Signatures::__magic_newline(%s) if 0; ], |
1205
|
|
|
|
|
|
|
"\n" x $num_newlines; |
1206
|
|
|
|
|
|
|
} |
1207
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
|
1209
|
|
|
|
|
|
|
# A hook for extension authors |
1210
|
|
|
|
|
|
|
# (see also type_check below) |
1211
|
|
|
|
|
|
|
sub inject_for_type_check |
1212
|
|
|
|
|
|
|
{ |
1213
|
52
|
|
|
52
|
1
|
54
|
my $self = shift; |
1214
|
52
|
|
33
|
|
|
93
|
my $class = ref $self || $self; |
1215
|
52
|
|
|
|
|
59
|
my ($sig) = @_; |
1216
|
|
|
|
|
|
|
|
1217
|
52
|
100
|
100
|
|
|
115
|
my $check_exists = $sig->is_optional && !defined $sig->default |
1218
|
|
|
|
|
|
|
? $sig->check_exists : ''; |
1219
|
|
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
# This is an optimization to unroll typecheck which makes Mouse types about 40% faster. |
1221
|
|
|
|
|
|
|
# It only happens when type_check() has not been overridden. |
1222
|
52
|
100
|
|
|
|
492
|
if( $class->can("type_check") eq __PACKAGE__->can("type_check") ) { |
1223
|
51
|
|
|
|
|
301
|
my $check = sprintf q[($%s::mutc{cache}{'%s'} ||= %s->_make_constraint('%s'))->check(%s)], |
1224
|
|
|
|
|
|
|
__PACKAGE__, $sig->type, $class, $sig->type, $sig->variable; |
1225
|
|
|
|
|
|
|
|
1226
|
51
|
100
|
|
|
|
155
|
my( $error_reporter, $variable_name ) = |
1227
|
|
|
|
|
|
|
$sig->is_placeholder |
1228
|
|
|
|
|
|
|
? ( 'placeholder_type_error', $sig->position ) |
1229
|
|
|
|
|
|
|
: ( 'type_error', $sig->variable_name ); |
1230
|
51
|
|
|
|
|
156
|
my $error = sprintf q[%s->%s('%s', %s, '%s') ], |
1231
|
|
|
|
|
|
|
$class, $error_reporter, $sig->type, $sig->variable, $variable_name; |
1232
|
51
|
|
|
|
|
69
|
my $code = "$error if "; |
1233
|
51
|
100
|
|
|
|
91
|
$code .= "$check_exists && " if $check_exists; |
1234
|
51
|
|
|
|
|
81
|
$code .= "!$check"; |
1235
|
51
|
|
|
|
|
134
|
return "$code;"; |
1236
|
|
|
|
|
|
|
} |
1237
|
|
|
|
|
|
|
# If a subclass has overridden type_check(), we must use that. |
1238
|
|
|
|
|
|
|
else { |
1239
|
1
|
|
|
|
|
3
|
my $name = $sig->variable_name; |
1240
|
1
|
|
|
|
|
2
|
my $code = "${class}->type_check('@{[$sig->type]}', @{[$sig->passed_in]}, '$name')"; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
1241
|
1
|
50
|
|
|
|
2
|
$code .= "if $check_exists" if $check_exists; |
1242
|
1
|
|
|
|
|
3
|
return "$code;"; |
1243
|
|
|
|
|
|
|
} |
1244
|
|
|
|
|
|
|
} |
1245
|
|
|
|
|
|
|
|
1246
|
|
|
|
|
|
|
# This class method just dies with the message generated by signature_error. |
1247
|
|
|
|
|
|
|
# If necessary it can be overridden by a subclass to do something fancier. |
1248
|
|
|
|
|
|
|
# |
1249
|
|
|
|
|
|
|
sub signature_error_handler { |
1250
|
67
|
|
|
67
|
1
|
63
|
my ($class, $msg) = @_; |
1251
|
67
|
|
|
|
|
392
|
die $msg; |
1252
|
|
|
|
|
|
|
} |
1253
|
|
|
|
|
|
|
|
1254
|
|
|
|
|
|
|
# This is a common function to throw errors so that they appear to be from the point of the calling |
1255
|
|
|
|
|
|
|
# sub, not any of the Method::Signatures subs. |
1256
|
|
|
|
|
|
|
sub signature_error { |
1257
|
68
|
|
|
68
|
1
|
1197
|
my ($proto, $msg) = @_; |
1258
|
68
|
|
33
|
|
|
273
|
my $class = ref $proto || $proto; |
1259
|
|
|
|
|
|
|
|
1260
|
68
|
|
|
|
|
328
|
my ($file, $line, $method) = carp_location_for($class); |
1261
|
68
|
|
|
|
|
277
|
$class->signature_error_handler("In call to $method(), $msg at $file line $line.\n"); |
1262
|
|
|
|
|
|
|
} |
1263
|
|
|
|
|
|
|
|
1264
|
|
|
|
|
|
|
sub required_arg { |
1265
|
9
|
|
|
9
|
1
|
6456
|
my ($class, $var) = @_; |
1266
|
|
|
|
|
|
|
|
1267
|
9
|
|
|
|
|
87
|
$class->signature_error("missing required argument $var"); |
1268
|
|
|
|
|
|
|
} |
1269
|
|
|
|
|
|
|
|
1270
|
|
|
|
|
|
|
|
1271
|
|
|
|
|
|
|
sub required_placeholder_arg { |
1272
|
3
|
|
|
3
|
0
|
1183
|
my ($class, $idx) = @_; |
1273
|
|
|
|
|
|
|
|
1274
|
3
|
|
|
|
|
10
|
$class->signature_error("missing required placeholder argument at position $idx"); |
1275
|
|
|
|
|
|
|
} |
1276
|
|
|
|
|
|
|
|
1277
|
|
|
|
|
|
|
|
1278
|
|
|
|
|
|
|
# STUFF FOR TYPE CHECKING |
1279
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
# This variable will hold all the bits we need. MUTC could stand for Moose::Util::TypeConstraint, |
1281
|
|
|
|
|
|
|
# or it could stand for Mouse::Util::TypeConstraint ... depends on which one you've got loaded (or |
1282
|
|
|
|
|
|
|
# Mouse if you have neither loaded). Because we use Any::Moose to allow the user to choose |
1283
|
|
|
|
|
|
|
# whichever they like, we'll need to figure out the exact method names to call. We'll also need a |
1284
|
|
|
|
|
|
|
# type constraint cache, where we stick our constraints once we find or create them. This insures |
1285
|
|
|
|
|
|
|
# that we only have to run down any given constraint once, the first time it's seen, and then after |
1286
|
|
|
|
|
|
|
# that it's simple enough to pluck back out. This is very similar to how MooseX::Params::Validate |
1287
|
|
|
|
|
|
|
# does it. |
1288
|
|
|
|
|
|
|
our %mutc; |
1289
|
|
|
|
|
|
|
|
1290
|
|
|
|
|
|
|
# This is a helper function to initialize our %mutc variable. |
1291
|
|
|
|
|
|
|
sub _init_mutc |
1292
|
|
|
|
|
|
|
{ |
1293
|
13
|
|
|
13
|
|
5437
|
require Any::Moose; |
1294
|
13
|
|
|
|
|
15096
|
Any::Moose->import('::Util::TypeConstraints'); |
1295
|
|
|
|
|
|
|
|
1296
|
62
|
|
|
62
|
|
298
|
no strict 'refs'; |
|
62
|
|
|
|
|
70
|
|
|
62
|
|
|
|
|
28241
|
|
1297
|
13
|
|
|
|
|
5530
|
my $class = any_moose('::Util::TypeConstraints'); |
1298
|
13
|
|
|
|
|
491
|
$mutc{class} = $class; |
1299
|
|
|
|
|
|
|
|
1300
|
13
|
|
|
|
|
17
|
$mutc{findit} = \&{ $class . '::find_or_parse_type_constraint' }; |
|
13
|
|
|
|
|
61
|
|
1301
|
13
|
|
|
|
|
18
|
$mutc{pull} = \&{ $class . '::find_type_constraint' }; |
|
13
|
|
|
|
|
42
|
|
1302
|
13
|
|
|
|
|
17
|
$mutc{make_class} = \&{ $class . '::class_type' }; |
|
13
|
|
|
|
|
38
|
|
1303
|
13
|
|
|
|
|
16
|
$mutc{make_role} = \&{ $class . '::role_type' }; |
|
13
|
|
|
|
|
35
|
|
1304
|
|
|
|
|
|
|
|
1305
|
13
|
|
|
|
|
51
|
$mutc{isa_class} = $mutc{pull}->("ClassName"); |
1306
|
13
|
|
|
|
|
278
|
$mutc{isa_role} = $mutc{pull}->("RoleName"); |
1307
|
|
|
|
|
|
|
} |
1308
|
|
|
|
|
|
|
|
1309
|
|
|
|
|
|
|
# This is a helper function to find (or create) the constraint we need for a given type. It would |
1310
|
|
|
|
|
|
|
# be called when the type is not found in our cache. |
1311
|
|
|
|
|
|
|
sub _make_constraint |
1312
|
|
|
|
|
|
|
{ |
1313
|
31
|
|
|
31
|
|
325892
|
my ($class, $type) = @_; |
1314
|
|
|
|
|
|
|
|
1315
|
31
|
100
|
|
|
|
114
|
_init_mutc() unless $mutc{class}; |
1316
|
|
|
|
|
|
|
|
1317
|
|
|
|
|
|
|
# Look for basic types (Int, Str, Bool, etc). This will also create a new constraint for any |
1318
|
|
|
|
|
|
|
# parameterized types (e.g. ArrayRef[Int]) or any disjunctions (e.g. Int|ScalarRef[Int]). |
1319
|
31
|
|
|
|
|
206
|
my $constr = eval { $mutc{findit}->($type) }; |
|
31
|
|
|
|
|
91
|
|
1320
|
31
|
100
|
|
|
|
5984
|
if ($@) |
1321
|
|
|
|
|
|
|
{ |
1322
|
1
|
|
|
|
|
4
|
$class->signature_error("the type $type is unrecognized (looks like it doesn't parse correctly)"); |
1323
|
|
|
|
|
|
|
} |
1324
|
30
|
100
|
|
|
|
476
|
return $constr if $constr; |
1325
|
|
|
|
|
|
|
|
1326
|
|
|
|
|
|
|
# Check for roles. Note that you *must* check for roles before you check for classes, because a |
1327
|
|
|
|
|
|
|
# role ISA class. |
1328
|
5
|
50
|
|
|
|
43
|
return $mutc{make_role}->($type) if $mutc{isa_role}->check($type); |
1329
|
|
|
|
|
|
|
|
1330
|
|
|
|
|
|
|
# Now check for classes. |
1331
|
5
|
100
|
|
|
|
34
|
return $mutc{make_class}->($type) if $mutc{isa_class}->check($type); |
1332
|
|
|
|
|
|
|
|
1333
|
2
|
|
|
|
|
9
|
$class->signature_error("the type $type is unrecognized (perhaps you forgot to load it?)"); |
1334
|
|
|
|
|
|
|
} |
1335
|
|
|
|
|
|
|
|
1336
|
|
|
|
|
|
|
# This method does the actual type checking. It's what we inject into our user's method, to be |
1337
|
|
|
|
|
|
|
# called directly by them. |
1338
|
|
|
|
|
|
|
# |
1339
|
|
|
|
|
|
|
# Note that you can override this instead of inject_for_type_check if you'd rather. If you do, |
1340
|
|
|
|
|
|
|
# remember that this is a class method, not an object method. That's because it's called at |
1341
|
|
|
|
|
|
|
# runtime, when there is no Method::Signatures object still around. |
1342
|
|
|
|
|
|
|
sub type_check |
1343
|
|
|
|
|
|
|
{ |
1344
|
0
|
|
|
0
|
1
|
0
|
my ($class, $type, $value, $name) = @_; |
1345
|
|
|
|
|
|
|
|
1346
|
|
|
|
|
|
|
# find it if isn't cached |
1347
|
0
|
|
0
|
|
|
0
|
$mutc{cache}->{$type} ||= $class->_make_constraint($type); |
1348
|
|
|
|
|
|
|
|
1349
|
|
|
|
|
|
|
# throw an error if the type check fails |
1350
|
0
|
0
|
|
|
|
0
|
unless ($mutc{cache}->{$type}->check($value)) |
1351
|
|
|
|
|
|
|
{ |
1352
|
0
|
|
|
|
|
0
|
$class->type_error($type, $value, $name); |
1353
|
|
|
|
|
|
|
} |
1354
|
|
|
|
|
|
|
|
1355
|
|
|
|
|
|
|
# $mutc{cache} = {}; |
1356
|
|
|
|
|
|
|
} |
1357
|
|
|
|
|
|
|
|
1358
|
|
|
|
|
|
|
# If you just want to change what the type failure errors look like, just override this. |
1359
|
|
|
|
|
|
|
# Note that you can call signature_error yourself to handle the croak-like aspects. |
1360
|
|
|
|
|
|
|
sub type_error |
1361
|
|
|
|
|
|
|
{ |
1362
|
19
|
|
|
19
|
1
|
7718
|
my ($class, $type, $value, $name) = @_; |
1363
|
19
|
100
|
|
|
|
70
|
$value = defined $value ? qq{"$value"} : 'undef'; |
1364
|
19
|
|
|
|
|
119
|
$class->signature_error(qq{the '$name' parameter ($value) is not of type $type}); |
1365
|
|
|
|
|
|
|
} |
1366
|
|
|
|
|
|
|
|
1367
|
|
|
|
|
|
|
sub placeholder_type_error |
1368
|
|
|
|
|
|
|
{ |
1369
|
1
|
|
|
1
|
0
|
284
|
my ($class, $type, $value, $idx) = @_; |
1370
|
1
|
50
|
|
|
|
5
|
$value = defined $value ? qq{"$value"} : 'undef'; |
1371
|
1
|
|
|
|
|
5
|
$class->signature_error(qq{the placeholder parameter at position $idx ($value) is not of type $type}); |
1372
|
|
|
|
|
|
|
} |
1373
|
|
|
|
|
|
|
|
1374
|
|
|
|
|
|
|
# Errors from `where' constraints are handled here. |
1375
|
|
|
|
|
|
|
sub where_error |
1376
|
|
|
|
|
|
|
{ |
1377
|
15
|
|
|
15
|
1
|
22084
|
my ($class, $value, $name, $constraint) = @_; |
1378
|
15
|
100
|
|
|
|
39
|
$value = defined $value ? qq{"$value"} : 'undef'; |
1379
|
15
|
|
|
|
|
33
|
$class->signature_error(qq{$name value ($value) does not satisfy constraint: $constraint}); |
1380
|
|
|
|
|
|
|
} |
1381
|
|
|
|
|
|
|
|
1382
|
|
|
|
|
|
|
sub placeholder_where_error |
1383
|
|
|
|
|
|
|
{ |
1384
|
1
|
|
|
1
|
0
|
3
|
my ($class, $value, $idx, $constraint) = @_; |
1385
|
1
|
50
|
|
|
|
4
|
$value = defined $value ? qq{"$value"} : 'undef'; |
1386
|
1
|
|
|
|
|
5
|
$class->signature_error(qq{the placeholder parameter at position $idx value ($value) does not satisfy constraint: $constraint}); |
1387
|
|
|
|
|
|
|
} |
1388
|
|
|
|
|
|
|
|
1389
|
|
|
|
|
|
|
=head1 PERFORMANCE |
1390
|
|
|
|
|
|
|
|
1391
|
|
|
|
|
|
|
There is no run-time performance penalty for using this module above |
1392
|
|
|
|
|
|
|
what it normally costs to do argument handling. |
1393
|
|
|
|
|
|
|
|
1394
|
|
|
|
|
|
|
There is also no run-time penalty for type-checking if you do not |
1395
|
|
|
|
|
|
|
declare types. The run-time penalty if you do declare types should be |
1396
|
|
|
|
|
|
|
very similar to using L<Mouse::Util::TypeConstraints> (or |
1397
|
|
|
|
|
|
|
L<Moose::Util::TypeConstraints>) directly, and should be faster than |
1398
|
|
|
|
|
|
|
using a module such as L<MooseX::Params::Validate>. The magic of |
1399
|
|
|
|
|
|
|
L<Any::Moose> is used to give you the lightweight L<Mouse> if you have |
1400
|
|
|
|
|
|
|
not yet loaded L<Moose>, or the full-bodied L<Moose> if you have. |
1401
|
|
|
|
|
|
|
|
1402
|
|
|
|
|
|
|
Type-checking modules are not loaded until run-time, so this is fine: |
1403
|
|
|
|
|
|
|
|
1404
|
|
|
|
|
|
|
use Method::Signatures; |
1405
|
|
|
|
|
|
|
use Moose; |
1406
|
|
|
|
|
|
|
# you will still get Moose type checking |
1407
|
|
|
|
|
|
|
# (assuming you declare one or more methods with types) |
1408
|
|
|
|
|
|
|
|
1409
|
|
|
|
|
|
|
|
1410
|
|
|
|
|
|
|
=head1 DEBUGGING |
1411
|
|
|
|
|
|
|
|
1412
|
|
|
|
|
|
|
One of the best ways to figure out what Method::Signatures is doing is |
1413
|
|
|
|
|
|
|
to run your code through B::Deparse (run the code with -MO=Deparse). |
1414
|
|
|
|
|
|
|
|
1415
|
|
|
|
|
|
|
Setting the C<METHOD_SIGNATURES_DEBUG> environment variable will cause |
1416
|
|
|
|
|
|
|
Method::Signatures to display debugging information when it is |
1417
|
|
|
|
|
|
|
compiling signatures. |
1418
|
|
|
|
|
|
|
|
1419
|
|
|
|
|
|
|
=head1 EXAMPLE |
1420
|
|
|
|
|
|
|
|
1421
|
|
|
|
|
|
|
Here's an example of a method which displays some text and takes some |
1422
|
|
|
|
|
|
|
extra options. |
1423
|
|
|
|
|
|
|
|
1424
|
|
|
|
|
|
|
use Method::Signatures; |
1425
|
|
|
|
|
|
|
|
1426
|
|
|
|
|
|
|
method display($text is ro, :$justify = "left", :$fh = \*STDOUT) { |
1427
|
|
|
|
|
|
|
... |
1428
|
|
|
|
|
|
|
} |
1429
|
|
|
|
|
|
|
|
1430
|
|
|
|
|
|
|
# $text = $stuff, $justify = "left" and $fh = \*STDOUT |
1431
|
|
|
|
|
|
|
$obj->display($stuff); |
1432
|
|
|
|
|
|
|
|
1433
|
|
|
|
|
|
|
# $text = $stuff, $justify = "left" and $fh = \*STDERR |
1434
|
|
|
|
|
|
|
$obj->display($stuff, fh => \*STDERR); |
1435
|
|
|
|
|
|
|
|
1436
|
|
|
|
|
|
|
# error, missing required $text argument |
1437
|
|
|
|
|
|
|
$obj->display(); |
1438
|
|
|
|
|
|
|
|
1439
|
|
|
|
|
|
|
The display() method is equivalent to all this code. |
1440
|
|
|
|
|
|
|
|
1441
|
|
|
|
|
|
|
sub display { |
1442
|
|
|
|
|
|
|
my $self = shift; |
1443
|
|
|
|
|
|
|
|
1444
|
|
|
|
|
|
|
croak('display() missing required argument $text') unless @_ > 0; |
1445
|
|
|
|
|
|
|
const my $text = $_[0]; |
1446
|
|
|
|
|
|
|
|
1447
|
|
|
|
|
|
|
my(%args) = @_[1 .. $#_]; |
1448
|
|
|
|
|
|
|
my $justify = exists $args{justify} ? $args{justify} : 'left'; |
1449
|
|
|
|
|
|
|
my $fh = exists $args{fh} ? $args{'fh'} : \*STDOUT; |
1450
|
|
|
|
|
|
|
|
1451
|
|
|
|
|
|
|
... |
1452
|
|
|
|
|
|
|
} |
1453
|
|
|
|
|
|
|
|
1454
|
|
|
|
|
|
|
|
1455
|
|
|
|
|
|
|
=head1 EXPERIMENTING |
1456
|
|
|
|
|
|
|
|
1457
|
|
|
|
|
|
|
If you want to experiment with the prototype syntax, start with |
1458
|
|
|
|
|
|
|
C<Method::Signatures::parse_func>. It takes a method prototype |
1459
|
|
|
|
|
|
|
and returns a string of Perl 5 code which will be placed at the |
1460
|
|
|
|
|
|
|
beginning of that method. |
1461
|
|
|
|
|
|
|
|
1462
|
|
|
|
|
|
|
If you would like to try to provide your own type checking, subclass |
1463
|
|
|
|
|
|
|
L<Method::Signatures> and either override C<type_check> or |
1464
|
|
|
|
|
|
|
C<inject_for_type_check>. See L</EXTENDING>, below. |
1465
|
|
|
|
|
|
|
|
1466
|
|
|
|
|
|
|
This interface is experimental, unstable and will change between |
1467
|
|
|
|
|
|
|
versions. |
1468
|
|
|
|
|
|
|
|
1469
|
|
|
|
|
|
|
|
1470
|
|
|
|
|
|
|
=head1 EXTENDING |
1471
|
|
|
|
|
|
|
|
1472
|
|
|
|
|
|
|
If you wish to subclass Method::Signatures, the following methods are |
1473
|
|
|
|
|
|
|
good places to start. |
1474
|
|
|
|
|
|
|
|
1475
|
|
|
|
|
|
|
=head2 too_many_args_error, named_param_error, required_arg, type_error, where_error |
1476
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
These are class methods which report the various run-time errors |
1478
|
|
|
|
|
|
|
(extra parameters, unknown named parameter, required parameter |
1479
|
|
|
|
|
|
|
missing, parameter fails type check, and parameter fails where |
1480
|
|
|
|
|
|
|
constraint respectively). Note that each one calls |
1481
|
|
|
|
|
|
|
C<signature_error>, which your versions should do as well. |
1482
|
|
|
|
|
|
|
|
1483
|
|
|
|
|
|
|
=head2 signature_error |
1484
|
|
|
|
|
|
|
|
1485
|
|
|
|
|
|
|
This is a class method which calls C<signature_error_handler> (see |
1486
|
|
|
|
|
|
|
below) and reports the error as being from the caller's perspective. |
1487
|
|
|
|
|
|
|
Most likely you will not need to override this. If you'd like to have |
1488
|
|
|
|
|
|
|
Method::Signatures errors give full stack traces (similar to |
1489
|
|
|
|
|
|
|
C<$Carp::Verbose>), have a look at L<Carp::Always>. |
1490
|
|
|
|
|
|
|
|
1491
|
|
|
|
|
|
|
=head2 signature_error_handler |
1492
|
|
|
|
|
|
|
|
1493
|
|
|
|
|
|
|
By default, C<signature_error> generates an error message and |
1494
|
|
|
|
|
|
|
C<die>s with that message. If you need to do something fancier with |
1495
|
|
|
|
|
|
|
the generated error message, your subclass can define its own |
1496
|
|
|
|
|
|
|
C<signature_error_handler>. For example: |
1497
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
package My::Method::Signatures; |
1499
|
|
|
|
|
|
|
|
1500
|
|
|
|
|
|
|
use Moose; |
1501
|
|
|
|
|
|
|
extends 'Method::Signatures'; |
1502
|
|
|
|
|
|
|
|
1503
|
|
|
|
|
|
|
sub signature_error_handler { |
1504
|
|
|
|
|
|
|
my ($class, $msg) = @_; |
1505
|
|
|
|
|
|
|
die bless { message => $msg }, 'My::ExceptionClass'; |
1506
|
|
|
|
|
|
|
}; |
1507
|
|
|
|
|
|
|
|
1508
|
|
|
|
|
|
|
=head2 type_check |
1509
|
|
|
|
|
|
|
|
1510
|
|
|
|
|
|
|
This is a class method which is called to verify that parameters have |
1511
|
|
|
|
|
|
|
the proper type. If you want to change the way that |
1512
|
|
|
|
|
|
|
Method::Signatures does its type checking, this is most likely what |
1513
|
|
|
|
|
|
|
you want to override. It calls C<type_error> (see above). |
1514
|
|
|
|
|
|
|
|
1515
|
|
|
|
|
|
|
=head2 inject_for_type_check |
1516
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
This is the object method that actually inserts the call to |
1518
|
|
|
|
|
|
|
L</type_check> into your Perl code. Most likely you will not need to |
1519
|
|
|
|
|
|
|
override this, but if you wanted different parameters passed into |
1520
|
|
|
|
|
|
|
C<type_check>, this would be the place to do it. |
1521
|
|
|
|
|
|
|
|
1522
|
|
|
|
|
|
|
|
1523
|
|
|
|
|
|
|
=head1 BUGS, CAVEATS and NOTES |
1524
|
|
|
|
|
|
|
|
1525
|
|
|
|
|
|
|
Please report bugs and leave feedback at |
1526
|
|
|
|
|
|
|
E<lt>bug-Method-SignaturesE<gt> at E<lt>rt.cpan.orgE<gt>. Or use the |
1527
|
|
|
|
|
|
|
web interface at L<http://rt.cpan.org>. Report early, report often. |
1528
|
|
|
|
|
|
|
|
1529
|
|
|
|
|
|
|
=head2 One liners |
1530
|
|
|
|
|
|
|
|
1531
|
|
|
|
|
|
|
If you want to write "use Method::Signatures" in a one-liner, do a |
1532
|
|
|
|
|
|
|
C<-MMethod::Signatures> first. This is due to a bug/limitation in |
1533
|
|
|
|
|
|
|
Devel::Declare. |
1534
|
|
|
|
|
|
|
|
1535
|
|
|
|
|
|
|
=head2 Close parends in quotes or comments |
1536
|
|
|
|
|
|
|
|
1537
|
|
|
|
|
|
|
Because of the way L<Devel::Declare> parses things, an unbalanced |
1538
|
|
|
|
|
|
|
close parend inside a quote or comment could throw off the signature |
1539
|
|
|
|
|
|
|
parsing. For instance: |
1540
|
|
|
|
|
|
|
|
1541
|
|
|
|
|
|
|
func foo ( |
1542
|
|
|
|
|
|
|
$foo, # $foo might contain ) |
1543
|
|
|
|
|
|
|
$bar |
1544
|
|
|
|
|
|
|
) |
1545
|
|
|
|
|
|
|
|
1546
|
|
|
|
|
|
|
is going to produce a syntax error, because the parend inside the |
1547
|
|
|
|
|
|
|
comment is perceived as the end of the signature. On the other hand, |
1548
|
|
|
|
|
|
|
this: |
1549
|
|
|
|
|
|
|
|
1550
|
|
|
|
|
|
|
func foo ( |
1551
|
|
|
|
|
|
|
$foo, # (this is the $foo parend) |
1552
|
|
|
|
|
|
|
$bar |
1553
|
|
|
|
|
|
|
) |
1554
|
|
|
|
|
|
|
|
1555
|
|
|
|
|
|
|
is fine, because the parends in the comments are balanced. |
1556
|
|
|
|
|
|
|
|
1557
|
|
|
|
|
|
|
If you absolutely can't avoid an unbalanced close parend, such as in |
1558
|
|
|
|
|
|
|
the following signature: |
1559
|
|
|
|
|
|
|
|
1560
|
|
|
|
|
|
|
func foo ( $foo, $bar = ")" ) # this won't parse correctly |
1561
|
|
|
|
|
|
|
|
1562
|
|
|
|
|
|
|
you can always use a backslash to tell the parser that that close |
1563
|
|
|
|
|
|
|
parend doesn't indicate the end of the signature: |
1564
|
|
|
|
|
|
|
|
1565
|
|
|
|
|
|
|
func foo ( $foo, $bar = "\)" ) # this is fine |
1566
|
|
|
|
|
|
|
|
1567
|
|
|
|
|
|
|
This even works in single quotes: |
1568
|
|
|
|
|
|
|
|
1569
|
|
|
|
|
|
|
func foo ( $foo, $bar = '\)' ) # default is ')', *not* '\)'! |
1570
|
|
|
|
|
|
|
|
1571
|
|
|
|
|
|
|
although we don't recomment that form, as it may be surprising to |
1572
|
|
|
|
|
|
|
readers of your code. |
1573
|
|
|
|
|
|
|
|
1574
|
|
|
|
|
|
|
=head2 No source filter |
1575
|
|
|
|
|
|
|
|
1576
|
|
|
|
|
|
|
While this module does rely on the black magic of L<Devel::Declare> to |
1577
|
|
|
|
|
|
|
access Perl's own parser, it does not depend on a source filter. As |
1578
|
|
|
|
|
|
|
such, it doesn't try to parse and rewrite your source code and there |
1579
|
|
|
|
|
|
|
should be no weird side effects. |
1580
|
|
|
|
|
|
|
|
1581
|
|
|
|
|
|
|
Devel::Declare only affects compilation. After that, it's a normal |
1582
|
|
|
|
|
|
|
subroutine. As such, for all that hairy magic, this module is |
1583
|
|
|
|
|
|
|
surprisingly stable. |
1584
|
|
|
|
|
|
|
|
1585
|
|
|
|
|
|
|
=head2 Earlier Perl versions |
1586
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
The most noticeable is if an error occurs at compile time, such as a |
1588
|
|
|
|
|
|
|
strict error, perl might not notice until it tries to compile |
1589
|
|
|
|
|
|
|
something else via an C<eval> or C<require> at which point perl will |
1590
|
|
|
|
|
|
|
appear to fail where there is no reason to fail. |
1591
|
|
|
|
|
|
|
|
1592
|
|
|
|
|
|
|
We recommend you use the L<"compile_at_BEGIN"> flag to turn off |
1593
|
|
|
|
|
|
|
compile-time parsing. |
1594
|
|
|
|
|
|
|
|
1595
|
|
|
|
|
|
|
You can't use any feature that requires a smartmatch expression (i.e. |
1596
|
|
|
|
|
|
|
conditional L<"Defaults"> and L<"Value Constraints">) in Perl 5.8. |
1597
|
|
|
|
|
|
|
|
1598
|
|
|
|
|
|
|
Method::Signatures cannot be used with Perl versions prior to 5.8 |
1599
|
|
|
|
|
|
|
because L<Devel::Declare> does not work with those earlier versions. |
1600
|
|
|
|
|
|
|
|
1601
|
|
|
|
|
|
|
=head2 What about class methods? |
1602
|
|
|
|
|
|
|
|
1603
|
|
|
|
|
|
|
Right now there's nothing special about class methods. Just use |
1604
|
|
|
|
|
|
|
C<$class> as your invocant like the normal Perl 5 convention. |
1605
|
|
|
|
|
|
|
|
1606
|
|
|
|
|
|
|
There may be special syntax to separate class from object methods in |
1607
|
|
|
|
|
|
|
the future. |
1608
|
|
|
|
|
|
|
|
1609
|
|
|
|
|
|
|
=head2 What about the return value? |
1610
|
|
|
|
|
|
|
|
1611
|
|
|
|
|
|
|
Currently there is no support for declaring the type of the return |
1612
|
|
|
|
|
|
|
value. |
1613
|
|
|
|
|
|
|
|
1614
|
|
|
|
|
|
|
=head2 How does this relate to Perl's built-in prototypes? |
1615
|
|
|
|
|
|
|
|
1616
|
|
|
|
|
|
|
It doesn't. Perl prototypes are a rather different beastie from |
1617
|
|
|
|
|
|
|
subroutine signatures. They don't work on methods anyway. |
1618
|
|
|
|
|
|
|
|
1619
|
|
|
|
|
|
|
A syntax for function prototypes is being considered. |
1620
|
|
|
|
|
|
|
|
1621
|
|
|
|
|
|
|
func($foo, $bar?) is proto($;$) |
1622
|
|
|
|
|
|
|
|
1623
|
|
|
|
|
|
|
=head2 Error checking |
1624
|
|
|
|
|
|
|
|
1625
|
|
|
|
|
|
|
Here's some additional checks I would like to add, mostly to avoid |
1626
|
|
|
|
|
|
|
ambiguous or non-sense situations. |
1627
|
|
|
|
|
|
|
|
1628
|
|
|
|
|
|
|
* If one positional param is optional, everything to the right must be optional |
1629
|
|
|
|
|
|
|
|
1630
|
|
|
|
|
|
|
method foo($a, $b?, $c?) # legal |
1631
|
|
|
|
|
|
|
|
1632
|
|
|
|
|
|
|
method bar($a, $b?, $c) # illegal, ambiguous |
1633
|
|
|
|
|
|
|
|
1634
|
|
|
|
|
|
|
Does C<< ->bar(1,2) >> mean $a = 1 and $b = 2 or $a = 1, $c = 3? |
1635
|
|
|
|
|
|
|
|
1636
|
|
|
|
|
|
|
* Positionals are resolved before named params. They have precedence. |
1637
|
|
|
|
|
|
|
|
1638
|
|
|
|
|
|
|
|
1639
|
|
|
|
|
|
|
=head2 Slurpy parameter restrictions |
1640
|
|
|
|
|
|
|
|
1641
|
|
|
|
|
|
|
Slurpy parameters are currently more restricted than they need to be. |
1642
|
|
|
|
|
|
|
It is possible to work out a slurpy parameter in the middle, or a |
1643
|
|
|
|
|
|
|
named slurpy parameter. However, there's lots of edge cases and |
1644
|
|
|
|
|
|
|
possible nonsense configurations. Until that's worked out, we've left |
1645
|
|
|
|
|
|
|
it restricted. |
1646
|
|
|
|
|
|
|
|
1647
|
|
|
|
|
|
|
=head2 What about... |
1648
|
|
|
|
|
|
|
|
1649
|
|
|
|
|
|
|
Method traits are in the pondering stage. |
1650
|
|
|
|
|
|
|
|
1651
|
|
|
|
|
|
|
An API to query a method's signature is in the pondering stage. |
1652
|
|
|
|
|
|
|
|
1653
|
|
|
|
|
|
|
Now that we have method signatures, multi-methods are a distinct possibility. |
1654
|
|
|
|
|
|
|
|
1655
|
|
|
|
|
|
|
Applying traits to all parameters as a short-hand? |
1656
|
|
|
|
|
|
|
|
1657
|
|
|
|
|
|
|
# Equivalent? |
1658
|
|
|
|
|
|
|
method foo($a is ro, $b is ro, $c is ro) |
1659
|
|
|
|
|
|
|
method foo($a, $b, $c) is ro |
1660
|
|
|
|
|
|
|
|
1661
|
|
|
|
|
|
|
L<Role::Basic> roles are currently not recognized by the type system. |
1662
|
|
|
|
|
|
|
|
1663
|
|
|
|
|
|
|
A "go really fast" switch. Turn off all runtime checks that might |
1664
|
|
|
|
|
|
|
bite into performance. |
1665
|
|
|
|
|
|
|
|
1666
|
|
|
|
|
|
|
Method traits. |
1667
|
|
|
|
|
|
|
|
1668
|
|
|
|
|
|
|
method add($left, $right) is predictable # declarative |
1669
|
|
|
|
|
|
|
method add($left, $right) is cached # procedural |
1670
|
|
|
|
|
|
|
# (and Perl 6 compatible) |
1671
|
|
|
|
|
|
|
|
1672
|
|
|
|
|
|
|
|
1673
|
|
|
|
|
|
|
=head1 THANKS |
1674
|
|
|
|
|
|
|
|
1675
|
|
|
|
|
|
|
Most of this module is based on or copied from hard work done by many |
1676
|
|
|
|
|
|
|
other people. |
1677
|
|
|
|
|
|
|
|
1678
|
|
|
|
|
|
|
All the really scary parts are copied from or rely on Matt Trout's, |
1679
|
|
|
|
|
|
|
Florian Ragwitz's and Rhesa Rozendaal's L<Devel::Declare> work. |
1680
|
|
|
|
|
|
|
|
1681
|
|
|
|
|
|
|
The prototype syntax is a slight adaptation of all the |
1682
|
|
|
|
|
|
|
excellent work the Perl 6 folks have already done. |
1683
|
|
|
|
|
|
|
|
1684
|
|
|
|
|
|
|
The type checking and method modifier work was supplied by Buddy |
1685
|
|
|
|
|
|
|
Burden (barefootcoder). Thanks to this, you can now use |
1686
|
|
|
|
|
|
|
Method::Signatures (or, more properly, |
1687
|
|
|
|
|
|
|
L<Method::Signatures::Modifiers>) instead of |
1688
|
|
|
|
|
|
|
L<MooseX::Method::Signatures>, which fixes many of the problems |
1689
|
|
|
|
|
|
|
commonly attributed to L<MooseX::Declare>. |
1690
|
|
|
|
|
|
|
|
1691
|
|
|
|
|
|
|
Value constraints and default conditions (i.e. "where" and "when") |
1692
|
|
|
|
|
|
|
were added by Damian Conway, who also rewrote some of the signature |
1693
|
|
|
|
|
|
|
parsing to make it more robust and more extensible. |
1694
|
|
|
|
|
|
|
|
1695
|
|
|
|
|
|
|
Also thanks to Matthijs van Duin for his awesome L<Data::Alias> which |
1696
|
|
|
|
|
|
|
makes the C<\@foo> signature work perfectly and L<Sub::Name> which |
1697
|
|
|
|
|
|
|
makes the subroutine names come out right in caller(). |
1698
|
|
|
|
|
|
|
|
1699
|
|
|
|
|
|
|
And thanks to Florian Ragwitz for his parallel |
1700
|
|
|
|
|
|
|
L<MooseX::Method::Signatures> module from which I borrow ideas and |
1701
|
|
|
|
|
|
|
code. |
1702
|
|
|
|
|
|
|
|
1703
|
|
|
|
|
|
|
|
1704
|
|
|
|
|
|
|
=head1 LICENSE |
1705
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
The original code was taken from Matt S. Trout's tests for L<Devel::Declare>. |
1707
|
|
|
|
|
|
|
|
1708
|
|
|
|
|
|
|
Copyright 2007-2012 by Michael G Schwern E<lt>schwern@pobox.comE<gt>. |
1709
|
|
|
|
|
|
|
|
1710
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
1711
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
1712
|
|
|
|
|
|
|
|
1713
|
|
|
|
|
|
|
See F<http://www.perl.com/perl/misc/Artistic.html> |
1714
|
|
|
|
|
|
|
|
1715
|
|
|
|
|
|
|
|
1716
|
|
|
|
|
|
|
=head1 SEE ALSO |
1717
|
|
|
|
|
|
|
|
1718
|
|
|
|
|
|
|
L<MooseX::Method::Signatures> for an alternative implementation. |
1719
|
|
|
|
|
|
|
|
1720
|
|
|
|
|
|
|
L<Perl6::Signature> for a more complete implementation of Perl 6 signatures. |
1721
|
|
|
|
|
|
|
|
1722
|
|
|
|
|
|
|
L<Method::Signatures::Simple> for a more basic version of what Method::Signatures provides. |
1723
|
|
|
|
|
|
|
|
1724
|
|
|
|
|
|
|
L<Function::Parameters> for a subset of Method::Signature's features without using L<Devel::Declare>. |
1725
|
|
|
|
|
|
|
|
1726
|
|
|
|
|
|
|
L<signatures> for C<sub> with signatures. |
1727
|
|
|
|
|
|
|
|
1728
|
|
|
|
|
|
|
Perl 6 subroutine parameters and arguments - L<http://perlcabal.org/syn/S06.html#Parameters_and_arguments> |
1729
|
|
|
|
|
|
|
|
1730
|
|
|
|
|
|
|
L<Moose::Util::TypeConstraints> or L<Mouse::Util::TypeConstraints> for |
1731
|
|
|
|
|
|
|
further details on how the type-checking works. |
1732
|
|
|
|
|
|
|
|
1733
|
|
|
|
|
|
|
=cut |
1734
|
|
|
|
|
|
|
|
1735
|
|
|
|
|
|
|
|
1736
|
|
|
|
|
|
|
1; |