Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Using ArrayRef data members in Moose

by stephen (Priest)
on Jul 04, 2008 at 00:25 UTC ( #695498=note: print w/replies, xml ) Need Help??

in reply to Using ArrayRef data members in Moose

I understand that you want to avoid using extension modules, but in this case AttributeHelpers does almost exactly what you want.
package Holder; use Moose; use MooseX::AttributeHelpers; has 'things' => ( metaclass => 'Collection::Array', is => 'rw', isa => 'ArrayRef[Str]', default => sub { [qw/blue green green yellow/] }, auto_deref => 1, provides => { 'push' => 'add_things' }, ); package main; my $holder = Holder->new(); print "My things are:\t\t", join(' ', $holder->things() ), "\n"; $holder->add_things('mango'); print "My things are now:\t", join(' ', $holder->things() ), "\n";
And the output:
My things are: blue green green yellow My things are now: blue green green yellow mango
Moose doesn't offer lvalue accessors, which I believe is a deliberate choice.


Replies are listed 'Best First'.
Re^2: Using ArrayRef data members in Moose
by stvn (Monsignor) on Jul 04, 2008 at 03:39 UTC
    Moose doesn't offer lvalue accessors, which I believe is a deliberate choice.

    Yes, very deliberate. Lvalue accessors are broken by design for anything other then simple accessors which require no validation or processing of arguments at all. The only way to get around this is to use tie() and to put all the validation/arg-processing in the STORE method that tie() uses. However this is a completely incorrect division of responsibility not to mention slow and kludgey, and all for such a small amount of syntactic sugar, it just wasn't worth it.

Re^2: Using ArrayRef data members in Moose
by window_breaker (Sexton) on Sep 24, 2010 at 18:44 UTC
    I was looking for this kind of behavior in a module I'm writing, and found this thread very useful. I'd like to point out that the functionality provided by MooseX::AttributeHelpers has been added to core Moose, and it's very slick. Read about it on CPAN:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695498]
[chacham]: i'm reading an article on and thee is a disturbing message on the bottom right that says, "Fork me on GitHub". Maybe i just have a dirty mind.
[choroba]: Check the ninja on
[chacham]: oh my...
[Corion]: choroba++ # fixed width
marto has a Dirty_Mind on vinyl

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2017-08-17 12:58 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (287 votes). Check out past polls.