MooX::Const - Syntactic sugar for constant and write-once Moo attributes


version v0.5.1


  use Moo;
  use MooX::Const;

  use Types::Standard -types;

  has thing => (
    is  => 'const',
    isa => ArrayRef[HashRef],


This is syntactic sugar for using Types::Const with Moo. The SYNOPSIS above is equivalent to:

  use Types::Const -types;

  has thing => (
    is     => 'ro',
    isa    => Const[ArrayRef[HashRef]],
    coerce => 1,

It modifies the has function to support "const" attributes. These are read-only ("ro") attributes for references, where the underlying data structure has been set as read-only.

This will return an error if there is no "isa", the "isa" is not a Type::Tiny type, if it is not a reference, or if it is blessed object.

Simple value types such as Int or Str are silently converted to read-only attributes.

As of v0.5.0, it also supports write-once ("once") attributes for references:

  has setting => (
    is  => 'once',
    isa => HashRef,

This allows you to set the attribute once. The value is coerced into a constant, and cannot be changed again.

Note that "wo" is a deprecated synonym for "once". It will be removed in the future, since "wo" is used for "write-only" in some Moose-like extensions.

As of v0.4.0, this now supports the strict setting:

  has thing => (
    is     => 'const',
    isa    => ArrayRef[HashRef],
    strict => 0,

When this is set to a false value, then the read-only constraint will only be applied when running in strict mode, see Devel::StrictMode.

If omitted, strict is assumed to be true.


Accessing non-existent keys for hash references will throw an error. This is a feature, not a bug, of read-only hash references, and it can be used to catch mistakes in code that refer to non-existent keys.

Unfortunately, this behaviour is not replicated with array references.

See Types::Const for other known issues related to the Const type.

Using with Moose and Mouse

This module appears to work with Moose, and there is now a small test suite.

It does not work with Mouse. Pull requests are welcome.

Write-Once Attributes

Class::Accessor antlers/moose-like mode uses "wo" for write-only attributes, not write-once attributes.

As of v0.5.0, you should be using "once" instead of "wo".









The development version is on github at and may be cloned from git://


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


Robert Rothenberg <>

This module was inspired by suggestions from Kang-min Liu 劉康民 <> in a blog post.


Kang-min Liu 劉康民 <>


This software is Copyright (c) 2018-2021 by Robert Rothenberg.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)