version 1.0104
[p5sagit/Function-Parameters.git] / lib / Function / Parameters.pm
CommitLineData
7a63380c 1package Function::Parameters;
2
7dd35535 3use v5.14.0;
7a63380c 4use warnings;
5
63915d26 6use Carp qw(confess);
7
db81d362 8use XSLoader;
9BEGIN {
d72d56ce 10 our $VERSION = '1.0104';
db81d362 11 XSLoader::load;
7a63380c 12}
13
2d5cf47a 14sub _assert_valid_identifier {
15 my ($name, $with_dollar) = @_;
16 my $bonus = $with_dollar ? '\$' : '';
17 $name =~ /^${bonus}[^\W\d]\w*\z/
18 or confess qq{"$name" doesn't look like a valid identifier};
19}
20
b72eb6ee 21sub _assert_valid_attributes {
22 my ($attrs) = @_;
4b76be72 23 $attrs =~ m{
24 ^ \s*+
25 : \s*+
26 (?&ident) (?! [^\s:(] ) (?&param)?+ \s*+
27 (?:
28 (?: : \s*+ )?
29 (?&ident) (?! [^\s:(] ) (?&param)?+ \s*+
30 )*+
31 \z
32
33 (?(DEFINE)
34 (?<ident>
35 [^\W\d]
36 \w*+
37 )
38 (?<param>
39 \(
40 [^()\\]*+
41 (?:
42 (?:
43 \\ .
44 |
45 (?&param)
46 )
47 [^()\\]*+
48 )*+
49 \)
50 )
51 )
52 }sx or confess qq{"$attrs" doesn't look like valid attributes};
b72eb6ee 53}
54
125c067e 55my @bare_arms = qw(function method);
2d5cf47a 56my %type_map = (
11c807bc 57 function => {
58 name => 'optional',
59 default_arguments => 1,
63915d26 60 check_argument_count => 0,
11c807bc 61 named_parameters => 1,
62 types => 1,
63915d26 63 },
11c807bc 64 method => {
65 name => 'optional',
66 default_arguments => 1,
63915d26 67 check_argument_count => 0,
11c807bc 68 named_parameters => 1,
69 types => 1,
70 attrs => ':method',
71 shift => '$self',
72 invocant => 1,
7947f7ce 73 },
11c807bc 74 classmethod => {
75 name => 'optional',
76 default_arguments => 1,
63915d26 77 check_argument_count => 0,
11c807bc 78 named_parameters => 1,
79 types => 1,
80 attributes => ':method',
81 shift => '$class',
82 invocant => 1,
a23979e1 83 },
2d5cf47a 84);
7817d698 85for my $k (keys %type_map) {
86 $type_map{$k . '_strict'} = {
87 %{$type_map{$k}},
88 check_argument_count => 1,
89 };
90}
c9a39f6b 91
db81d362 92sub import {
93 my $class = shift;
7a63380c 94
fcaf7811 95 if (!@_) {
96 @_ = {
97 fun => 'function',
98 method => 'method',
99 };
100 }
101 if (@_ == 1 && $_[0] eq ':strict') {
102 @_ = {
103 fun => 'function_strict',
104 method => 'method_strict',
105 };
106 }
125c067e 107 if (@_ == 1 && ref($_[0]) eq 'HASH') {
fcaf7811 108 @_ = map [$_, $_[0]{$_}], keys %{$_[0]};
125c067e 109 }
7a63380c 110
125c067e 111 my %spec;
112
113 my $bare = 0;
114 for my $proto (@_) {
115 my $item = ref $proto
116 ? $proto
117 : [$proto, $bare_arms[$bare++] || confess(qq{Don't know what to do with "$proto"})]
118 ;
ae6e00b5 119 my ($name, $proto_type) = @$item;
2d5cf47a 120 _assert_valid_identifier $name;
121
ae6e00b5 122 unless (ref $proto_type) {
123 # use '||' instead of 'or' to preserve $proto_type in the error message
124 $proto_type = $type_map{$proto_type}
125 || confess qq["$proto_type" doesn't look like a valid type (one of ${\join ', ', sort keys %type_map})];
2d5cf47a 126 }
b72eb6ee 127
ae6e00b5 128 my %type = %$proto_type;
129 my %clean;
10acc8b1 130
ae6e00b5 131 $clean{name} = delete $type{name} || 'optional';
132 $clean{name} =~ /^(?:optional|required|prohibited)\z/
133 or confess qq["$clean{name}" doesn't look like a valid name attribute (one of optional, required, prohibited)];
10acc8b1 134
ae6e00b5 135 $clean{shift} = delete $type{shift} || '';
10acc8b1 136 _assert_valid_identifier $clean{shift}, 1 if $clean{shift};
137
698e861c 138 $clean{attrs} = join ' ', map delete $type{$_} || (), qw(attributes attrs);
10acc8b1 139 _assert_valid_attributes $clean{attrs} if $clean{attrs};
125c067e 140
59f51b8b 141 $clean{default_arguments} =
142 exists $type{default_arguments}
143 ? !!delete $type{default_arguments}
144 : 1
145 ;
63915d26 146 $clean{check_argument_count} = !!delete $type{check_argument_count};
d8e5d540 147 $clean{invocant} = !!delete $type{invocant};
e158cf8f 148 $clean{named_parameters} = !!delete $type{named_parameters};
51a483f8 149 $clean{types} = !!delete $type{types};
63915d26 150
ae6e00b5 151 %type and confess "Invalid keyword property: @{[keys %type]}";
152
153 $spec{$name} = \%clean;
125c067e 154 }
155
db81d362 156 for my $kw (keys %spec) {
157 my $type = $spec{$kw};
158
63915d26 159 my $flags =
11c807bc 160 $type->{name} eq 'prohibited' ? FLAG_ANON_OK :
161 $type->{name} eq 'required' ? FLAG_NAME_OK :
162 FLAG_ANON_OK | FLAG_NAME_OK
63915d26 163 ;
11c807bc 164 $flags |= FLAG_DEFAULT_ARGS if $type->{default_arguments};
51a483f8 165 $flags |= FLAG_CHECK_NARGS | FLAG_CHECK_TARGS if $type->{check_argument_count};
11c807bc 166 $flags |= FLAG_INVOCANT if $type->{invocant};
167 $flags |= FLAG_NAMED_PARAMS if $type->{named_parameters};
168 $flags |= FLAG_TYPES_OK if $type->{types};
63915d26 169 $^H{HINTK_FLAGS_ . $kw} = $flags;
ae6e00b5 170 $^H{HINTK_SHIFT_ . $kw} = $type->{shift};
10acc8b1 171 $^H{HINTK_ATTRS_ . $kw} = $type->{attrs};
db81d362 172 $^H{+HINTK_KEYWORDS} .= "$kw ";
125c067e 173 }
eeb7df5f 174}
175
db81d362 176sub unimport {
eeb7df5f 177 my $class = shift;
125c067e 178
db81d362 179 if (!@_) {
180 delete $^H{+HINTK_KEYWORDS};
125c067e 181 return;
182 }
183
db81d362 184 for my $kw (@_) {
185 $^H{+HINTK_KEYWORDS} =~ s/(?<![^ ])\Q$kw\E //g;
125c067e 186 }
187}
188
db81d362 189
53c979f0 190our %metadata;
191
192sub _register_info {
193 my (
194 $key,
195 $declarator,
196 $invocant,
51a483f8 197 $invocant_type,
53c979f0 198 $positional_required,
199 $positional_optional,
200 $named_required,
201 $named_optional,
202 $slurpy,
51a483f8 203 $slurpy_type,
53c979f0 204 ) = @_;
205
51a483f8 206 my $info = {
207 declarator => $declarator,
208 invocant => defined $invocant ? [$invocant, $invocant_type] : undef,
209 slurpy => defined $slurpy ? [$slurpy , $slurpy_type ] : undef,
210 positional_required => $positional_required,
211 positional_optional => $positional_optional,
212 named_required => $named_required,
213 named_optional => $named_optional,
214 };
215
216 $metadata{$key} = $info;
217}
218
219sub _mkparam1 {
220 my ($pair) = @_;
221 my ($v, $t) = @{$pair || []} or return undef;
222 Function::Parameters::Param->new(
223 name => $v,
224 type => $t,
225 )
226}
227
228sub _mkparams {
229 my @r;
230 while (my ($v, $t) = splice @_, 0, 2) {
231 push @r, Function::Parameters::Param->new(
232 name => $v,
233 type => $t,
234 );
235 }
236 \@r
53c979f0 237}
238
239sub info {
240 my ($func) = @_;
241 my $key = _cv_root $func or return undef;
51a483f8 242 my $info = $metadata{$key} or return undef;
53c979f0 243 require Function::Parameters::Info;
244 Function::Parameters::Info->new(
51a483f8 245 keyword => $info->{declarator},
246 invocant => _mkparam1($info->{invocant}),
247 slurpy => _mkparam1($info->{slurpy}),
248 (map +("_$_" => _mkparams @{$info->{$_}}), glob '{positional,named}_{required,optional}')
53c979f0 249 )
250}
251
125c067e 252'ok'
7a63380c 253
254__END__
255
f2541b7d 256=encoding UTF-8
257
7a63380c 258=head1 NAME
259
260Function::Parameters - subroutine definitions with parameter lists
261
262=head1 SYNOPSIS
263
81203272 264 use Function::Parameters qw(:strict);
7a63380c 265
698e861c 266 # simple function
7a63380c 267 fun foo($bar, $baz) {
268 return $bar + $baz;
269 }
270
698e861c 271 # function with prototype
d71d548b 272 fun mymap($fun, @args)
273 :(&@)
274 {
7a63380c 275 my @res;
276 for (@args) {
277 push @res, $fun->($_);
278 }
279 @res
280 }
281
282 print "$_\n" for mymap { $_ * 2 } 1 .. 4;
125c067e 283
698e861c 284 # method with implicit $self
125c067e 285 method set_name($name) {
286 $self->{name} = $name;
287 }
d8e5d540 288
289 # method with explicit invocant
290 method new($class: %init) {
291 return bless { %init }, $class;
292 }
293
81203272 294 # function with optional parameters
698e861c 295 fun search($haystack, $needle = qr/^(?!)/, $offset = 0) {
296 ...
297 }
d8e5d540 298
81203272 299 # method with named parameters
300 method resize(:$width, :$height) {
301 $self->{width} = $width;
302 $self->{height} = $height;
698e861c 303 }
8dbfd12d 304
81203272 305 $obj->resize(height => 4, width => 5);
8dbfd12d 306
81203272 307 # function with named optional parameters
308 fun search($haystack, :$needle = qr/^(?!)/, :$offset = 0) {
309 ...
310 }
8dbfd12d 311
81203272 312 my $results = search $text, offset => 200;
8dbfd12d 313
81203272 314=head1 DESCRIPTION
8dbfd12d 315
81203272 316This module extends Perl with keywords that let you define functions with
317parameter lists. It uses Perl's L<keyword plugin|perlapi/PL_keyword_plugin>
318API, so it works reliably and doesn't require a source filter.
319
320=head2 Basics
321
322The anatomy of a function (as recognized by this module):
323
324=over
8dbfd12d 325
81203272 326=item 1.
327
328The keyword introducing the function.
329
330=item 2.
331
332The function name (optional).
333
334=item 3.
335
336The parameter list (optional).
337
338=item 4.
339
340The prototype (optional).
341
342=item 5.
343
344The attribute list (optional).
345
346=item 6.
347
348The function body.
349
350=back
351
352Example:
353
354 # (1) (2) (3) (4) (5) (6)
355 fun foo ($x, $y) :($$) :lvalue { ... }
c9a39f6b 356
81203272 357 # (1) (6)
358 my $f = fun { ... };
125c067e 359
81203272 360In the following section I'm going to describe all parts in order from simplest to most complex.
7a63380c 361
81203272 362=head3 Body
7a63380c 363
81203272 364This is just a normal block of statements, as with L<C<sub>|perlsub>. No surprises here.
7a63380c 365
81203272 366=head3 Name
7a63380c 367
81203272 368If present, it specifies the name of the function being defined. As with
369L<C<sub>|perlsub>, if a name is present, the whole declaration is syntactically
370a statement and its effects are performed at compile time (i.e. at runtime you
371can call functions whose definitions only occur later in the file). If no name
372is present, the declaration is an expression that evaluates to a reference to
373the function in question. No surprises here either.
7a63380c 374
81203272 375=head3 Attributes
7a63380c 376
81203272 377Attributes are relatively unusual in Perl code, but if you want them, they work
378exactly the same as with L<C<sub>|perlsub/Subroutine-Attributes>.
c9a39f6b 379
81203272 380=head3 Prototype
698e861c 381
81203272 382As with L<C<sub>|perlsub/Prototypes>, a prototype, if present, contains hints as to how
383the compiler should parse calls to this function. This means prototypes have no
384effect if the function call is compiled before the function declaration has
385been seen by the compiler or if the function to call is only determined at
386runtime (e.g. because it's called as a method or through a reference).
698e861c 387
81203272 388With L<C<sub>|perlsub>, a prototype comes directly after the function name (if
389any). C<Function::Parameters> reserves this spot for the
390L<parameter list|/"Parameter list">. To specify a prototype, put it as the
391first attribute (e.g. C<fun foo :(&$$)>). This is syntactically unambiguous
392because normal L<attributes|/Attributes> need a name after the colon.
7a63380c 393
81203272 394=head3 Parameter list
125c067e 395
81203272 396The parameter list is a list of variables enclosed in parentheses, except it's
397actually a bit more complicated than that. A parameter list can include the
398following 6 parts, all of which are optional:
125c067e 399
81203272 400=over
125c067e 401
81203272 402=item 1. Invocant
125c067e 403
81203272 404This is a scalar variable followed by a colon (C<:>) and no comma. If an
405invocant is present in the parameter list, the first element of
406L<C<@_>|perlvar/@ARG> is automatically L<C<shift>ed|perlfunc/shift> off and
407placed in this variable. This is intended for methods:
125c067e 408
81203272 409 method new($class: %init) {
410 return bless { %init }, $class;
411 }
412
413 method throw($self:) {
414 die $self;
415 }
125c067e 416
81203272 417=item 2. Required positional parameters
fcaf7811 418
81203272 419The most common kind of parameter. This is simply a comma-separated list of
420scalars, which are filled from left to right with the arguments that the caller
421passed in:
fcaf7811 422
81203272 423 fun add($x, $y) {
424 return $x + $y;
425 }
426
427 say add(2, 3); # "5"
428
429=item 3. Optional positional parameters
430
431Parameters can be marked as optional by putting an equals sign (C<=>) and an
432expression (the "default argument") after them. If no corresponding argument is
433passed in by the caller, the default argument will be used to initialize the
434parameter:
435
436 fun scale($base, $factor = 2) {
437 return $base * $factor;
438 }
439
440 say scale(3, 5); # "15"
441 say scale(3); # "6"
442
443The default argument is I<not> cached. Every time a function is called with
444some optional arguments missing, the corresponding default arguments are
445evaluated from left to right. This makes no difference for a value like C<2>
446but it is important for expressions with side effects, such as reference
447constructors (C<[]>, C<{}>) or function calls.
448
449Default arguments see not only the surrounding lexical scope of their function
450but also any preceding parameters. This allows the creation of dynamic defaults
451based on previous arguments:
452
453 method set_name($self: $nick = $self->default_nick, $real_name = $nick) {
454 $self->{nick} = $nick;
455 $self->{real_name} = $real_name;
456 }
457
458 $obj->set_name("simplicio"); # same as: $obj->set_name("simplicio", "simplicio");
63a24d7c 459
81203272 460Because default arguments are actually evaluated as part of the function body,
461you can also do silly things like this:
462
463 fun foo($n = return "nope") {
464 "you gave me $n"
465 }
466
467 say foo(2 + 2); # "you gave me 4"
468 say foo(); # "nope"
469
470=item 4. Required named parameters
471
472By putting a colon (C<:>) in front of a parameter you can make it named
473instead of positional:
474
475 fun rectangle(:$width, :$height) {
476 ...
477 }
478
479 rectangle(width => 2, height => 5);
480 rectangle(height => 5, width => 2); # same thing!
481
482That is, the caller must specify a key name in addition to the value, but in
483exchange the order of the arguments doesn't matter anymore. As with hash
484initialization, you can specify the same key multiple times and the last
485occurrence wins:
486
487 rectangle(height => 1, width => 2, height => 2, height => 5;
488 # same as: rectangle(width => 2, height => 5);
489
490You can combine positional and named parameters as long as the positional
491parameters come first:
492
493 fun named_rectangle($name, :$width, :$height) {
494 ...
495 }
496
497 named_rectangle("Avocado", width => 0.5, height => 1.2);
498
499=item 5. Optional named parameters
500
501As with positional parameters, you can make named parameters optional by
502specifying a default argument after an equals sign (C<=>):
503
504 fun rectangle(:$width, :$height, :$color = "chartreuse") {
505 ...
506 }
507
508 rectangle(height => 10, width => 5);
509 # same as: rectangle(height => 10, width => 5, color => "chartreuse");
125c067e 510
511=cut
512
513=pod
81203272 514
515 fun get($url, :$cookie_jar = HTTP::Cookies->new(), :$referrer = $url) {
516 ...
517 }
125c067e 518
81203272 519 my $data = get "http://www.example.com/", referrer => undef; # overrides $referrer = $url
125c067e 520
81203272 521The above example shows that passing any value (even C<undef>) will override
522the default argument.
63a24d7c 523
81203272 524=item 6. Slurpy parameter
ce052c57 525
81203272 526Finally you can put an array or hash in the parameter list, which will gobble
527up the remaining arguments (if any):
ce052c57 528
81203272 529 fun foo($x, $y, @rest) { ... }
530
531 foo "a", "b"; # $x = "a", $y = "b", @rest = ()
532 foo "a", "b", "c"; # $x = "a", $y = "b", @rest = ("c")
533 foo "a", "b", "c", "d"; # $x = "a", $y = "b", @rest = ("c", "d")
ce052c57 534
81203272 535If you combine this with named parameters, the slurpy parameter will end up
536containing all unrecognized keys:
ce052c57 537
81203272 538 fun bar(:$size, @whatev) { ... }
539
540 bar weight => 20, size => 2, location => [0, -3];
541 # $size = 2, @whatev = ('weight', 20, 'location', [0, -3])
ce052c57 542
81203272 543=back
ce052c57 544
81203272 545Apart from the L<C<shift>|perlfunc/shift> performed by the L<invocant|/"1.
546Invocant">, all of the above leave L<C<@_>|perlvar/@ARG> unchanged; and if you
547don't specify a parameter list at all, L<C<@_>|perlvar/@ARG> is all you get.
d8e5d540 548
81203272 549=head3 Keyword
d8e5d540 550
81203272 551The keywords provided by C<Function::Parameters> are customizable. Since
552C<Function::Parameters> is actually a L<pragma|perlpragma>, the provided
553keywords have lexical scope. The following import variants can be used:
d8e5d540 554
81203272 555=over
273c6544 556
81203272 557=item C<use Function::Parameters ':strict'>
273c6544 558
81203272 559Provides the keywords C<fun> and C<method> (described below) and enables
560argument checks so that calling a function and omitting a required argument (or
561passing too many arguments) will throw an error.
273c6544 562
81203272 563=item C<use Function::Parameters>
273c6544 564
81203272 565Provides the keywords C<fun> and C<method> (described below) and enables
566"lax" mode: Omitting a required argument sets it to C<undef> while excess
567arguments are silently ignored.
273c6544 568
81203272 569=item C<< use Function::Parameters { KEYWORD1 => TYPE1, KEYWORD2 => TYPE2, ... } >>
698e861c 570
81203272 571Provides completely custom keywords as described by their types. A "type" is
572either a string (one of the predefined types C<function>, C<method>,
573C<classmethod>, C<function_strict>, C<method_strict>, C<classmethod_strict>) or
574a reference to a hash with the following keys:
698e861c 575
81203272 576=over
698e861c 577
81203272 578=item C<name>
698e861c 579
81203272 580Valid values: C<optional> (default), C<required> (all functions defined with
581this keyword must have a name), and C<prohibited> (functions defined with this
582keyword must be anonymous).
698e861c 583
81203272 584=item C<shift>
698e861c 585
81203272 586Valid values: strings that look like scalar variables. This lets you specify a
587default L<invocant|/"1. Invocant">, i.e. a function defined with this keyword
588that doesn't have an explicit invocant in its parameter list will automatically
589L<C<shift>|perlfunc/shift> its first argument into the variable specified here.
698e861c 590
81203272 591=item C<invocant>
698e861c 592
81203272 593Valid values: booleans. If you set this to a true value, the keyword will
594accept L<invocants|/"1. Invocant"> in parameter lists; otherwise specifying
595an invocant in a function defined with this keyword is a syntax error.
698e861c 596
81203272 597=item C<attributes>
698e861c 598
81203272 599Valid values: strings containing (source code for) attributes. This causes any
600function defined with this keyword to have the specified
601L<attributes|attributes> (in addition to any attributes specified in the
602function definition itself).
698e861c 603
81203272 604=item C<default_arguments>
698e861c 605
81203272 606Valid values: booleans. This property is on by default; use
607C<< default_arguments => 0 >> to turn it off. This controls whether optional
608parameters are allowed. If it is turned off, using C<=> in parameter lists is
609a syntax error.
698e861c 610
81203272 611=item C<check_argument_count>
698e861c 612
81203272 613Valid values: booleans. If turned on, functions defined with this keyword will
614automatically check that they have been passed all required arguments and no
615excess arguments. If this check fails, an exception will by thrown via
616L<C<Carp::croak>|Carp>.
698e861c 617
ce052c57 618=back
619
81203272 620The predefined type C<function> is equivalent to:
698e861c 621
622 {
11c807bc 623 name => 'optional',
624 invocant => 0,
625 default_arguments => 1,
698e861c 626 check_argument_count => 0,
627 }
628
81203272 629These are all default values, so C<function> is also equivalent to C<{}>.
698e861c 630
81203272 631C<method> is equivalent to:
698e861c 632
633 {
11c807bc 634 name => 'optional',
635 shift => '$self',
636 invocant => 1,
637 attributes => ':method',
638 default_arguments => 1,
81203272 639 check_argument_count => 0,
698e861c 640 }
641
7817d698 642
81203272 643C<classmethod> is equivalent to:
698e861c 644
645 {
11c807bc 646 name => 'optional',
647 shift => '$class',
648 invocant => 1,
649 attributes => ':method',
650 default_arguments => 1,
81203272 651 check_argument_count => 0,
698e861c 652 }
ce052c57 653
81203272 654C<function_strict>, C<method_strict>, and
655C<classmethod_strict> are like C<function>, C<method>, and
656C<classmethod>, respectively, but with C<< check_argument_count => 1 >>.
63a24d7c 657
81203272 658=back
63a24d7c 659
81203272 660Plain C<use Function::Parameters> is equivalent to
661C<< use Function::Parameters { fun => 'function', method => 'method' } >>.
63a24d7c 662
81203272 663C<use Function::Parameters qw(:strict)> is equivalent to
664C<< use Function::Parameters { fun => 'function_strict', method => 'method_strict' } >>.
63a24d7c 665
ebdc721b 666=head2 Introspection
667
668You can ask a function at runtime what parameters it has. This functionality is
669available through the function C<Function::Parameters::info> (which is not
670exported, so you have to call it by its full name). It takes a reference to a
671function, and returns either C<undef> (if it knows nothing about the function)
672or a L<Function::Parameters::Info> object describing the parameter list.
673
af04c6f1 674Note: This feature is implemented using L<Moo>, so you'll need to have L<Moo>
b70db867 675installed if you want to call C<Function::Parameters::info> (alternatively, if
676L<Moose> is already loaded by the time C<Function::Parameters::info> is first
677called, it will use that instead).
af04c6f1 678
ebdc721b 679See L<Function::Parameters::Info> for examples.
680
81203272 681=head2 Wrapping C<Function::Parameters>
125c067e 682
81203272 683If you want to write a wrapper around C<Function::Parameters>, you only have to
684call its C<import> method. Due to its L<pragma|perlpragma> nature it always
685affects the file that is currently being compiled.
63a24d7c 686
687 package Some::Wrapper;
688 use Function::Parameters ();
689 sub import {
690 Function::Parameters->import;
698e861c 691 # or Function::Parameters->import(@custom_import_args);
63a24d7c 692 }
eeb7df5f 693
0175ff9a 694=head2 Experimental feature: Types
695
696An experimental feature is now available: You can annotate parameters with
697L<Moose types|Moose::Manual::Types>. That is, before each parameter you can put
698a type specification consisting of identifiers (C<Foo>), unions (C<... | ...>),
699and parametric types (C<...[...]>). Example:
700
701 fun foo(Int $n, ArrayRef[String | CodeRef] $cb) { ... }
702
703If you do this, L<Moose> will be loaded automatically (if that hasn't happened
704yet). These specifications are parsed and validated using
705L<C<Moose::Util::TypeConstraints::find_or_parse_type_constraint>|Moose::Util::TypeConstraints/find_or_parse_type_constraint>.
706
707If you are in "lax" mode, nothing further happens and the types are ignored. If
708you are in "strict" mode, C<Function::Parameters> generates code to make sure
709any values passed in conform to the type (via
d54d249f 710L<< C<< $constraint->check($value) >>|Moose::Meta::TypeConstraint/$constraint->check($value) >>).
0175ff9a 711
712In addition, these type constraints are inspectable through the
713L<Function::Parameters::Info> object returned by
714L<C<Function::Parameters::info>|/Introspection>.
715
716=head2 Experimental experimental feature: Type expressions
717
718An even more experimental feature is the ability to specify arbitrary
719expressions as types. The syntax for this is like the literal types described
720above, but with an expression wrapped in parentheses (C<( EXPR )>). Example:
721
722 fun foo(('Int') $n, ($othertype) $x) { ... }
723
724Every type expression must return either a string (which is resolved as for
725literal types), or a L<type constraint object|Moose::Meta::TypeConstraint>
726(providing C<check> and C<get_message> methods).
727
728Note that these expressions are evaluated (once) at parse time (similar to
729C<BEGIN> blocks), so make sure that any variables you use are set and any
730functions you call are defined at parse time.
731
81203272 732=head2 How it works
733
734The module is actually written in L<C|perlxs> and uses
735L<C<PL_keyword_plugin>|perlapi/PL_keyword_plugin> to generate opcodes directly.
736However, you can run L<C<perl -MO=Deparse ...>|B::Deparse> on your code to see
737what happens under the hood. In the simplest case (no argument checks, possibly
738an L<invocant|/"1. Invocant">, required positional/slurpy parameters only), the
739generated code corresponds to:
740
741 fun foo($x, $y, @z) { ... }
742 # ... turns into ...
743 sub foo { my ($x, $y, @z) = @_; sub foo; ... }
744
745 method bar($x, $y, @z) { ... }
746 # ... turns into ...
747 sub bar :method { my $self = shift; my ($x, $y, @z) = @_; sub bar; ... }
748
0175ff9a 749=head1 SUPPORT AND DOCUMENTATION
750
751After installing, you can find documentation for this module with the
752perldoc command.
753
754 perldoc Function::Parameters
755
756You can also look for information at:
757
758=over
759
760=item MetaCPAN
761
762L<https://metacpan.org/module/Function%3A%3AParameters>
763
764=item RT, CPAN's request tracker
765
766L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Function-Parameters>
767
768=item AnnoCPAN, Annotated CPAN documentation
769
770L<http://annocpan.org/dist/Function-Parameters>
771
772=item CPAN Ratings
773
774L<http://cpanratings.perl.org/d/Function-Parameters>
775
776=item Search CPAN
777
778L<http://search.cpan.org/dist/Function-Parameters/>
779
780=back
781
ebdc721b 782=head1 SEE ALSO
783
784L<Function::Parameters::Info>
785
7a63380c 786=head1 AUTHOR
787
788Lukas Mai, C<< <l.mai at web.de> >>
789
790=head1 COPYRIGHT & LICENSE
791
b70db867 792Copyright 2010-2013 Lukas Mai.
7a63380c 793
794This program is free software; you can redistribute it and/or modify it
795under the terms of either: the GNU General Public License as published
796by the Free Software Foundation; or the Artistic License.
797
798See http://dev.perl.org/licenses/ for more information.
799
800=cut