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