How can I use read-only attributes of Moose objects?

I am an absolute beginner before Moose, and so far I have read Moosemost of the Cookbook .

There are a few things that I do not get. I created the following package:

package MyRange;

use Moose;
use namespace::autoclean;

has [ 'start', 'end' ] => (
    is       => 'ro',
    isa      => 'Int',
    required => 1,
);

__PACKAGE__->meta->make_immutable;

1;

Then:

use MyRange;    
my $br = MyRange->new(
    start                => 100,
    end                  => 180
);

Now I can access my fields using, for example, $br->{start}but I also can modify them (though they are "read only"), using, for example $br->{start}=5000. I can also add new keys, for example $br->{xxx}=111.

Did I miss something? Is any object protected? What does it mean ro?

+5
source share
2 answers

is => 'ro', Moose , .

$br->start;

$br->end;

:

$br->start(42);

is => 'rw', .

, , - - , Moose.

Moose, .. Moose::Manual, . , , Moose::Manual::Attributes.

+14

$br->{start}, , Moose. , . , Moose , .

:

my $start = $br->start;

, "RO", , :

$br->start(32);
+3

All Articles