MooseX::Types::Ro - Moose type constraints for read-only containers
package Foo;
use Moose;
use MooseX::Types::Ro qw(RoArrayRef RoHashRef);
has array => ( is => 'ro', isa => RoArrayRef, coerce => 1 );
has hash => ( is => 'ro', isa => RoHashRef, coerce => 1 );
my $foo = Foo->new(array => [1, 2, 3], hash => { foo => 1, bar => 2 });
# These all throw exceptions now:
$foo->array->[0] = 42;
$foo->array->[3] = 42;
push @{$foo->array}, 69;
$foo->hash->{bar} = 3;
delete $foo->hash->{bar};p
$foo->hash->{baz} = 4;
MooseX::Types::Ro provides Moose type constraints for read-only array and hash references. Attempting to modify the values in any way, including adding or deleting from the arrays and hashes will throw an exception.
A subtype of ArrayRef
in which the array itself and all the elements are read-only. Coerces from ArrayRef
by making a shallow copy and marking it read-only.
A subtype of HashRef
in which the hash itself and all the values are read-only. Coerces from HashRef by making a shallow copy and marking it read-only.
MooseX::Types, MooseX::Types::Moose, Moose::Util::TypeConstraints, Internals
Dagfinn Ilmari Mannsåker <[email protected]>.
Copyright (c) 2010 Dagfinn Ilmari Mannsåker <[email protected]>.
This program is free software; you can redistribute and/or modify it under the same terms as perl itself.