attributesmoosedefaults

How can I set defaults for Moose attributes?


It appears that Moose 'has' attribute requires the option 'is'. It goes gaga if I omit it.

Practically every number or string attribute I declare will be 'rw'.

How can I set a default for 'is' so I don't have to write it every time?

I checked out Moose::Meta::Attribute::Native, but if it tells me how to do that, I'm too stupid to understand it.


Solution

  • You could use the MooseX::HasDefaults module.

    It gives you two options, either set the default to 'ro' or to 'rw':

    use Moose;
    use MooseX::HasDefaults::RW;
    
    has 'thing' => (
      isa  => 'Str'
    );
    

    The above code creates an attribute 'thing' which is 'rw' by default.