X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFunction%2FParameters%2FInfo.pm;h=b18814cc0ac283b72a32850a7a02a5dc480c57a3;hb=ea89928ab2c7540613e10f76fe920afce632f854;hp=5177d4d33d07c5b8580a76443809aad505a34b02;hpb=0d87c016a41b5c79531b89496bd20f408a44c840;p=p5sagit%2FFunction-Parameters.git diff --git a/lib/Function/Parameters/Info.pm b/lib/Function/Parameters/Info.pm index 5177d4d..b18814c 100644 --- a/lib/Function/Parameters/Info.pm +++ b/lib/Function/Parameters/Info.pm @@ -4,9 +4,35 @@ use v5.14.0; use warnings; -use Moo; +our $VERSION = '0.02'; + +# 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 }, + ; -our $VERSION = '0.01'; + has $_ => (is => 'ro') for qw(name type); + + __PACKAGE__->$meta_make_immutable; +} my @pn_ro = glob '{positional,named}_{required,optional}'; @@ -39,6 +65,8 @@ sub args_max { $r } +__PACKAGE__->$meta_make_immutable; + 'ok' __END__ @@ -78,7 +106,7 @@ methods are available: =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 $info->positional_required @@ -123,6 +151,18 @@ 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 +173,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