如何使用Moose句柄委托对象的子对象?

时间:2012-05-14 02:16:19

标签: perl delegates moose

Moose documentation表示我可以轻松地委托给对象thing

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

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

但我真的想委托一个关于thing的对象,特别是一个datetime对象

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

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

如何构建句柄以使其执行此操作?

1 个答案:

答案 0 :(得分:3)

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

如果不将datetime_month添加到thing,则必须编写自己的委托人。