Damien Krotkine home

Moose trick with MooseX-Getopt and roles

18 May 2011 - Paris

On MooseX::Getopt

At work I extensively use Moose in my everyday Perl coding. I also use MooseX::Getopt to automatically handle command line arguments as attributes, thus simplifying the implementation of scripts.

By default, MooseX::Getopt consider all public attributes to be mapped on a command line argument. There are many ways to tell MooseX::Getopt to ignore a public attribute:

Act on distant attributes

The previous actions are to be performed on the attribute definition. But what about the situations where you don’t write the attribute definition yourself ? Like, for instance, if you inherits the attributes from an other class, or if you got the attributes by consuming a role ?

In this case, you’ll need to perform an action on the attribute from a distance. Here are two solutions, that were given to me by the nice folks on #moose (namely sartak and doy)

Apply the trait from a distance

The first solution is to run this code after having consumed a role, or inherited a class, that provides the attribute ‘some_attr’:

   MooseX::Getopt::Meta::Attribute::Trait::NoGetopt->apply(
       __PACKAGE__->meta->get_attribute('some_attr')
   );

Adds the trait to the attribute definition

A syntactically simpler solution is to add the trait in the attribute, in our class:

    has '+some_attr' => (traits => ['NoGetopt'])

The ‘+’ character allows to add things to an already defined attribute, instead of trying to overwrite its definition altogether.

blog comments powered by Disqus
Fork me on GitHub