in reply to delegate Moose accessors to a HASH?

handles => { this => sub { $_[0]->data->{this} }, that => sub { $_[0]->data->{that} }, },

Replies are listed 'Best First'.
Re^2: delegate Moose accessors to a HASH?
by perl5ever (Pilgrim) on Jun 23, 2011 at 19:42 UTC
    The setter doesn't seem to work:
    $foo->that(10) # sets $foo->data->{that} = 10

    I can make this work:

    $foo->that = 10
    by adding :lvalue to the sub declarations, but that's not what I want.

      Sorry, I wrote it as a getter, not an accessor.

      handles => { this => sub { @_ == 1 ? $_[0]->data->{this} : $_[0]->data->{this} = $_[1] }, that => sub { @_ == 1 ? $_[0]->data->{that} : $_[0]->data->{that} = $_[1] }, },

      You can use a map to avoid duplication and/or an accessor generating function.