File Coverage

blib/lib/Function/Parameters.pm
Criterion Covered Total %
statement 201 213 94.3
branch 88 110 80.0
condition 12 16 75.0
subroutine 31 33 93.9
pod 0 1 0.0
total 332 373 89.0


line stmt bran cond sub pod time code
1             package Function::Parameters 2.002006;
2              
3 49     49   6848355 use v5.14.0;
  49         196  
4 49     49   326 use warnings;
  49         129  
  49         3407  
5 49     49   383 use warnings::register;
  49         199  
  49         4047  
6              
7 49     49   420 use Carp qw(croak confess);
  49         205  
  49         3738  
8 49     49   375 use Scalar::Util qw(blessed);
  49         103  
  49         11135  
9              
10             sub _croak {
11 251     251   3032436 my (undef, $file, $line) = caller 1;
12 251         2502 die @_, " at $file line $line.\n";
13             }
14              
15 49     49   346 use XSLoader;
  49         97  
  49         1715  
16             BEGIN {
17 49     49   92845 XSLoader::load;
18             }
19              
20             sub _warn_config_not_a_reference {
21 2     2   2615 warnings::warnif sprintf q{%s: $^H{'%s'} is not a reference; skipping: %s}, __PACKAGE__, HINTK_CONFIG, $^H{+HINTK_CONFIG};
22             }
23              
24             sub _assert_valid_identifier {
25 278     278   623 my ($name, $with_dollar) = @_;
26 278 100       4744 my $bonus = $with_dollar ? '\$' : '';
27 278 100       19203 $name =~ /\A${bonus}[^\W\d]\w*\z/
28             or confess qq{"$name" doesn't look like a valid identifier};
29             }
30              
31             sub _assert_valid_attributes {
32 78     78   228 my ($attrs) = @_;
33 78 100       1297 $attrs =~ m{
34             \A \s*+
35             : \s*+
36             (?&ident) (?! [^\s:(] ) (?&param)?+ \s*+
37             (?:
38             (?: : \s*+ )?
39             (?&ident) (?! [^\s:(] ) (?&param)?+ \s*+
40             )*+
41             \z
42              
43             (?(DEFINE)
44             (?<ident>
45             [^\W\d]
46             \w*+
47             )
48             (?<param>
49             \(
50             [^()\\]*+
51             (?:
52             (?:
53             \\ .
54             |
55             (?&param)
56             )
57             [^()\\]*+
58             )*+
59             \)
60             )
61             )
62             }sx or confess qq{"$attrs" doesn't look like valid attributes};
63             }
64              
65             sub _reify_type_moose {
66 0     0   0 require Moose::Util::TypeConstraints;
67 0         0 Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_[0])
68             }
69              
70             sub _malformed_type {
71 0     0   0 my ($type, $msg) = @_;
72 0         0 my $pos = pos $_[0];
73 0         0 substr $type, $pos, 0, ' <-- HERE ';
74 0         0 croak "Malformed type: $msg marked by <-- HERE in '$type'";
75             }
76              
77             sub _reify_type_auto_parameterized {
78             # (str, caller)
79 72 50   72   276 $_[0] =~ /\G ( \w+ (?: :: \w+)* ) \s* /xgc or _malformed_type $_[0], "missing type name";
80 72         173 my $name = $1;
81 72 50       265 $name = "$_[1]::$name" unless $name =~ /::/;
82 72         137 my $fun = do {
83 49     49   429 no strict 'refs';
  49         95  
  49         168236  
84 72 100       849 defined &$name or croak "Undefined type name $name";
85 70         145 \&$name
86             };
87              
88 70 100       223 $_[0] =~ /\G \[ \s* /xgc
89             or return $fun;
90              
91 8         14 my @args;
92 8         20 until ($_[0] =~ /\G \] \s* /xgc) {
93 16 100 33     53 $_[0] =~ /\G , \s* /xgc or _malformed_type $_[0], "missing ',' or ']'"
94             if @args;
95 16         41 push @args, &_reify_type_auto_union;
96             }
97              
98 8     8   51 sub { $fun->([map $_->(), @args]) }
99 8         49 }
100              
101             sub _reify_type_auto_term {
102             # (str, caller)
103 76     76   126 my $compl = 0;
104 76         219 while ($_[0] =~ /\G ~ \s* /xgc) {
105 8         41 $compl++;
106             }
107              
108 76         103 my $inner;
109 76 100       194 if ($_[0] =~ /\G \( \s* /xgc) {
110 4         11 $inner = &_reify_type_auto_union;
111 4 50       28 $_[0] =~ /\G \) \s* /xgc or _malformed_type $_[0], "missing ')'";
112             } else {
113 72         147 $inner = &_reify_type_auto_parameterized;
114             }
115              
116             !$compl
117             ? $inner
118             : sub {
119 4     4   162 my $t = $inner->();
120 4         56 for my $i (1 .. $compl) {
121 8         68 $t = ~$t;
122             }
123             $t
124 4         59 }
125 74 100       192 }
126              
127             sub _reify_type_auto_alternative {
128             # (str, caller)
129 72     72   152 my $fun = &_reify_type_auto_term;
130 70         156 while ($_[0] =~ m!\G / \s* !xgc) {
131 4         22 my $right = &_reify_type_auto_term;
132 4         8 my $left = $fun;
133 4     4   23 $fun = sub { $left->() / $right->() };
  4         233  
134             }
135             $fun
136 70         122 }
137              
138             sub _reify_type_auto_intersection {
139             # (str, caller)
140 64     64   123 my $fun = &_reify_type_auto_alternative;
141 62         139 while ($_[0] =~ /\G & \s* /xgc) {
142 8         15 my $right = &_reify_type_auto_alternative;
143 8         15 my $left = $fun;
144 8     8   44 $fun = sub { $left->() & $right->() };
  8         198  
145             }
146             $fun
147 62         109 }
148              
149             sub _reify_type_auto_union {
150             # (str, caller)
151 50     50   96 my $fun = &_reify_type_auto_intersection;
152 48         134 while ($_[0] =~ /\G \| \s* /xgc) {
153 14         32 my $right = &_reify_type_auto_intersection;
154 14         24 my $left = $fun;
155 14     14   108 $fun = sub { $left->() | $right->() };
  14         359  
156             }
157             $fun
158 48         109 }
159              
160             sub _reify_type_auto {
161 30     30   644250 my ($type) = @_;
162 30         64 my $caller = caller;
163              
164 30         118 $type =~ /\G \s+ /xgc;
165 30         111 my $tfun = _reify_type_auto_union $type, $caller;
166 28 50       79 $type =~ /\G \z/xgc or _malformed_type $type, "trailing garbage";
167 28         1346 $tfun->()
168             }
169              
170             sub _delete_default {
171 1176     1176   2053 my ($href, $key, $default) = @_;
172 1176 100       3132 exists $href->{$key} ? delete $href->{$key} : $default
173             }
174              
175             sub _find_or_add_idx {
176 1     1   3 my ($array, $x) = @_;
177 1         2 my $index;
178 1         6 for my $i (0 .. $#$array) {
179 0 0       0 if ($array->[$i] == $x) {
180 0         0 $index = $i;
181 0         0 last;
182             }
183             }
184 1 50       5 unless (defined $index) {
185 1         3 $index = @$array;
186 1         3 push @$array, $x;
187             }
188             $index
189 1         3 }
190              
191             my %type_map = (
192             function_strict => {},
193             function_lax => {
194             defaults => 'function_strict',
195             strict => 0,
196             },
197             function => { defaults => 'function_strict' },
198              
199             method_strict => {
200             defaults => 'function_strict',
201             attributes => ':method',
202             shift => '$self',
203             invocant => 1,
204             },
205             method_lax => {
206             defaults => 'method_strict',
207             strict => 0,
208             },
209             method => { defaults => 'method_strict' },
210              
211             classmethod_strict => {
212             defaults => 'method_strict',
213             shift => '$class',
214             },
215             classmethod_lax => {
216             defaults => 'classmethod_strict',
217             strict => 0,
218             },
219             classmethod => { defaults => 'classmethod_strict' },
220              
221             around => {
222             defaults => 'method',
223             name => 'required',
224             install_sub => 'around',
225             shift => ['$orig', '$self'],
226             runtime => 1,
227             },
228             (
229             map +(
230             $_ => {
231             defaults => 'method',
232             name => 'required',
233             install_sub => $_,
234             runtime => 1,
235             }
236             ), qw(
237             before after augment override
238             ),
239             ),
240             );
241              
242             my %import_map = (
243             fun => 'function',
244             (
245             map +($_ => $_),
246             qw(
247             method
248             classmethod
249             before
250             after
251             around
252             augment
253             override
254             )
255             ),
256              
257             ':strict' => {
258             fun => 'function_strict',
259             method => 'method_strict',
260             },
261              
262             ':lax' => {
263             fun => 'function_lax',
264             method => 'method_lax',
265             },
266              
267             ':std' => [qw(fun method)],
268             ':modifiers' => [qw(
269             before
270             after
271             around
272             augment
273             override
274             )],
275             );
276             for my $v (values %import_map) {
277             if (ref $v eq 'ARRAY') {
278             $v = {
279             map +($_ => $import_map{$_} || die "Internal error: $v => $_"),
280             @$v
281             };
282             }
283             }
284              
285             sub import {
286 96     96   528232 my $class = shift;
287              
288 96         195 my %imports;
289 96 100       450 @_ = qw(:std) if !@_;
290 96         267 for my $item (@_) {
291 102         209 my $part;
292 102 100       336 if (ref $item) {
293 51         100 $part = $item;
294             } else {
295 51 100       520 my $type = $import_map{$item}
296             or croak qq{"$item" is not exported by the $class module};
297 49 100       148 $part = ref $type
298             ? $type
299             : { $item => $type };
300             }
301 100         632 @imports{keys %$part} = values %$part;
302             }
303              
304 94         176 my %spec;
305              
306 94         449 for my $name (sort keys %imports) {
307 176         561 _assert_valid_identifier $name;
308 171         536 my $proto_type = $imports{$name};
309              
310 171 100       632 $proto_type = {defaults => $proto_type} unless ref $proto_type;
311              
312 171         650 my %type = %$proto_type;
313 171         640 while (my $defaults = delete $type{defaults}) {
314 345 100       913 my $base = $type_map{$defaults}
315 1         265 or confess qq["$defaults" doesn't look like a valid type (one of ${\join ', ', sort keys %type_map})];
316 344         1569 %type = (%$base, %type);
317             }
318              
319 170 100       473 if (exists $type{strict}) {
320 21   66     135 $type{check_argument_count} ||= $type{strict};
321 21         39 delete $type{strict};
322             }
323              
324 170         261 my %clean;
325              
326 170   100     811 $clean{name} = delete $type{name} // 'optional';
327 170 50       861 $clean{name} =~ /\A(?:optional|required|prohibited)\z/
328             or confess qq["$clean{name}" doesn't look like a valid name attribute (one of optional, required, prohibited)];
329              
330 170   100     643 $clean{attrs} = delete $type{attributes} // '';
331 170 100       623 _assert_valid_attributes $clean{attrs} if $clean{attrs};
332              
333 168 100       438 if (!exists $type{reify_type}) {
334 164         402 $clean{reify_type} = \&_reify_type_auto;
335             } else {
336 4   50     12 my $rt = delete $type{reify_type} // '(undef)';
337 4 50       20 if (!ref $rt) {
    50          
338 0 0       0 $rt =
    0          
339             $rt eq 'auto' ? \&_reify_type_auto :
340             $rt eq 'moose' ? \&_reify_type_moose :
341             confess qq{"$rt" isn't a known predefined type reifier};
342             } elsif (ref $rt ne 'CODE') {
343 0         0 confess qq{"$rt" doesn't look like a type reifier};
344             }
345              
346 4         11 $clean{reify_type} = $rt;
347             }
348              
349 168 100       373 if (!exists $type{install_sub}) {
350 150         288 $clean{install_sub} = '';
351             } else {
352 18         32 my $is = delete $type{install_sub};
353 18 100       46 if (!ref $is) {
    50          
354 17         51 _assert_valid_identifier $is;
355             } elsif (ref $is ne 'CODE') {
356 0         0 confess qq{"$is" doesn't look like a sub installer};
357             }
358              
359 18         71 $clean{install_sub} = $is;
360             }
361              
362 168         252 $clean{shift} = do {
363 168   100     562 my $shift = delete $type{shift} // [];
364 168 100       462 $shift = [$shift] if !ref $shift;
365 168         287 my $str = '';
366 168         262 my @shifty_types;
367 168         362 for my $item (@$shift) {
368 85         167 my ($name, $type);
369 85 100       222 if (ref $item) {
370 1 50       4 @$item == 2 or confess "A 'shift' item must have 2 elements, not " . @$item;
371 1         3 ($name, $type) = @$item;
372             } else {
373 84         166 $name = $item;
374             }
375 85         245 _assert_valid_identifier $name, 1;
376 85 50       488 $name eq '$_' and confess q[Using "$_" as a parameter is not supported];
377 85         284 $str .= $name;
378 85 100       259 if (defined $type) {
379 1 50       6 blessed($type) or confess "${name}'s type must be an object, not $type";
380 1         5 my $index = _find_or_add_idx \@shifty_types, $type;
381 1         4 $str .= "/$index";
382             }
383 85         241 $str .= ' ';
384             }
385 168         373 $clean{shift_types} = \@shifty_types;
386 168         644 $str
387             };
388              
389 168         507 $clean{default_arguments} = _delete_default \%type, 'default_arguments', 1;
390 168         376 $clean{named_parameters} = _delete_default \%type, 'named_parameters', 1;
391 168         370 $clean{types} = _delete_default \%type, 'types', 1;
392 168         373 $clean{invocant} = _delete_default \%type, 'invocant', 0;
393 168         364 $clean{runtime} = _delete_default \%type, 'runtime', 0;
394 168         350 $clean{check_argument_count} = _delete_default \%type, 'check_argument_count', 1;
395 168         342 $clean{check_argument_types} = _delete_default \%type, 'check_argument_types', 1;
396              
397 168 100       456 %type and confess "Invalid keyword property: @{[sort keys %type]}";
  1         244  
398              
399 167         672 $spec{$name} = \%clean;
400             }
401              
402 85   100     197 my %config = %{$^H{+HINTK_CONFIG} // {}};
  85         544  
403 85         294 for my $kw (keys %spec) {
404 167         369 my $type = $spec{$kw};
405              
406             my $flags =
407             $type->{name} eq 'prohibited' ? FLAG_ANON_OK :
408 167 100       621 $type->{name} eq 'required' ? FLAG_NAME_OK :
    100          
409             FLAG_ANON_OK | FLAG_NAME_OK
410             ;
411 167 100       484 $flags |= FLAG_DEFAULT_ARGS if $type->{default_arguments};
412 167 100       467 $flags |= FLAG_CHECK_NARGS if $type->{check_argument_count};
413 167 50       431 $flags |= FLAG_CHECK_TARGS if $type->{check_argument_types};
414 167 100       375 $flags |= FLAG_INVOCANT if $type->{invocant};
415 167 50       431 $flags |= FLAG_NAMED_PARAMS if $type->{named_parameters};
416 167 50       379 $flags |= FLAG_TYPES_OK if $type->{types};
417 167 100       402 $flags |= FLAG_RUNTIME if $type->{runtime};
418             $config{$kw} = {
419             HINTSK_FLAGS, => $flags,
420             HINTSK_SHIFT, => $type->{shift},
421             HINTSK_ATTRS, => $type->{attrs},
422             HINTSK_REIFY, => $type->{reify_type},
423             HINTSK_INSTL, => $type->{install_sub},
424 167         1033 !@{$type->{shift_types}} ? () : (
425             HINTSK_SHIF2, => $type->{shift_types},
426 167 100       383 ),
427             };
428             }
429 85         19354 $^H{+HINTK_CONFIG} = \%config;
430             }
431              
432             sub unimport {
433 8     8   57 my $class = shift;
434              
435 8 100       18 if (!@_) {
436 3         11 delete $^H{+HINTK_CONFIG};
437 3         265 return;
438             }
439              
440 5         8 my %config = %{$^H{+HINTK_CONFIG}};
  5         19  
441 5         13 delete @config{@_};
442 5         377 $^H{+HINTK_CONFIG} = \%config;
443             }
444              
445              
446             our %metadata;
447              
448             sub _register_info {
449             my (
450 556     556   1284308 $key,
451             $declarator,
452             $shift,
453             $positional_required,
454             $positional_optional,
455             $named_required,
456             $named_optional,
457             $slurpy,
458             $slurpy_type,
459             ) = @_;
460              
461 556 100       4067 my $info = {
462             declarator => $declarator,
463             shift => $shift,
464             positional_required => $positional_required,
465             positional_optional => $positional_optional,
466             named_required => $named_required,
467             named_optional => $named_optional,
468             slurpy => defined $slurpy ? [$slurpy, $slurpy_type] : undef,
469             };
470              
471 556         154175 $metadata{$key} = $info;
472             }
473              
474             sub _mkparam1 {
475 16     16   42 my ($pair) = @_;
476 16 100       30 my ($v, $t) = @{$pair || []} or return undef;
  16 100       187  
477 4         20 Function::Parameters::Param->new(
478             name => $v,
479             type => $t,
480             )
481             }
482              
483             sub _mkparams {
484 64     64   76 my @r;
485 64         177 while (my ($v, $t) = splice @_, 0, 2) {
486 38         121 push @r, Function::Parameters::Param->new(
487             name => $v,
488             type => $t,
489             );
490             }
491             \@r
492 64         241 }
493              
494             sub info {
495 18     18 0 151476 my ($func) = @_;
496 18 50       122 my $key = _cv_root $func or return undef;
497 18 100       90 my $info = $metadata{$key} or return undef;
498 16         4530 require Function::Parameters::Info;
499             Function::Parameters::Info->new(
500             keyword => $info->{declarator},
501             nshift => $info->{shift},
502             slurpy => _mkparam1($info->{slurpy}),
503             (
504 16         99 map +("_$_" => _mkparams @{$info->{$_}}),
  64         163  
505             qw(
506             positional_required
507             positional_optional
508             named_required
509             named_optional
510             )
511             )
512             )
513             }
514              
515             'ok'
516              
517             __END__
518              
519             =encoding UTF-8
520              
521             =for highlighter language=perl
522              
523             =head1 NAME
524              
525             Function::Parameters - define functions and methods with parameter lists ("subroutine signatures")
526              
527             =head1 SYNOPSIS
528              
529             use Function::Parameters;
530              
531             # plain function
532             fun foo($x, $y, $z = 5) {
533             return $x + $y + $z;
534             }
535             print foo(1, 2), "\n"; # 8
536              
537             # method with implicit $self
538             method bar($label, $n) {
539             return "$label: " . ($n * $self->scale);
540             }
541              
542             # named arguments: order doesn't matter in the call
543             fun create_point(:$x, :$y, :$color) {
544             print "creating a $color point at ($x, $y)\n";
545             }
546             create_point(
547             color => "red",
548             x => 10,
549             y => 5,
550             );
551              
552             package Derived {
553             use Function::Parameters qw(:std :modifiers);
554             use Moo;
555              
556             extends 'Base';
557              
558             has 'go_big' => (
559             is => 'ro',
560             );
561              
562             # "around" method with implicit $orig and $self
563             around size() {
564             return $self->$orig() * 2 if $self->go_big;
565             return $self->$orig();
566             }
567             }
568              
569             =head1 DESCRIPTION
570              
571             This module provides two new keywords, C<fun> and C<method>, for defining
572             functions and methods with parameter lists. At minimum this saves you from
573             having to unpack C<@_> manually, but this module can do much more for you.
574              
575             The parameter lists provided by this module are similar to the C<signatures>
576             feature available in perl v5.20+. However, this module supports all perl
577             versions starting from v5.14 and it offers far more features than core
578             signatures. The downside is that you need a C compiler if you want to install
579             it from source, as it uses Perl's L<keyword plugin|perlapi/PL_keyword_plugin>
580             API in order to work reliably without requiring a source filter.
581              
582             =head2 Default functionality
583              
584             This module is a lexically scoped pragma: If you C<use Function::Parameters>
585             inside a block or file, the keywords won't be available outside of that block
586             or file.
587              
588             You can also disable C<Function::Parameters> within a block:
589              
590             {
591             no Function::Parameters; # disable all keywords
592             ...
593             }
594              
595             Or explicitly list the keywords you want to disable:
596              
597             {
598             no Function::Parameters qw(method);
599             # 'method' is a normal identifier here
600             ...
601             }
602              
603             You can also explicitly list the keywords you want to enable:
604              
605             use Function::Parameters qw(fun); # provides 'fun' but not 'method'
606             use Function::Parameters qw(method); # provides 'method' but not 'fun'
607              
608             =head3 Simple parameter lists
609              
610             By default you get two keywords, C<fun> and C<method> (but see
611             L</Customizing and extending> below). C<fun> is very similar to C<sub>. You can
612             use it to define both named and anonymous functions:
613              
614             fun left_pad($str, $n) {
615             return sprintf '%*s', $n, $str;
616             }
617              
618             print left_pad("hello", 10), "\n";
619              
620             my $twice = fun ($x) { $x * 2 };
621             print $twice->(21), "\n";
622              
623             In the simplest case the parameter list is just a comma-separated list of zero
624             or more scalar variables (enclosed in parentheses, following the function name,
625             if any).
626              
627             C<Function::Parameters> automatically validates the arguments your function is
628             called with. If the number of arguments doesn't match the parameter list, an
629             exception is thrown.
630              
631             Apart from that, the parameter variables are defined and initialized as if by:
632              
633             sub left_pad {
634             sub left_pad;
635             my ($str, $n) = @_;
636             ...
637             }
638              
639             In particular, C<@_> is still available in functions defined by C<fun> and
640             holds the original argument list.
641              
642             The inner C<sub left_pad;> declaration is intended to illustrate that the name
643             of the function being defined is in scope in its own body, meaning you can call
644             it recursively without having to use parentheses:
645              
646             fun fac($n) {
647             return 1 if $n < 2;
648             return $n * fac $n - 1;
649             }
650              
651             In a normal C<sub> the last line would have had to be written
652             C<return $n * fac($n - 1);>.
653              
654             C<method> is almost the same as C<fun> but automatically creates a C<$self>
655             variable as the first parameter (which is removed from C<@_>):
656              
657             method foo($x, $y) {
658             ...
659             }
660              
661             # works like:
662             sub foo :method {
663             my $self = shift;
664             my ($x, $y) = @_;
665             ...
666             }
667              
668             As you can see, the C<:method> attribute is also added automatically (see
669             L<attributes/method> for details).
670              
671             In some cases (e.g. class methods) C<$self> is not the best name for the
672             invocant of the method. You can override it on a case-by-case basis by putting
673             a variable name followed by a C<:> (colon) as the first thing in the parameter
674             list:
675              
676             method new($class: $x, $y) {
677             return bless { x => $x, y => $y }, $class;
678             }
679              
680             Here the invocant is named C<$class>, not C<$self>. It looks a bit weird but
681             still works the same way if the remaining parameter list is empty:
682              
683             method from_env($class:) {
684             return $class->new($ENV{x}, $ENV{y});
685             }
686              
687             =head3 Default arguments
688              
689             (Most of the following examples use C<fun> only. Unless specified otherwise,
690             everything applies to C<method> as well.)
691              
692             You can make some arguments optional by giving them default values.
693              
694             fun passthrough($x, $y //= 42, $z = []) {
695             return ($x, $y, $z);
696             }
697              
698             In this example the first parameter C<$x> is required, but C<$y> and C<$z> are
699             optional.
700              
701             passthrough('a', 'b', 'c', 'd') # error: Too many arguments
702             passthrough('a', 'b', 'c') # returns ('a', 'b', 'c')
703             passthrough('a', 'b', undef) # returns ('a', 'b', undef)
704             passthrough('a', 'b') # returns ('a', 'b', [])
705             passthrough('a', undef) # returns ('a', 42, [])
706             passthrough('a', undef, 'c') # returns ('a', 42, 'c')
707             passthrough('a') # returns ('a', 42, [])
708             passthrough() # error: Too few arguments
709              
710             Default arguments specified with C<=> are evaluated whenever a corresponding
711             real argument is not passed in by the caller. C<undef> counts as a real
712             argument; you can't use the default value for parameter I<N> and still pass a
713             value for parameter I<N+1>.
714              
715             Default arguments specified with C<//=> are evaluated whenever a corresponding
716             real argument is not passed in or when that argument is C<undef>. That is,
717             passing in C<undef> to a C<//=> parameter lets you explicitly request the
718             default.
719              
720             Both C<=> and C<//=> default arguments can be mixed freely in the same
721             parameter list.
722              
723             C<$z = []> means each call that doesn't pass a third argument gets a new array
724             reference (they're not shared between calls).
725              
726             Default arguments are evaluated as part of the function body, allowing for
727             silliness such as:
728              
729             fun weird($name = return "nope") {
730             print "Hello, $name!\n";
731             return $name;
732             }
733              
734             weird("Larry"); # prints "Hello, Larry!" and returns "Larry"
735             weird(); # returns "nope" immediately; function body doesn't run
736              
737             Preceding parameters are in scope for default arguments:
738              
739             fun dynamic_default($x, $y = length $x) {
740             return "$x/$y";
741             }
742              
743             dynamic_default("hello", 0) # returns "hello/0"
744             dynamic_default("hello") # returns "hello/5"
745             dynamic_default("abc") # returns "abc/3"
746              
747             If you just want to make a parameter optional without giving it a special
748             value, write C<$param = undef>. There is a special shortcut syntax for
749             this case: C<$param = undef> can also be written C<$param => (with no following
750             expression).
751              
752             fun foo($x = undef, $y = undef, $z = undef) {
753             # three arguments, all optional
754             ...
755             }
756              
757             fun foo($x=, $y=, $z=) {
758             # shorter syntax, same meaning
759             ...
760             }
761              
762             Optional parameters must come at the end. It is not possible to have a required
763             parameter after an optional one.
764              
765             =head3 Slurpy/rest parameters
766              
767             The last parameter of a function or method can be an array. This lets you slurp
768             up any number of arguments the caller passes (0 or more).
769              
770             fun scale($factor, @values) {
771             return map { $_ * $factor } @values;
772             }
773              
774             scale(10, 1 .. 4) # returns (10, 20, 30, 40)
775             scale(10) # returns ()
776              
777             You can also use a hash, but then the number of arguments has to be even.
778              
779             =head3 Named parameters
780              
781             As soon as your functions take more than three arguments, it gets harder to
782             keep track of which argument means what:
783              
784             foo($handle, $w, $h * 2 + 15, 1, 24, 'icon');
785             # what do these arguments mean?
786              
787             C<Function::Parameters> offers an alternative for these kinds of situations in
788             the form of named parameters. Unlike the parameters described previously, which
789             are identified by position, these parameters are identified by name:
790              
791             fun create_point(:$x, :$y, :$color) {
792             ...
793             }
794              
795             # Case 1
796             create_point(
797             x => 50,
798             y => 50,
799             color => 0xff_00_00,
800             );
801              
802             To create a named parameter, put a C<:> (colon) in front of it in the parameter
803             list. When the function is called, the arguments have to be supplied in the
804             form of a hash initializer (a list of alternating keys/values). As with a hash,
805             the order of key/value pairs doesn't matter (except in the case of duplicate
806             keys, where the last occurrence wins):
807              
808             # Case 2
809             create_point(
810             color => 0xff_00_00,
811             x => 50,
812             y => 50,
813             );
814              
815             # Case 3
816             create_point(
817             x => 200,
818             color => 0x12_34_56,
819             color => 0xff_00_00,
820             x => 50,
821             y => 50,
822             );
823              
824             Case 1, Case 2, and Case 3 all mean the same thing.
825              
826             As with positional parameters, you can make named parameters optional by
827             supplying a L<default argument|/Default arguments> with C<=> or C<//=>:
828              
829             # use default if no 'color' key exists in the argument list
830             fun create_point(:$x, :$y, :$color = 0x00_00_00) {
831             ...
832             }
833              
834             create_point(x => 0, y => 64) # color => 0x00_00_00 is implicit
835              
836             Or:
837              
838             # use default if 'color' value is not defined
839             fun create_point(:$x, :$y, :$color //= 0x00_00_00) {
840             ...
841             }
842              
843             create_point(x => 0, y => 64, color => undef) # color => 0x00_00_00 is implicit
844              
845             If you want to accept any key/value pairs, you can add a
846             L<rest parameter|/Slurpy/rest parameters> (hashes are particularly useful):
847              
848             fun accept_all_keys(:$name, :$age, %rest) {
849             ...
850             }
851              
852             accept_all_keys(
853             age => 42,
854             gender => 2,
855             name => "Jamie",
856             marbles => [],
857             );
858             # $name = "Jamie";
859             # $age = 42;
860             # %rest = (
861             # gender => 2,
862             # marbles => [],
863             # );
864              
865             You can combine positional and named parameters, but all positional parameters
866             have to come first:
867              
868             method output(
869             $data,
870             :$handle = $self->output_handle,
871             :$separator = $self->separator,
872             :$quote_fields = 0,
873             ) {
874             ...
875             }
876              
877             $obj->output(["greetings", "from", "space"]);
878             $obj->output(
879             ["a", "random", "example"],
880             quote_fields => 1,
881             separator => ";",
882             );
883              
884             =head3 Unnamed parameters
885              
886             If your function doesn't use a particular parameter at all, you can omit its
887             name and just write a sigil in the parameter list:
888              
889             register_callback('click', fun ($target, $) {
890             ...
891             });
892              
893             Here we're calling a hypothetical C<register_callback> function that registers
894             our coderef to be called in response to a C<click> event. It will pass two
895             arguments to the click handler, but the coderef only cares about the first one
896             (C<$target>). The second parameter doesn't even get a name (just a sigil,
897             C<$>). This marks it as unused.
898              
899             This case typically occurs when your functions have to conform to an externally
900             imposed interface, e.g. because they're called by someone else. It can happen
901             with callbacks or methods that don't need all of the arguments they get.
902              
903             You can use unnamed L<slurpy parameters|/Slurpy/rest parameters> to accept and
904             ignore all following arguments. In particular, C<fun foo(@)> is a lot like
905             C<sub foo> in that it accepts and ignores any number of arguments (and just
906             leaves them in C<@_>).
907              
908             =head3 Type constraints
909              
910             It is possible to automatically check the types of arguments passed to your
911             function. There are two ways to do this.
912              
913             =over
914              
915             =item 1.
916              
917             use Types::Standard qw(Str Int ArrayRef);
918              
919             fun foo(Str $label, ArrayRef[Int] $counts) {
920             ...
921             }
922              
923             In this variant you simply put the name of a type in front of a parameter. The
924             way this works is that C<Function::Parameters> parses the type using a
925             restrictive set of rules:
926              
927             =over
928              
929             =item *
930              
931             A I<type> is a simplified expression that only uses C<(>, C<)>, C<|>, C<&>,
932             C</>, C<~>, and simple types, except the first character cannot be C<(> (see
933             syntax #2 below). The relative operator precedence is as in Perl; see
934             L<perlop>.
935              
936             =item *
937              
938             C<(> C<)> can be used for grouping, but have no effect otherwise.
939              
940             =item *
941              
942             C<~> (highest precedence) is a unary prefix operator meant for complementary
943             types (as provided by L<Type::Tiny>).
944              
945             =item *
946              
947             C</> is a binary infix operator meant for alternative types (as provided by
948             L<Type::Tiny>).
949              
950             =item *
951              
952             C<&> is a binary infix operator meant for intersection types (as provided by
953             L<Type::Tiny>).
954              
955             =item *
956              
957             C<|> (lowest precedence) is a binary infix operator meant for union types (as
958             provided by basically everyone doing type constraints, including L<Moose> (see
959             L<Moose::Manual::Types/TYPE UNIONS> and L<MooseX::Types>) and L<Type::Tiny>).
960              
961             =item *
962              
963             A I<simple type> is an identifier, optionally followed by a list of one or more
964             types, separated by C<,> (comma), enclosed in C<[> C<]> (square brackets).
965              
966             =back
967              
968             C<Function::Parameters> then resolves simple types by looking for functions of
969             the same name in your current package. A type specification like
970             C<Str | ArrayRef[Int]> ends up running the Perl code
971             C<Str() | ArrayRef([Int()])> (at compile time, while the function definition is
972             being processed). In other words, C<Function::Parameters> doesn't support any
973             types natively; it simply uses whatever is in scope.
974              
975             You don't have to define these type constraints yourself; you can import them
976             from a type library such as L<Types::Standard> or L<MooseX::Types::Moose>.
977              
978             The only requirement is that the returned value (here referred to as C<$tc>,
979             for "type constraint") is an object that provides C<< $tc->check($value) >>
980             and C<< $tc->get_message($value) >> methods. C<check> is called to determine
981             whether a particular value is valid; it should return a true or false value.
982             C<get_message> is called on values that fail the C<check> test; it should
983             return a string that describes the error.
984              
985             Type constraints can optionally support two additional features:
986              
987             =over
988              
989             =item *
990              
991             Coercion. If the C<< $tc->has_coercion >> method exists and returns a true
992             value, every incoming argument is automatically transformed by
993             C<< $value = $tc->coerce($value) >> before being type-checked.
994              
995             =item *
996              
997             Inlining. If the C<< $tc->can_be_inlined >> method exists and returns a true
998             value, the call to C<< $tc->check($value) >> is automatically replaced by the
999             code returned (in string form) from C<< $tc->inline_check('$value') >>. (For
1000             compatibility with L<Moose>, if C<$tc> has no C<inline_check> method,
1001             C<< $tc->_inline_check('$value') >> is used instead.)
1002              
1003             =back
1004              
1005             =item 2.
1006              
1007             my ($my_type, $some_other_type);
1008             BEGIN {
1009             $my_type = Some::Constraint::Class->new;
1010             $some_other_type = Some::Other::Class->new;
1011             }
1012              
1013             fun foo(($my_type) $label, ($some_other_type) $counts) {
1014             ...
1015             }
1016              
1017             In this variant you enclose an arbitrary Perl expression in C<(> C<)>
1018             (parentheses) and put it in front of a parameter. This expression is evaluated
1019             at compile time and must return a type constraint object as described above.
1020             (If you use variables here, make sure they're defined at compile time.)
1021              
1022             =back
1023              
1024             =head3 Method modifiers
1025              
1026             C<Function::Parameters> has support for method modifiers as provided by L<Moo>
1027             or L<Moose>. They're not exported by default, so you have to say
1028              
1029             use Function::Parameters qw(:modifiers);
1030              
1031             to get them. This line gives you method modifiers I<only>; C<fun> and C<method>
1032             are not defined. To get both the standard keywords and method modifiers, you
1033             can either write two C<use> lines:
1034              
1035             use Function::Parameters;
1036             use Function::Parameters qw(:modifiers);
1037              
1038             or explicitly list the keywords you want:
1039              
1040             use Function::Parameters qw(fun method :modifiers);
1041              
1042             or add the C<:std> import tag (which gives you the default import behavior):
1043              
1044             use Function::Parameters qw(:std :modifiers);
1045              
1046             This defines the following additional keywords: C<before>, C<after>, C<around>,
1047             C<augment>, C<override>. These work mostly like C<method>, but they don't
1048             install the function into your package themselves. Instead they invoke whatever
1049             C<before>, C<after>, C<around>, C<augment>, or C<override> function
1050             (respectively) is in scope to do the job.
1051              
1052             before foo($x, $y, $z) {
1053             ...
1054             }
1055              
1056             works like
1057              
1058             &before('foo', method ($x, $y, $z) {
1059             ...
1060             });
1061              
1062             C<after>, C<augment>, and C<override> work the same way.
1063              
1064             C<around> is slightly different: Instead of shifting off the first element of
1065             C<@_> into C<$self> (as C<method> does), it shifts off I<two> values:
1066              
1067             around foo($x, $y, $z) {
1068             ...
1069             }
1070              
1071             works like
1072              
1073             &around('foo', sub :method {
1074             my $orig = shift;
1075             my $self = shift;
1076             my ($x, $y, $z) = @_;
1077             ...
1078             });
1079              
1080             (except you also get the usual C<Function::Parameters> features such as
1081             checking the number of arguments, etc).
1082              
1083             C<$orig> and C<$self> both count as invocants and you can override their names
1084             like this:
1085              
1086             around foo($original, $object: $x, $y, $z) {
1087             # $original is a reference to the wrapped method;
1088             # $object is the object we're being called on
1089             ...
1090             }
1091              
1092             If you use C<:> to pick your own invocant names in the parameter list of
1093             C<around>, you must specify exactly two variables.
1094              
1095             These modifiers also differ from C<fun> and C<method> (and C<sub>) in that they
1096             require a function name (there are no anonymous method modifiers) and they
1097             take effect at runtime, not compile time. When you say C<fun foo() {}>, the
1098             C<foo> function is defined right after the closing C<}> of the function body is
1099             parsed. But with e.g. C<before foo() {}>, the declaration becomes a normal
1100             function call (to the C<before> function in the current package), which is
1101             performed at runtime.
1102              
1103             =head3 Prototypes and attributes
1104              
1105             You can specify attributes (see L<perlsub/Subroutine Attributes>) for your
1106             functions using the usual syntax:
1107              
1108             fun deref($x) :lvalue {
1109             ${$x}
1110             }
1111              
1112             my $silly;
1113             deref(\$silly) = 42;
1114              
1115             To specify a prototype (see L<perlsub/Prototypes>), use the C<prototype>
1116             attribute:
1117              
1118             fun mypush($aref, @values) :prototype(\@@) {
1119             push @{$aref}, @values;
1120             }
1121              
1122             =head3 Introspection
1123              
1124             The function C<Function::Parameters::info> lets you introspect parameter lists
1125             at runtime. It is not exported, so you have to call it by its full name.
1126              
1127             It takes a reference to a function and returns either C<undef> (if it knows
1128             nothing about the function) or an object that describes the parameter list of
1129             the given function. See L<Function::Parameters::Info> for details.
1130              
1131             =head2 Customizing and extending
1132              
1133             =head3 Wrapping C<Function::Parameters>
1134              
1135             Due to its nature as a lexical pragma, importing from C<Function::Parameters>
1136             always affects the scope that is currently being compiled. If you want to write
1137             a wrapper module that enables C<Function::Parameters> automatically, just call
1138             C<< Function::Parameters->import >> from your own C<import> method (and
1139             C<< Function::Parameters->unimport >> from your C<unimport>, as required).
1140              
1141             =head3 Gory details of importing
1142              
1143             At the lowest layer C<use Function::Parameters ...> takes a list of one or more
1144             hash references. Each key is a keyword to be defined as specified by the
1145             corresponding value, which must be another hash reference containing
1146             configuration options.
1147              
1148             use Function::Parameters
1149             {
1150             keyword_1 => { ... },
1151             keyword_2 => { ... },
1152             },
1153             {
1154             keyword_3 => { ... },
1155             };
1156              
1157             If you don't specify a particular option, its default value is used. The
1158             available configuration options are:
1159              
1160             =over
1161              
1162             =item C<attributes>
1163              
1164             (string) The attributes that every function declared with this
1165             keyword should have (in the form of source code, with a leading C<:>).
1166              
1167             Default: nothing
1168              
1169             =item C<check_argument_count>
1170              
1171             (boolean) Whether functions declared with this keyword should check how many
1172             arguments they are called with. If false, omitting a required argument sets it
1173             to C<undef> and excess arguments are silently ignored. If true, an exception is
1174             thrown if too few or too many arguments are passed.
1175              
1176             Default: C<1>
1177              
1178             =item C<check_argument_types>
1179              
1180             (boolean) Whether functions declared with this keyword should check the types
1181             of the arguments they are called with. If false,
1182             L<type constraints|/Type constraints> are parsed but silently ignored. If true,
1183             an exception is thrown if an argument fails a type check.
1184              
1185             Default: C<1>
1186              
1187             =item C<default_arguments>
1188              
1189             (boolean) Whether functions declared with this keyword should allow default
1190             arguments in their parameter list. If false,
1191             L<default arguments|/Default arguments> are a compile-time error.
1192              
1193             Default: C<1>
1194              
1195             =item C<install_sub>
1196              
1197             (sub name or reference) If this is set, named functions declared with this
1198             keyword are not entered into the symbol table directly. Instead the subroutine
1199             specified here (by name or reference) is called with two arguments, the name of
1200             the function being declared and a reference to its body.
1201              
1202             Default: nothing
1203              
1204             =item C<invocant>
1205              
1206             (boolean) Whether functions declared with this keyword should allow explicitly
1207             specifying invocant(s) at the beginning of the parameter list (as in
1208             C<($invocant: ...)> or C<($invocant1, $invocant2, $invocant3: ...)>).
1209              
1210             Default: 0
1211              
1212             =item C<name>
1213              
1214             (string) There are three possible values for this option. C<'required'> means
1215             functions declared with this keyword must have a name. C<'prohibited'> means
1216             specifying a name is not allowed. C<'optional'> means this keyword can be used
1217             for both named and anonymous functions.
1218              
1219             Default: C<'optional'>
1220              
1221             =item C<named_parameters>
1222              
1223             (boolean) Whether functions declared with this keyword should allow named
1224             parameters. If false, L<named parameters|/Named parameters> are a compile-time
1225             error.
1226              
1227             Default: C<1>
1228              
1229             =item C<reify_type>
1230              
1231             (coderef or C<'auto'> or C<'moose'>) The code reference used to resolve
1232             L<type constraints|/Type constraints> in functions declared with this keyword.
1233             It is called once for each type constraint that doesn't use the C<( EXPR )>
1234             syntax, with one argument, the text of the type in the parameter list (e.g.
1235             C<'ArrayRef[Int]'>). The package the function declaration is in is available
1236             through L<C<caller>|perlfunc/caller EXPR>.
1237              
1238             The only requirement is that the returned value (here referred to as C<$tc>,
1239             for "type constraint") is an object that provides C<< $tc->check($value) >>
1240             and C<< $tc->get_message($value) >> methods. C<check> is called to determine
1241             whether a particular value is valid; it should return a true or false value.
1242             C<get_message> is called on values that fail the C<check> test; it should
1243             return a string that describes the error.
1244              
1245             Type constraints can optionally support two additional features:
1246              
1247             =over
1248              
1249             =item *
1250              
1251             Coercion. If the C<< $tc->has_coercion >> method exists and returns a true
1252             value, every incoming argument is automatically transformed by
1253             C<< $value = $tc->coerce($value) >> before being type-checked.
1254              
1255             =item *
1256              
1257             Inlining. If the C<< $tc->can_be_inlined >> method exists and returns a true
1258             value, the call to C<< $tc->check($value) >> is automatically replaced by the
1259             code returned (in string form) from C<< $tc->inline_check('$value') >>. (For
1260             compatibility with L<Moose>, if C<$tc> has no C<inline_check> method,
1261             C<< $tc->_inline_check('$value') >> is used instead.)
1262              
1263             =back
1264              
1265             Instead of a code reference you can also specify one of two strings.
1266              
1267             C<'auto'> stands for a built-in type reifier that treats identifiers as
1268             subroutine names, C<[> C<]> as an array reference, C<~> as bitwise complement,
1269             C</> as division, C<&> as bitwise and, and C<|> as bitwise or. In other words,
1270             it parses and executes type constraints (mostly) as if they had been Perl
1271             source code.
1272              
1273             C<'moose'> stands for a built-in type reifier that loads
1274             L<Moose::Util::TypeConstraints> and just forwards to
1275             L<C<find_or_create_isa_type_constraint>|Moose::Util::TypeConstraints/find_or_create_isa_type_constraint($type_name)>.
1276              
1277             Default: C<'auto'>
1278              
1279             =item C<runtime>
1280              
1281             (boolean) Whether functions declared with this keyword should be installed into
1282             the symbol table at runtime. If false, named functions are defined (or their
1283             L<C<install_sub>|/C<install_sub>> is invoked if specified) immediately after
1284             their declaration is parsed (as with L<C<sub>|perlfunc/sub NAME BLOCK>). If
1285             true, function declarations become normal statements that only take effect at
1286             runtime (similar to C<*foo = sub { ... };> or
1287             C<< $install_sub->('foo', sub { ... }); >>, respectively).
1288              
1289             Default: C<0>
1290              
1291             =item C<shift>
1292              
1293             (string or arrayref) In its simplest form, this is the name of a variable that
1294             acts as the default invocant (a required leading argument that is removed from
1295             C<@_>) for all functions declared with this keyword (e.g. C<'$self'> for
1296             methods). You can also set this to an array reference of strings, which lets
1297             you specify multiple default invocants, or even to an array reference of array
1298             references of the form C<[ $name, $type ]> (where C<$name> is the variable name
1299             and C<$type> is a L<type constraint object|/Type constraints>), which lets you
1300             specify multiple default invocants with type constraints.
1301              
1302             If you define any default invocants here and also allow individual declarations
1303             to override the default (with C<< invocant => 1 >>), the number of overridden
1304             invocants must match the default. For example, C<method> has a default invocant
1305             of C<$self>, so C<method foo($x, $y: $z)> is invalid because it tries to define
1306             two invocants.
1307              
1308             Default: C<[]> (meaning no invocants)
1309              
1310             =item C<strict>
1311              
1312             (boolean) Whether functions declared with this keyword should do "strict"
1313             checks on their arguments. Currently setting this simply sets
1314             L<C<check_argument_count>|/C<check_argument_count>> to the same value with no
1315             other effects.
1316              
1317             Default: nothing
1318              
1319             =item C<types>
1320              
1321             (boolean) Whether functions declared with this keyword should allow type
1322             constraints in their parameter lists. If false, trying to use
1323             L<type constraints|/Type constraints> is a compile-time error.
1324              
1325             Default: C<1>
1326              
1327             =back
1328              
1329             You can get the same effect as C<use Function::Parameters;> by saying:
1330              
1331             use Function::Parameters {
1332             fun => {
1333             # 'fun' uses default settings only
1334             },
1335             method => {
1336             attributes => ':method',
1337             shift => '$self',
1338             invocant => 1,
1339             # the rest is defaults
1340             },
1341             };
1342              
1343             =head3 Configuration bundles
1344              
1345             Because specifying all these configuration options from scratch each time is a
1346             lot of writing, C<Function::Parameters> offers configuration bundles in the
1347             form of special strings. These strings can be used to replace a configuration
1348             hash completely or as the value of the C<defaults> pseudo-option within a
1349             configuration hash. The latter lets you use the configuration bundle behind the
1350             string to provide defaults and tweak them with your own settings.
1351              
1352             The following bundles are available:
1353              
1354             =over
1355              
1356             =item C<function_strict>
1357              
1358             Equivalent to C<{}>, i.e. all defaults.
1359              
1360             =item C<function_lax>
1361              
1362             Equivalent to:
1363              
1364             {
1365             defaults => 'function_strict',
1366             strict => 0,
1367             }
1368              
1369             i.e. just like L<C<function_strict>|/C<function_strict>> but with
1370             L<C<strict>|/C<strict>> checks turned off.
1371              
1372             =item C<function>
1373              
1374             Equivalent to C<function_strict>. This is what the default C<fun> keyword
1375             actually uses. (In version 1 of this module, C<function> was equivalent to
1376             C<function_lax>.)
1377              
1378             =item C<method_strict>
1379              
1380             Equivalent to:
1381              
1382             {
1383             defaults => 'function_strict',
1384             attributes => ':method',
1385             shift => '$self',
1386             invocant => 1,
1387             }
1388              
1389             =item C<method_lax>
1390              
1391             Equivalent to:
1392              
1393             {
1394             defaults => 'method_strict',
1395             strict => 0,
1396             }
1397              
1398             i.e. just like L<C<method_strict>|/C<method_strict>> but with
1399             L<C<strict>|/C<strict>> checks turned off.
1400              
1401             =item C<method>
1402              
1403             Equivalent to C<method_strict>. This is what the default C<method> keyword
1404             actually uses. (In version 1 of this module, C<method> was equivalent to
1405             C<method_lax>.)
1406              
1407             =item C<classmethod_strict>
1408              
1409             Equivalent to:
1410              
1411             {
1412             defaults => 'method_strict',
1413             shift => '$class',
1414             }
1415              
1416             i.e. just like L<C<method_strict>|/C<method_strict>> but the implicit first
1417             parameter is called C<$class>, not C<$self>.
1418              
1419             =item C<classmethod_lax>
1420              
1421             Equivalent to:
1422              
1423             {
1424             defaults => 'classmethod_strict',
1425             strict => 0,
1426             }
1427              
1428             i.e. just like L<C<classmethod_strict>|/C<classmethod_strict>> but with
1429             L<C<strict>|/C<strict>> checks turned off.
1430              
1431             =item C<classmethod>
1432              
1433             Equivalent to C<classmethod_strict>. This is currently not used anywhere within
1434             C<Function::Parameters>.
1435              
1436             =item C<around>
1437              
1438             Equivalent to:
1439              
1440             {
1441             defaults => 'method',
1442             install_sub => 'around',
1443             shift => ['$orig', '$self'],
1444             runtime => 1,
1445             name => 'required',
1446             }
1447              
1448             i.e. just like L<C<method>|/C<method>> but with a custom installer
1449             (C<'around'>), two implicit first parameters, only taking effect at
1450             runtime, and a method name is required.
1451              
1452             =item C<before>
1453              
1454             Equivalent to:
1455              
1456             {
1457             defaults => 'method',
1458             install_sub => 'before',
1459             runtime => 1,
1460             name => 'required',
1461             }
1462              
1463             i.e. just like L<C<method>|/C<method>> but with a custom installer
1464             (C<'before'>), only taking effect at runtime, and a method name is required.
1465              
1466             =item C<after>
1467              
1468             Equivalent to:
1469              
1470             {
1471             defaults => 'method',
1472             install_sub => 'after',
1473             runtime => 1,
1474             name => 'required',
1475             }
1476              
1477             i.e. just like L<C<method>|/C<method>> but with a custom installer
1478             (C<'after'>), only taking effect at runtime, and a method name is required.
1479              
1480             =item C<augment>
1481              
1482             Equivalent to:
1483              
1484             {
1485             defaults => 'method',
1486             install_sub => 'augment',
1487             runtime => 1,
1488             name => 'required',
1489             }
1490              
1491             i.e. just like L<C<method>|/C<method>> but with a custom installer
1492             (C<'augment'>), only taking effect at runtime, and a method name is required.
1493              
1494             =item C<override>
1495              
1496             Equivalent to:
1497              
1498             {
1499             defaults => 'method',
1500             install_sub => 'override',
1501             runtime => 1,
1502             name => 'required',
1503             }
1504              
1505             i.e. just like L<C<method>|/C<method>> but with a custom installer
1506             (C<'override'>), only taking effect at runtime, and a method name is required.
1507              
1508             =back
1509              
1510             You can get the same effect as C<use Function::Parameters;> by saying:
1511              
1512             use Function::Parameters {
1513             fun => { defaults => 'function' },
1514             method => { defaults => 'method' },
1515             };
1516              
1517             or:
1518              
1519             use Function::Parameters {
1520             fun => 'function',
1521             method => 'method',
1522             };
1523              
1524             =head3 Import tags
1525              
1526             In addition to hash references you can also use special strings in your import
1527             list. The following import tags are available:
1528              
1529             =over
1530              
1531             =item C<'fun'>
1532              
1533             Equivalent to C<< { fun => 'function' } >>.
1534              
1535             =item C<'method'>
1536              
1537             Equivalent to C<< { method => 'method' } >>.
1538              
1539             =item C<'classmethod'>
1540              
1541             Equivalent to C<< { classmethod => 'classmethod' } >>.
1542              
1543             =item C<'before'>
1544              
1545             Equivalent to C<< { before => 'before' } >>.
1546              
1547             =item C<'after'>
1548              
1549             Equivalent to C<< { after => 'after' } >>.
1550              
1551             =item C<'around'>
1552              
1553             Equivalent to C<< { around => 'around' } >>.
1554              
1555             =item C<'augment'>
1556              
1557             Equivalent to C<< { augment => 'augment' } >>.
1558              
1559             =item C<'override'>
1560              
1561             Equivalent to C<< { override => 'override' } >>.
1562              
1563             =item C<':strict'>
1564              
1565             Equivalent to C<< { fun => 'function_strict', method => 'method_strict' } >>
1566             but that's just the default behavior anyway.
1567              
1568             =item C<':lax'>
1569              
1570             Equivalent to C<< { fun => 'function_lax', method => 'method_lax' } >>, i.e. it
1571             provides C<fun> and C<method> keywords that define functions that don't check
1572             their arguments.
1573              
1574             =item C<':std'>
1575              
1576             Equivalent to C<< 'fun', 'method' >>. This is what's used by default:
1577              
1578             use Function::Parameters;
1579              
1580             is the same as:
1581              
1582             use Function::Parameters qw(:std);
1583              
1584             =item C<':modifiers'>
1585              
1586             Equivalent to C<< 'before', 'after', 'around', 'augment', 'override' >>.
1587              
1588             =back
1589              
1590             For example, when you say
1591              
1592             use Function::Parameters qw(:modifiers);
1593              
1594             C<:modifiers> is an import tag that L<expands to|/C<':modifiers'>>
1595              
1596             use Function::Parameters qw(before after around augment override);
1597              
1598             Each of those is another import tag. Stepping through the first one:
1599              
1600             use Function::Parameters qw(before);
1601              
1602             is L<equivalent to|/C<'before'>>:
1603              
1604             use Function::Parameters { before => 'before' };
1605              
1606             This says to define the keyword C<before> according to the
1607             L<configuration bundle C<before>|/C<before>>:
1608              
1609             use Function::Parameters {
1610             before => {
1611             defaults => 'method',
1612             install_sub => 'before',
1613             runtime => 1,
1614             name => 'required',
1615             },
1616             };
1617              
1618             The C<< defaults => 'method' >> part L<pulls in|/Configuration bundles> the
1619             contents of the L<C<'method'> configuration bundle|/C<method>> (which is the
1620             same as L<C<'method_strict'>|/C<method_strict>>):
1621              
1622             use Function::Parameters {
1623             before => {
1624             defaults => 'function_strict',
1625             attributes => ':method',
1626             shift => '$self',
1627             invocant => 1,
1628             install_sub => 'before',
1629             runtime => 1,
1630             name => 'required',
1631             },
1632             };
1633              
1634             This in turn uses the
1635             L<C<'function_strict'> configuration bundle|/C<function_strict>> (which is
1636             empty because it consists of default values only):
1637              
1638             use Function::Parameters {
1639             before => {
1640             attributes => ':method',
1641             shift => '$self',
1642             invocant => 1,
1643             install_sub => 'before',
1644             runtime => 1,
1645             name => 'required',
1646             },
1647             };
1648              
1649             But if we wanted to be completely explicit, we could write this as:
1650              
1651             use Function::Parameters {
1652             before => {
1653             check_argument_count => 1,
1654             check_argument_types => 1,
1655             default_arguments => 1,
1656             named_parameters => 1,
1657             reify_type => 'auto',
1658             types => 1,
1659              
1660             attributes => ':method',
1661             shift => '$self',
1662             invocant => 1,
1663             install_sub => 'before',
1664             runtime => 1,
1665             name => 'required',
1666             },
1667             };
1668              
1669             =head2 Incompatibilites with version 1 of C<Function::Parameters>
1670              
1671             =over
1672              
1673             =item *
1674              
1675             Version 1 defaults to lax mode (no argument checks). To get the same behavior
1676             on both version 1 and version 2, explicitly write either
1677             C<use Function::Parameters qw(:strict);> (the new default) or
1678             C<use Function::Parameters qw(:lax);> (the old default). (Or write
1679             C<use Function::Parameters 2;> to trigger an error if an older version of
1680             C<Function::Parameters> is loaded.)
1681              
1682             =item *
1683              
1684             Parameter lists used to be optional. The syntax C<fun foo { ... }> would accept
1685             any number of arguments. This syntax has been removed; you now have to write
1686             C<fun foo(@) { ... }> to accept (and ignore) all arguments. On the other hand,
1687             if you meant for the function to take no arguments, write C<fun foo() { ... }>.
1688              
1689             =item *
1690              
1691             There used to be a shorthand syntax for prototypes: Using C<:(...)> (i.e. an
1692             attribute with an empty name) as the first attribute was equivalent to
1693             C<:prototype(...)>. This syntax has been removed.
1694              
1695             =item *
1696              
1697             The default type reifier used to be hardcoded to use L<Moose> (as in
1698             C<< reify_type => 'moose' >>). This has been changed to use whatever type
1699             functions are in scope (C<< reify_type => 'auto' >>).
1700              
1701             =item *
1702              
1703             Type reifiers used to see the wrong package in
1704             L<C<caller>|perlfunc/caller EXPR>. As a workaround the correct calling package
1705             used to be passed as a second argument. This problem has been fixed and the
1706             second argument has been removed. (Technically this is a core perl bug
1707             (L<GH #15597|https://github.com/Perl/perl5/issues/15597>) that
1708             wasn't so much fixed as worked around in C<Function::Parameters>.)
1709              
1710             If you want your type reifier to be compatible with both versions, you can do
1711             this:
1712              
1713             sub my_reifier {
1714             my ($type, $package) = @_;
1715             $package //= caller;
1716             ...
1717             }
1718              
1719             Or using C<Function::Parameters> itself:
1720              
1721             fun my_reifier($type, $package = caller) {
1722             ...
1723             }
1724              
1725             =back
1726              
1727             =head1 DIAGNOSTICS
1728              
1729             =over
1730              
1731             =item Function::Parameters: $^H{'Function::Parameters/config'} is not a reference; skipping: HASH(%s)
1732              
1733             Function::Parameters relies on being able to put references in C<%^H> (the
1734             lexical compilation context) and pull them out again at compile time. You may
1735             see the warning above if what used to be a reference got turned into a plain
1736             string. In this case, Function::Parameters gives up and automatically disables
1737             itself, as if by C<no Function::Parameters;>.
1738              
1739             You can disable the warning in a given scope by saying
1740             C<no warnings 'Function::Parameters'>; see L<warnings>.
1741              
1742             Currently the only case I'm aware of where this happens with core perl is
1743             embedded code blocks in regexes that are compiled at runtime (in a scope where
1744             L<C<use re 'eval'>|re> is active):
1745              
1746             use strict;
1747             use warnings;
1748             use Function::Parameters;
1749             use re 'eval';
1750              
1751             my $code = '(?{ print "embedded code\n"; })';
1752             my $regex = qr/$code/;
1753              
1754             In my opinion, this is a bug in perl:
1755             L<GH #20950|https://github.com/Perl/perl5/issues/20950>.
1756              
1757             This case used to be a hard error in versions 2.001005 and before of this
1758             module.
1759              
1760             =back
1761              
1762             =begin :README
1763              
1764             =head1 INSTALLATION
1765              
1766             To download and install this module, use your favorite CPAN client, e.g.
1767             L<C<cpan>|cpan>:
1768              
1769             =for highlighter language=sh
1770              
1771             cpan Function::Parameters
1772              
1773             Or L<C<cpanm>|cpanm>:
1774              
1775             cpanm Function::Parameters
1776              
1777             To do it manually, run the following commands (after downloading and unpacking
1778             the tarball):
1779              
1780             perl Makefile.PL
1781             make
1782             make test
1783             make install
1784              
1785             =end :README
1786              
1787             =head1 SUPPORT AND DOCUMENTATION
1788              
1789             After installing, you can find documentation for this module with the
1790             L<C<perldoc>|perldoc> command.
1791              
1792             =for highlighter language=sh
1793              
1794             perldoc Function::Parameters
1795              
1796             You can also look for information at
1797             L<https://metacpan.org/pod/Function::Parameters>.
1798              
1799             To see a list of open bugs or report a new bug, visit
1800             L<https://codeberg.org/mauke/Function-Parameters/issues>.
1801              
1802             =head1 SEE ALSO
1803              
1804             L<Function::Parameters::Info>,
1805             L<Moose>,
1806             L<Moo>,
1807             L<Type::Tiny>
1808              
1809             =head1 AUTHOR
1810              
1811             Lukas Mai, C<< <l.mai at web.de> >>
1812              
1813             =head1 COPYRIGHT & LICENSE
1814              
1815             Copyright (C) 2010-2014, 2017, 2023 Lukas Mai.
1816              
1817             This program is free software; you can redistribute it and/or modify it
1818             under the terms of either: the GNU General Public License as published
1819             by the Free Software Foundation; or the Artistic License.
1820              
1821             See L<https://dev.perl.org/licenses/> for more information.
1822              
1823             =cut