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:
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)
The first solution is to run this code after having consumed a role, or inherited a class, that provides the attribute ‘some_attr’:
A syntactically simpler solution is to add the trait in the attribute, in our class:
The ‘+’ character allows to add things to an already defined attribute, instead of trying to overwrite its definition altogether.