Re: Announcement: Moose 2.0800 is released!

[email protected] (Lars Balker)
Newsgroups perl.moose
Message-ID <CAB-_BaR_tmJ9i0O9krMcDSWbv_mLtuBqNzp1DH_B7ACeQ3ursA@mail.gmail.com>
On Fri, Mar 29, 2013 at 10:13 AM, Ovid <[email protected]> wrote:
>     { package R::Programmer; use Moose::Role; sub pay_rate {12} }
>     { package R::Clerk;      use Moose::Role; sub pay_rate {10} }
>     { package R::Employee;   use Moose;
>         with 'R::Programmer' => {
>             # this is why I want a "rename" property here
>             exclude => 'pay_rate',
>             alias   => { pay_rate => 'programmer_pay_rate' }
>           },
>           'R::Clerk' => {
>             exclude => 'pay_rate',
>             alias   => { pay_rate => 'clerk_pay_rate' }
>           };
>         has [qw/hours programming_hours drudgery/] => ( is => 'ro' );
>         sub pay_rate {8} # ugly hack for quick example. Oops
>
>         sub paycheck {
>             my $self = shift;
>             return $self->programmer_pay_rate * $self->programming_hours
>                  + $self->clerk_pay_rate      * $self->drudgery;
>         }
>     }
>     my $employee = R::Employee->new(
>         programming_hours => 15,
>         drudgery          => 25,
>     );
>     say $employee->paycheck;

As Jesse's earlier example showed, instead of excluding and aliasing,
just refer to the methods directly within the roles:

use v5.10.0;
{ package R::Programmer; use Moose::Role; sub pay_rate {12} }
{ package R::Clerk;      use Moose::Role; sub pay_rate {10} }
{ package R::Employee;   use Moose;
  with 'R::Programmer';
  with 'R::Clerk';
  has [qw/programming_hours drudgery/] => ( is => 'ro' );
  sub paycheck {
      my $self = shift;
      return $self->R::Programmer::pay_rate * $self->programming_hours
          + $self->R::Clerk::pay_rate      * $self->drudgery;
  }
}
my $employee = R::Employee->new(
    programming_hours => 15,
    drudgery          => 25,
);
say $employee->paycheck;

--
Lars Balker                                                  Consult::Perl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.