X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFunction%2FParameters%2FInfo.pm;h=7e14b1231be4f6d03ec3b3339afe6b08ba715f09;hb=c1240985f4d17eac65a0219780ca0a46b97a1c02;hp=8604eeb8807b28128b5123b2c06ee6bb96a54a50;hpb=ebdc721b189a191184af4bbb1d405ab4ad33a183;p=p5sagit%2FFunction-Parameters.git diff --git a/lib/Function/Parameters/Info.pm b/lib/Function/Parameters/Info.pm index 8604eeb..7e14b12 100644 --- a/lib/Function/Parameters/Info.pm +++ b/lib/Function/Parameters/Info.pm @@ -1,12 +1,37 @@ package Function::Parameters::Info; use v5.14.0; - use warnings; -use Moo; +our $VERSION = '1.0301'; + +# If Moo isn't loaded yet but Moose is, avoid pulling in Moo and fall back to Moose +my ($Moo, $meta_make_immutable); +BEGIN { + if ($INC{'Moose.pm'} && !$INC{'Moo.pm'}) { + $Moo = 'Moose'; + $meta_make_immutable = sub { $_[0]->meta->make_immutable }; + } else { + require Moo; + $Moo = 'Moo'; + $meta_make_immutable = sub {}; + } + $Moo->import; +} + +{ + package Function::Parameters::Param; + + BEGIN { $Moo->import; } + use overload + fallback => 1, + '""' => sub { $_[0]->name }, + ; + + has $_ => (is => 'ro') for qw(name type); -our $VERSION = '0.01'; + __PACKAGE__->$meta_make_immutable; +} my @pn_ro = glob '{positional,named}_{required,optional}'; @@ -39,6 +64,8 @@ sub args_max { $r } +__PACKAGE__->$meta_make_immutable; + 'ok' __END__ @@ -75,33 +102,33 @@ L|Function::Parameters/Introspection> returns objects of this class to describe parameter lists of functions. The following methods are available: -=head2 C<< $info->invocant >> +=head2 $info->invocant Returns the name of the variable into which the first argument is -L|perlfunc/shift>ed into automatically, or C if no such thing +L|perlfunc/shift>ed automatically, or C if no such thing exists. This will usually return C<'$self'> for methods. -=head2 C<< $info->positional_required >> +=head2 $info->positional_required Returns a list of the names of the required positional parameters (or a count in scalar context). -=head2 C<< $info->positional_optional >> +=head2 $info->positional_optional Returns a list of the names of the optional positional parameters (or a count in scalar context). -=head2 C<< $info->named_required >> +=head2 $info->named_required Returns a list of the names of the required named parameters (or a count in scalar context). -=head2 C<< $info->named_optional >> +=head2 $info->named_optional Returns a list of the names of the optional named parameters (or a count in scalar context). -=head2 C<< $info->slurpy >> +=head2 $info->slurpy Returns the name of the final array or hash that gobbles up all remaining arguments, or C if no such thing exists. @@ -110,19 +137,31 @@ As a special case, functions defined without an explicit parameter list (i.e. without C<( )>) will return C<'@_'> here because they accept any number of arguments. -=head2 C<< $info->args_min >> +=head2 $info->args_min Returns the minimum number of arguments this function requires. This is computed as follows: Invocant and required positional parameters count 1 each. Optional parameters don't count. Required named parameters count 2 each (key + value). Slurpy parameters don't count either because they accept empty lists. -=head2 C<< $info->args_max >> +=head2 $info->args_max Returns the maximum number of arguments this function accepts. This is computed as follows: If there is any named or slurpy parameter, the result is C. Otherwise the result is the sum of all invocant and positional parameters. +=head2 Experimental feature: Types + +All the methods described above actually return parameter objects wherever the +description says "name". These objects have two methods: C, which +returns the name of the parameter (as a plain string), and C, which +returns the corresponding type constraint object (or undef if there was no type +specified). + +This should be invisible if you don't use types because the objects also +L stringification to call C. That is, if you treat +parameter objects like strings, they behave like strings (i.e. their names). + =head1 SEE ALSO L @@ -133,7 +172,7 @@ Lukas Mai, C<< >> =head1 COPYRIGHT & LICENSE -Copyright 2012 Lukas Mai. +Copyright 2013 Lukas Mai. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published