Skip Menu |

This queue is for tickets about the Function-Parameters CPAN distribution.

Report information
The Basics
Id: 80550
Status: resolved
Priority: 0/
Queue: Function-Parameters

People
Owner: Nobody in particular
Requestors: perl [...] toby.ink
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 1.0101



Subject: Introspection API
It would be nice if Function::Parameters provided an introspection API of some kind. Given a function name or coderef for a function defined via Function::Parameters I'd like to be able to query the its list of parameters - which are optional, which are required, which are named and which are positional. Also whether the function was declared as a method or not.
I agree that this would be a good thing to have in principle. I'm less sure about what the API should actually look like. For asking about the parameter list, there would probably be a function in Function::Parameters:: that returns an object with methods that return information about the parameters ... but in what form? Need to design an interface here. Suggestions? Next: How to actually implement this? Storing metadata during parsing/code generation is probably not hard, but how does the lookup work? Closures return a new subroutine each time they're evaluated at runtime, so the set of coderefs is not static. There's probably an internal associated code pointer that remains the same (CV?). Need to investigate perl internals. "Declared as a method" is a fuzzy concept. Function::Parameters has 3 orthogonal options that can be switched on/off for each keyword: 1) 'shift' - is $_[0] automatically shifted off and assigned to a variable (typically $self)? 2) 'invocant' - can individual function declarations override autoshifting (whether on or off) with an '$invocant:' parameter? 3) 'attributes' - what are the default attributes for subroutines declared with this keyword? The default method declarator includes ':method' here. #3 is the easiest to detect because you can already check whether attributes::get(\&foo) includes 'method'. In other words, if you don't use custom keywords, you can already detect methods declared with Function::Parameters right now. #1 and #2 would probably be covered by the proposed "parameter list" object - you'd check whether Function::Parameters::meta(\&foo)->invocant returns true (or something like that).
The developer release 1.00_02 (just uploaded) includes such an API. Thoughts?
For completeness, it would be nice if the Function::Parameters::Info object had the following additional methods, unless they'd be a pain to implement... * name - the name of the sub, or undef * body - the coderef * attributes - the attributes declared if any. Not sure what level of parsing I'd expect Function::Parameters::Info to have done. The minimal probably. * prototype - the prototype for the function. Yes, Perl has a built-in for this... but for completeness. :-) A "keyword" method telling you what keyword was used to declare the sub also seems obvious, but actually I think that should be deliberately omitted. It's an implementation detail. If people want to use different keywords to classify different types of subs, they can give each keyword an attribute.
Subject: Re: [rt.cpan.org #80550] Introspection API
Date: Fri, 16 Nov 2012 13:57:15 +0100
To: bug-Function-Parameters [...] rt.cpan.org
From: Lukas Mai <l.mai [...] web.de>
On 15.11.2012 17:15, Toby Inkster via RT wrote: Show quoted text
> * name - the name of the sub, or undef
Can be done with Sub::Identify. Show quoted text
> * body - the coderef
I don't think this should be in there, for the following reasons: - the Info object is looked up by coderef, so you already have that information - Potential memory leak if the metadata hash keeps all subs alive - the Info => coderef mapping isn't unique (if you have multiple closures built from the same physical code, they share their Info objects) Show quoted text
> * attributes - the attributes declared if any. Not sure what level of > parsing I'd expect Function::Parameters::Info to have done. The minimal > probably.
Can be done with attributes::get. Show quoted text
> * prototype - the prototype for the function. Yes, Perl has a built-in > for this... but for completeness. :-)
Can be done with prototype. Show quoted text
> A "keyword" method telling you what keyword was used to declare the sub > also seems obvious, but actually I think that should be deliberately > omitted. It's an implementation detail. If people want to use different > keywords to classify different types of subs, they can give each > keyword an attribute.
Already implemented. :-) It's just not documented for the reasons you've given, but I thought it might be useful for debugging. The way I see it, you can implement most of this functionality by wrapping Info in your own class and delegating to it.