Question

I'm using MooseX::Declare and methods, which uses MooseX::Method::Signatures. Let's say I have a class 'foo' with a method 'bar', and I've implemented it like:

class foo {
    method bar (Str $str, Bool :$flag = 1) {
        # ....
    }
}

I now want to write a front-end interface that asks a user what class they want to use, what method on that class they want to use, and then what options to the method they want. I can do the first two things, so let's say the user has now chosen class foo and method bar.

But how do I find out that method bar takes a string as the first argument, and a flag => bool key value pair that defaults to 1? My code needs to know this so I can then ask the user to supply these things.

Was it helpful?

Solution

First, get the method meta object:

my $method = $class->meta->find_method_by_name( $method_name );

Then, make sure it's a signaturey method:

confess "not method with a signature!"
  unless $method->isa('MooseX::Method::Signatures::Meta::Method');

Get its signature:

my $sig = $method->parsed_signature;

Then look at $sig's named_params and positional_params as detailed in the Parse::Method::Signatures::Sig docs.

To find parsed_signature, I had to look at the source to MooseX::Method::Signatures::Meta::Method… so be wary when you do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top