good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: List arguments of a methodby tobyink (Canon) |
on May 15, 2014 at 08:43 UTC ( [id://1086117]=note: print w/replies, xml ) | Need Help?? |
As others have said, no this can't be done. And certainly not via the symbol table because lexicals don't live there. PadWalker provides a function called peek_sub which will tell you the names of lexical variables used within a sub. However, not all lexical variables necessarily correspond to parameters. The sub might contain, say, a foreach my $x (@array) loop where $x is just a loop variable; nothing to do with a function parameters. A better alternative would be to write your module using one of the many CPAN extensions that provide declarative sugar for method signatures, and provide an introspection API. Here's an example using Kavorka:
The output is:
Function::Parameters would similarly work. Method::Signatures would not because although it provides very similar declarative sugar, it doesn't seem to have an introspection API. MooseX::Method::Signatures would also work, but it's incredibly slow.
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
In Section
Seekers of Perl Wisdom
|
|