|
NAMEmixin::with - declaring a mix-in classSYNOPSISpackage Dog::Retriever; use mixin::with 'Dog'; DESCRIPTIONmixin::with is used to declare mix-in classes.When to use a mixin?Mixin classes useful for those that add new functionality to an existing class. If you find yourself doing:package Foo::ExtraStuff; use base 'Foo'; sub new_method { ... } package Bar; use base qw(Foo Foo::ExtraStuff); it's a good indication that Foo::ExtraStuff might do better as a mixin. Instead of mixins, please consider using traits. See Class::Trait for an implementaiton. How?Basic usage is simple:package Foo::Extra; use mixin::with 'Foo'; sub new_thing { my($self) = shift; ...normal method... } "use mixin::with 'Foo'" is similar to subclassing from 'Foo'. All public methods of Foo::Extra will be mixed in. mixin::with considers all methods that don't start with an '_' as public. Limitations of mixinsThere's one critical difference between a normal subclass and one intended to be mixin. It can have no private methods. Instead, use lexical methods.my $private = sub { ... }; $self->$private(@args); instead of sub _private { ... } $self->_private(@args); Don't worry, it's the same thing. FAQ
AUTHORMichael G Schwern <schwern@pobox.com>LICENSECopyright 2002-2010 by Michael G SchwernThis 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 ALSOmixin, ruby from which I stole this idea.
Visit the GSP FreeBSD Man Page Interface. |