in reply to Re: Finding the interface implemented by an object
in thread Finding the interface implemented by an object
The output I get is:package Animal; use Moose; sub speak { my $self = shift; print 'A ', ref $self, ' goes ', $self->sound() . "\n"; } 1; package Danger; use Moose; sub speak { my $self = shift; print 'through an eye patch ...' . "\n"; } 1; package Mouse; use Moose; extends qw(Animal Danger); has 'sound' => (is => 'rw', default => 'squeek'); after 'speak' => sub { my $self = shift; print '[but you can barely hear it!]' . "\n"; }; 1; package main; my $danger_mouse = Mouse->new(); $danger_mouse->speak(); 1;
If I used Class::Std to implement this (using CUMULATIVE(BASE FIRST)) like so:A Mouse goes squeek [but you can barely hear it!]
I get the output I expect:package Animal; use Class::Std; { sub speak : CUMULATIVE(BASE FIRST) { my $self = shift; print 'A ', ref $self, ' goes ', $self->get_sound() . "\n"; } } 1; package Danger; use Class::Std; { sub speak : CUMULATIVE(BASE FIRST) { my $self = shift; print 'through an eye patch ...' . "\n"; } } 1; package Mouse; use Class::Std; use base qw(Animal Danger); { my %sound : ATTR( :name<sound>, :default<squeek> ); sub speak : CUMULATIVE(BASE FIRST) { my $self = shift; print '[but you can barely hear it!]' . "\n"; } } 1;
Is there a way to achieve similar results with Moose? I would just like to know. Thanks in advance =) UPDATE I knew I should have finished reading that post first =( This is achievable with Moose::Roles ... and a little thought. Like so:A Mouse goes squeek through an eye patch ... [but you can barely hear it!]
This gives the same output as the Class::Std version. My apologies all for jumping the gun.package Animal; use Moose; sub speak { my $self = shift; print 'A ', ref $self, ' goes ', $self->sound() . "\n"; } 1; package Danger; use Moose::Role; requires 'speak'; after 'speak' => sub { my $self = shift; print 'through an eye patch ...' . "\n"; }; 1; package Mouse; use Moose; extends 'Animal'; with 'Danger'; has 'sound' => (is => 'rw', default => 'squeek'); after 'speak' => sub { my $self = shift; print '[but you can barely hear it!]' . "\n"; }; 1; package main; my $danger_mouse = Mouse->new(); $danger_mouse->speak(); 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Finding the interface implemented by an object
by stvn (Monsignor) on Nov 28, 2007 at 15:19 UTC |