Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Version 0.07
Browse files Browse the repository at this point in the history
  • Loading branch information
schwern committed Jan 22, 2010
1 parent 2dd25b7 commit d3bb5aa
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0.07
0.07 Fri Jan 22 10:43:10 PST 2010
Bug Fixes:
* Classes which defined constants did not work with mixin on
Perl 5.10.x. [rt.cpan.org 40495]
Expand Down
377 changes: 377 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ t/mixup_errors.t
t/private_methods.t
t/SUPER.t
t/universal.t
README
LICENSE
META.yml
34 changes: 34 additions & 0 deletions META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
abstract: 'Mix-in inheritance, an alternative to multiple inheritance'
author:
- 'Michael G Schwern E<lt>[email protected]<gt>'
build_requires:
Test::More: 0.4
Test::NoWarnings: 0
configure_requires:
Module::Build: 0.36
generated_by: 'Module::Build version 0.3601'
keywords:
- mixin
- inheritance
- devel
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
name: mixin
provides:
mixin:
file: lib/mixin.pm
version: 0.07
mixin::with:
file: lib/mixin/with.pm
version: 0.07
requires:
base: 0
resources:
bugtracker: https://rt.cpan.org/Public/Dist/Display.html?Name=mixin
homepage: http://search.cpan.org/dist/mixin
license: http://dev.perl.org/licenses/
repository: http://github.com/schwern/mixin
version: 0.07
119 changes: 119 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
NAME
mixin - Mix-in inheritance, an alternative to multiple inheritance

SYNOPSIS
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

DESCRIPTION
Mixin inheritance is an alternative to the usual multiple-inheritance
and solves the problem of knowing which parent will be called. It also
solves a number of tricky problems like diamond inheritence.

The idea is to solve the same sets of problems which MI solves without
the problems of MI. For all practical purposes you can think of a mixin
as multiple inheritance without the actual inheritance.

Mixins are a band-aid for the problems of MI. A better solution is to
use traits (called "Roles" in Perl 6), which are like mixins on
steroids. Class::Trait implements this.

Using a mixin class
There are two steps to using a mixin-class.

First, make sure you are inherited from the class with which the
mixin-class is to be mixed.

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

Since Dog::Small isa Dog, that does it. Then simply mixin the new
functionality

use mixin 'Dog::Retriever';

and now you can use fetch().

Writing a mixin class
See mixin::with.

Mixins, Inheritance and SUPER
A class which uses a mixin *does not* inherit from it. However, through
some clever trickery, `SUPER' continues to work. Here's an example.

{
package Parent;
sub foo { "Parent" }
}

{
package Middle;
use mixin::with "Parent";

sub foo {
my $self = shift;
return $self->SUPER::foo(), "Middle";
}
}

{
package Child;
use base "Parent";
use mixin "Middle";

sub foo {
my $self = shift;
return $self->SUPER::foo(), "Child";
}
}

print join " ", Child->foo; # Parent Middle Child

This will print `Parent Middle Child'. You'll note that this is the same
result if Child inherited from Middle and Middle from Parent. Its also
the same result if Child multiply inherited from Middle and Parent but
*NOT* if it inherited from Parent then Middle. The advantage of mixins
vs multiple inheritance is such ambiguities do not exist.

Note that even though both the Child and Middle define foo() the Middle
mixin does not overwrite Child's foo(). A mixin does not simply export
its methods into the mixer and thus does not blow over existing methods.

NOTES
A mixin will not warn if the mixin and the user define the same method.

AUTHOR
Michael G Schwern <[email protected]>

LICENSE
Copyright 2002-2010 by Michael G Schwern

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

http://dev.perl.org/licenses/

SEE ALSO
Class::Trait - mixin.pm is a gateway drug to traits

Class::C3 - another band-aid on multiple inheritance

Moose::Role - Moose's implementation of traits/roles.

2 changes: 1 addition & 1 deletion lib/mixin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mixin;
use strict;
no strict 'refs';
use vars qw($VERSION);
$VERSION = '0.06';
$VERSION = '0.07';


=head1 NAME
Expand Down
2 changes: 1 addition & 1 deletion lib/mixin/with.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mixin::with;
use strict;
no strict 'refs';
use vars qw($VERSION);
$VERSION = 0.06;
$VERSION = 0.07;

=head1 NAME
Expand Down

0 comments on commit d3bb5aa

Please sign in to comment.