NAME
Class::C3::Adopt::NEXT
SYNOPSIS
package MyApp::Plugin::FooBar;
#use NEXT;
use Class::C3::Adopt::NEXT;
sub a_method {
my ($self) = @_;
# Do some stuff
# Re-dispatch method
$self->NEXT::method();
}
DESCRIPTION
NEXT sucks. I mean, it really really sucks. It was a good solution a few years ago, but isn't any more. It's slow, and the order in which it re-dispatches methods appears random at times. It also encourages bad programming practices, as you end up with code to re-dispatch methods when all you really wanted to do was run some code before or after a method fired.
However, if you have a large application, then weaning yourself off NEXT
isn't easy.
This module is intended as a drop-in replacement for NEXT, supporting the same interface, but using Class::C3 to do the hard work. You can then write new code without NEXT
, and migrate individual source files to use Class::C3
or method modifiers as appropriate, at whatever pace you're comfortable with.
MIGRATING
There are two main reasons for using NEXT:
- Providing plugins which run functionality before/after your methods.
-
Use Moose and make all of your plugins Moose::Roles, then use method modifiers to wrap methods.
Example:
package MyApp::Plugin::FooBar; use Moose::Role; before 'a_method' => { my ($self) = @_; # Do some stuff };
You can then use something like MooseX::Traits or MooseX::Object::Pluggable to load plugins dynamically.
- A complex class hierarchy where you actually need multiple dispatch.
-
Recommended strategy is to find the core class responsible for loading all the other classes in your application and add the following code:
use MRO::Compat; Class::C3::initialize();
after you have loaded all of your modules.
You then add
use mro 'c3'
to the top of a package as you start converting it, and gradually replace your calls toNEXT::method()
withmaybe::next::method()
, and calls toNEXT::ACTUAL::method()
withnext::method()
.On systems with Class::C3::XS present, this will automatically be used to speed up method re-dispatch. If you are running perl version 5.9.5 or greater then the C3 method resolution algorithm is included in perl. Correct use of MRO::Compat as shown above allows your code to be seamlessly forward and backwards compatible, taking advantage of native versions if available, but falling back to using pure perl
Class::C3
.
CAVEATS
There are some inheritance hierarchies that it is possible to create which cannot be resolved to a simple C3 hierarchy. In that case, this module will fall back to using NEXT
. In this case a warning will be emitted.
Because calculating the MRO of every class every time ->NEXT::foo
is used from within it is too expensive, runtime manipulations of @ISA
are prohibited.
FUNCTIONS
This module replaces NEXT::AUTOLOAD
with it's own version. If warnings are enabled then a warning will be emitted on the first use of NEXT
by each package.
SEE ALSO
MRO::Compat and Class::C3 for method re-dispatch and Moose for method modifiers and roles.
NEXT for documentation on the functionality you'll be removing.
AUTHORS
Florian Ragwitz rafl@debian.org
Tomas Doran bobtfish@bobtfish.net
COPYRIGHT AND LICENSE
Copyright (c) 2008 Florian Ragwitz
You may distribute this code under the same terms as Perl itself.