Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Finding the interface implemented by an object

by j1n3l0 (Friar)
on Nov 28, 2007 at 14:01 UTC ( [id://653534]=note: print w/replies, xml ) Need Help??


in reply to Re: Finding the interface implemented by an object
in thread Finding the interface implemented by an object

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!

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

    There are two important things to note about the Moose solution, which make it superior to the Class::Std solution.

    1. A Mouse is not a Danger
    2. Inheritance is an "is-a" relationship, and the Class::Std version incorrectly models the Mouse's relationship with Danger. The Moose version states that a Mouse "does" Danger, which still doesn't read quite right, but Danger is clearly more of a Trait of this particular Mouse, and not something that the Mouse "is".

    3. The CUMULATIVE(BASE FIRST) approach lacks a degree of control
    4. Because CUMULATIVE(BASE FIRST) determines the order in which your methods are called, you are limited to how much control you can have. Using the Moose modifiers before/after/around you have a great degree of control over when and how your methods get called. Moose also provides augment/inner which only works with classes (it makes no sense for roles), but also provides a more flexible and powerful means of controlling dispatch. See the Moose::Cookbook::Recipe7 for a good example of this.

    It should also be noted (with all due respect to TheDamian) that that Class::Std has 56 outstanding Bugs some over 2 years old and many which are quite serious. It also seems to be no longer maintained (last upload was Feb. 2006). In contrast, Moose is very actively developed by a handful of developers and is being used heavily in several large production sites so bugs tend to get fixed rather quickly.

    -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://653534]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found