Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

moose

I'm an absolute newbie to Moose and so far I have read Moose and most of the Cookbook.

There a few things I don't 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 e.g. $br->{start}, but I can also modify them (although they are "read only") using e.g. $br->{start}=5000. I can also add new keys like $br->{xxx}=111.

Am I missing anything? Isn't the object protected in some way? What's the meaning of ro?

like image 906
David B Avatar asked Oct 01 '10 13:10

David B


2 Answers

When you access to the attribute with $br->{start}, you are bypassing the accessor and you are adressing directly the underlying Moose implementation. You can do it, but you are not supposed to. Also, if Moose changes the implementation, your code will break.

You should instead access the attribute using the accessor method:

my $start = $br->start;

When you say that the attribute is 'RO', it means you are not allowed to change the attribute value using the accessor:

$br->start(32);
like image 38
dolmen Avatar answered Sep 18 '22 17:09

dolmen


When you said is => 'ro' you told Moose to create read-only accessors for you, that is, a reader method. You call that as

$br->start;

or

$br->end;

Setting the attributes using those methods will result in an exception:

$br->start(42);

If you had used is => 'rw', then the above would work and update the attribute's value.

What you're doing is direct hash access on the object, which violates encapsulation and shouldn't ever be necessary when using Moose.

The Moose manual, i.e. all the documents under the Moose::Manual namespace explain that in detail. A good starting point for questions like this is probably Moose::Manual::Attributes.

like image 127
rafl Avatar answered Sep 21 '22 17:09

rafl