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