At $work, we are using Moose a lot. So I spend a lot of time declaring attributes, and some of them are Strings that should be picked from a fixed list of values.
For instance, I need to be able to define a
task that can be only one of
'world_domination'. And use it like so:
So basically, the attribute is an
Until now, I was defining a new subtype for each
enum I needed. Something like:
It’s very verbose, painful and not readable, isn’t it ?
Moose::Util::TypeConstraints provides the
enum method, that serves exactly
our purpose. It’s basically a shortcut to create a subtype based on ‘Str’,
limited to a list of possible values. You can use it to build a named enum
subtype, or an anonymous subtype. See the usage in our case :
So that’s all, I guess this kind of feature is nothing new for seasoned Moose
developers, but it may help beginners, as there is no mention of