++ed by:

1 non-PAUSE user.

Author image Chip Salzenberg


MooseX::Locked - Moose role to automatically lock object hashes


version 1.01


By default, Moose creates objects as unlocked hashes. This lets attribute typos go unchecked. If you use MooseX::Locked in your Moose classes, any typos when accessing object attributes will be detected and throw exceptions.

    use Moose;
    use MooseX::Locked;

    has foo => ( is => 'ro' );


    sub thing_that_sets_foo {
        my ($self) = @_;
        $self->{oof} = 1;    # RUNTIME EXCEPTION

According to Moose's authors, you should never access attributes directly in this way. But when converting legacy code, eliminating direct hash accesses may be inconvenient, if not impossible. And I've found that the speed gain is sometimes impossible to resist. In such circumstances, or if you simply want to protect against others falling into temptation, you may find this module helpful.

Note that this module provides a metarole (i.e. a role on the metaclass), not a role. Your objects will not report ->does('MooseX::Locked').


If your code modifies the Moose attribute list after any objects are created, then you should avoid this class for now and report your use case in a ticket. It's possible that the code needs to be changed to serve you better. Or maybe it's just not for you. :)

Locked hashes use more memory than their unlocked equivalents. If you're going to have a LOT of a given class around, you may not want to use this role. But test first -- human intuition is not an effective profiler.


Chip Salzenberg, <chip at pobox.com>


Please report any bugs or feature requests to bug-moosex-locked at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Locked. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc MooseX::Locked

You can also look for information at:



Copyright 2011 Chip Salzenberg.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.