1

The Moose documentation says that I can delegate to the object thing easily enough.

has 'thing' => (
   ...
   handles => { set_foo => [ set => 'foo' ] },
);

# $self->set_foo(...) calls $self->thing->set('foo', ...)

But I really want to delegate to an object on thing, specifically a datetime object

has 'thing' => (
   ...
   handles => {
       get_month => { datetime ... },
   },
);

# $self->get_month calls $self->thing->datetime->month;

how would I have to construct handles to get it to do this?

xenoterracide
  • 16,274
  • 24
  • 118
  • 243

1 Answers1

3
has thing => (
   ...
   handles => {
      get_month => sub { $_[0]->thing->datetime->month },
   },
);

Short of adding datetime_month to thing, you'll have to write your own delegator.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    At this point you're already writing the method yourself and could as well drop the `handles` declaration and just write `sub get_month` manually. – rafl May 14 '12 at 07:41
  • @rafl, You could do that, but I disagree about it being better to move it out. Specifically, writing it as a delegator conveys information to the reader that you wouldn't get otherwise. – ikegami May 14 '12 at 16:57
  • I also have other handles on the same object that didn't require this level of depth... so it kind of puts them all in the same spot. – xenoterracide May 14 '12 at 20:48
  • IMO would have been nice to more declaratively delegate to infinite object methods. maybe `method => { method => { method => [ params ] } } }` – xenoterracide May 14 '12 at 20:51
  • @xenoterracide, That's positively awful. Not only does the params bit breaks the model, it does so in a very inconsistent and very limiting way (i.e. only applies to the inner layer). – ikegami May 15 '12 at 01:17
  • Furthermore, `sub { $_[0]->thing->datetime->get_month }` is *way* more readable than `{ datetime => { month => { get_month => [] } } }` – ikegami May 15 '12 at 01:21