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:

use v5.14; package Module1 { use Moo; use Kavorka; method allVars (Int $no1, Int $no2, Str $name1, Str $name2) { say "all vars = $no1 $no2 $name1 $name2"; } } say "Let's check the method call works..."; my $obj = Module1->new; $obj->allVars(1, 2, "Foo", "Bar"); say "Now let's inspect its parameters..."; my $info = Kavorka->info(\&Module1::allVars); for my $parameter ($info->signature->positional_params) { say $parameter->name, " has type ", $parameter->type->name; }

The output is:

Let's check the method call works... all vars = 1 2 Foo Bar Now let's inspect its parameters... $no1 has type Int $no2 has type Int $name1 has type Str $name2 has type Str

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 reply to Re: List arguments of a method by tobyink
in thread List arguments of a method by perlUser9

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.