Question

I don't know what's the difference between the mixin and mixin::with module?

package Dog;
sub speak { print "Bark!\n" }
sub new { my $class = shift;  bless {}, $class }

package Dog::Small;
use base 'Dog';
sub speak { print "Yip!\n"; }

package Dog::Retriever;
use mixin::with 'Dog';
sub fetch { print "Get your own stinking $_[1]\n" }

package Dog::Small::Retriever;
use base 'Dog::Small';
use mixin 'Dog::Retriever';

my $small_retriever = Dog::Small::Retriever->new;
$small_retriever->speak;          # Yip!
$small_retriever->fetch('ball');  # Get your own stinking ball

In the code above, can the following code :

package Dog::Small::Retriever;
use base 'Dog::Small';
use mixin 'Dog::Retriever';

be replaced with the following one?

package Dog::Small::Retriever;
#use base 'Dog::Small';
use mixin:with 'Dog::Retriever';
Was it helpful?

Solution

It looks as though mixin::with is intended for use in defining classes which add/change behaviors (Dog::Small and Dog::Retriever in your example), while mixin is used in the final consuming class (Dog::Small::Retriever).

But also note that the documentation for mixin::with suggests "Instead of mixins, please consider using traits. See Class::Trait for an implementation." Class::Trait, in turn, is deprecated and suggests Moose::Role.

Oh - you cannot skip the use base 'Dog::Small' declaration - if you did, your class would not inherit from Dog::Small (or Dog).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top