Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Dynamically adding methods to Moo class

by McA (Priest)
on Apr 23, 2014 at 15:24 UTC ( #1083379=perlquestion: print w/ replies, xml ) Need Help??
McA has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm writing a Moo based class with several methods. I'm pretty new to Moo. While writing the methods I saw that the bodies of the methods are almost the same. Is there a recommended way to "generate" these methods with Moo? Is there a kind of method generation facility? Or do I have to tweak with the symbol table?

Thank you in advance.

Best regards
McA

Comment on Dynamically adding methods to Moo class
Re: Dynamically adding methods to Moo class
by hippo (Curate) on Apr 23, 2014 at 15:56 UTC

    That will depend on what the methods actually do. If they are simple getters/setters then yes, Moo has ways to automagically "generate" those as outlined (or at least hinted at) in the documentation.

    For less standard methods you could use the normal approach of extracting the common code into a separate subroutine (or method) and have the individual (now slimmer) methods call that internally.

    If you could provide an example of two of your almost identical methods we might be able to discern an appropriate path of action.

      Hi hippo,

      thank you for your fast reply. I really hoped that there is a possibility to use this kind of "method installation infrastructure" Moo must have for e.g. to implement the 'has'-declaration. With this declaration you get later an appropriate sub which you can call as method.

      For an example use case. Assume you have a simple class:

      package Me; use strict; use warnings; use Moo; has 'typ' => ( is => 'ro', required => 1, ); has 'other' => ( is => 'ro', required => 1, ); sub info { my $self = shift; return $self->new( 'typ' => 'INFO', 'other' => "@_", ); }

      The method 'info' is just a convenience constructor for the Moo generated constructor 'new'. If I like to do this for several values of 'typ' I would repeat myself. So I hoped to "create" / "generate" these methods more elegant.

      Regards
      McA

        Don't forget that Moo is still Perl. You can still do all the stuff you could normally do using Perl!

        for my $t (qw/ INFO BINFO WINFO SCHMINFO /) { no strict 'refs'; *{lc $t} = sub { my $class = shift; $class->new( typ => $t, other => "@_" ); }; }
        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1083379]
Approved by rnewsham
Front-paged by Bloodnok
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-12-18 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls