X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFunction%2FParameters.pm;h=31845e8f468cdb0480459bfc8e02519b7d8ca271;hb=af04c6f14fb39ede4662be90662ce829c1cc49d2;hp=f9434da7da14c63e513d4122227cd8a69562a05b;hpb=355a0edef48bb7b9b432cbc08c653802d2331789;p=p5sagit%2FFunction-Parameters.git diff --git a/lib/Function/Parameters.pm b/lib/Function/Parameters.pm index f9434da..31845e8 100644 --- a/lib/Function/Parameters.pm +++ b/lib/Function/Parameters.pm @@ -2,14 +2,13 @@ package Function::Parameters; use v5.14.0; -use strict; use warnings; use Carp qw(confess); use XSLoader; BEGIN { - our $VERSION = 0.10_03; + our $VERSION = '1.00'; XSLoader::load; } @@ -156,6 +155,50 @@ sub unimport { } +our %metadata; + +sub _register_info { + my ( + $key, + $declarator, + $invocant, + $positional_required, + $positional_optional, + $named_required, + $named_optional, + $slurpy, + ) = @_; + + my $blob = pack '(Z*)*', + $declarator, + $invocant // '', + join(' ', @$positional_required), + join(' ', @$positional_optional), + join(' ', @$named_required), + join(' ', @$named_optional), + $slurpy // '', + ; + + $metadata{$key} = $blob; +} + +sub info { + my ($func) = @_; + my $key = _cv_root $func or return undef; + my $blob = $metadata{$key} or return undef; + my @info = unpack '(Z*)*', $blob; + require Function::Parameters::Info; + Function::Parameters::Info->new( + keyword => $info[0], + invocant => $info[1] || undef, + _positional_required => [split ' ', $info[2]], + _positional_optional => [split ' ', $info[3]], + _named_required => [split ' ', $info[4]], + _named_optional => [split ' ', $info[5]], + slurpy => $info[6] || undef, + ) +} + 'ok' __END__ @@ -570,6 +613,19 @@ C<< use Function::Parameters { fun => 'function', method => 'method' } >>. C is equivalent to C<< use Function::Parameters { fun => 'function_strict', method => 'method_strict' } >>. +=head2 Introspection + +You can ask a function at runtime what parameters it has. This functionality is +available through the function C (which is not +exported, so you have to call it by its full name). It takes a reference to a +function, and returns either C (if it knows nothing about the function) +or a L object describing the parameter list. + +Note: This feature is implemented using L, so you'll need to have L +installed if you want to call C. + +See L for examples. + =head2 Wrapping C If you want to write a wrapper around C, you only have to @@ -600,6 +656,10 @@ generated code corresponds to: # ... turns into ... sub bar :method { my $self = shift; my ($x, $y, @z) = @_; sub bar; ... } +=head1 SEE ALSO + +L + =head1 AUTHOR Lukas Mai, C<< >>