Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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.

stephen


Comment on Re: Using ArrayRef data members in Moose
Select or Download Code
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.

    -stvn
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: http://search.cpan.org/~drolsky/Moose-1.14/lib/Moose/Meta/Attribute/Native.pm

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695498]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-10-23 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls