Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 57582
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: knb [...] gfz-potsdam.de
Cc:
AdminCc:

Bug Information
Severity: Unimportant
Broken in: 1.03
Fixed in: (no value)



Subject: Moose::Meta::Attribute::Native::Trait::Array -, attrib declared as 'ro', could still be 'rw'?
I am puzzled because ... in the SYNOPSIS block of the Perldoc of Module Moose::Meta::Attribute::Native::Trait::Array - e.g. see here http://search.cpan.org/~flora/Moose-1.03/lib/Moose/Meta/Attribute/Native/Trait/Array.pm attrib 'options' is declared as 'ro'. it seems to me that Attributes declared as 'ro' are actually 'rw' - because it can be written to with the 'push' method of the Moose::Meta::Attribute::Native::Trait::Array Attribute. otherwise add_option etc from the 'handles' => option below would not make any sense ... it is contradictory in some way. Or am I missing something here? See test script below, inspired by Mouse-0.51/t/001_mouse/019-handles.t. use strict; use warnings; use Test::More; use Test::Exception; do { package Stuff; use Moose; has name => (is => 'rw'); has 'options' => ( traits => ['Array'], is => 'ro', # <== should be 'rw', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { all_options => 'elements', add_option => 'push', map_options => 'map', filter_options => 'grep', find_option => 'first', get_option => 'get', join_options => 'join', count_options => 'count', has_options => 'count', has_no_options => 'is_empty', sorted_options => 'sort', }, ); 1; }; can_ok(Stuff => qw(options has_options all_options add_option)); my $object2 = Stuff->new( name => "xx"); is($object2->name, "xx", "we really do have a Stuff "); ok($object2->has_no_options, "... empty 'option' attrib"); is($object2->has_options, 0, "there are no options"); ok($object2->add_option ("x"), "WHY does this work, the 'options' attribute is read-only"); is($object2->has_options, 1, "now there is an option"); done_testing; Linux 2.6.31-20-generic #58-Ubuntu SMP Fri Mar 12 04:38:19 UTC 2010 x86_64 GNU/Linux This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi
"is => 'ro'" is just a shortcut for "reader => $name_of_attribute", that is, "generate a reader method called $name_of_attribute for me". You can not set a new value using that generated method. (You can still modify a reference value though the reference, but that's outside of the scope of the generated accessors really.) "is => 'ro'" does not mean "this value may not ever be modified". See also the common idiom: has foo => ( is => 'ro', writer => '_set_foo', # private setter method ); With native traits, you just generate additional methods, just like you can with the reader, writer, and accessor options already. Those don't have any relation to other accessor methods that might exist for an attribute, no matter if they have been declared with "is", "reader", "writer", "accessor", or anything else. If you feel like the documentation is too unclear on that, please feel free to reopen this ticket.
From: knb [...] gfz-potsdam.de
Show quoted text
> If you feel like the documentation is too unclear on that, please feel > free to reopen this ticket.
Thank you very much. On stackoverflow.com someone asked the same question, so I'm not the only one being confused. Maybe I'll file a new "wishlist" bug report with more suggestions for documentation improvements. I noted some other things (fine points really) that I don't have the time to describe right now.