On the subject of Moose, I have a question ... Is multiple inheritance possible using Moose?

I have tried some simple examples such as this:

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;

The output I get is:

A Mouse goes squeek [but you can barely hear it!]

If I used Class::Std to implement this (using CUMULATIVE(BASE FIRST)) like so:

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;

I get the output I expect:

A Mouse goes squeek through an eye patch ... [but you can barely hear it!]

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:

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;

This gives the same output as the Class::Std version. My apologies all for jumping the gun.


Smoothie, smoothie, hundre prosent naturlig!

In reply to Re^2: Finding the interface implemented by an object by j1n3l0
in thread Finding the interface implemented by an object by oyse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.