Class::C3::Adopt::NEXT
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(); }
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.
NEXT
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.
Class::C3
There are two main reasons for using NEXT:
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.
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 to NEXT::method() with maybe::next::method(), and calls to NEXT::ACTUAL::method() with next::method().
use mro 'c3'
NEXT::method()
maybe::next::method()
NEXT::ACTUAL::method()
next::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.
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.
->NEXT::foo
@ISA
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.
NEXT::AUTOLOAD
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.
Florian Ragwitz rafl@debian.org
rafl@debian.org
Tomas Doran bobtfish@bobtfish.net
bobtfish@bobtfish.net
Copyright (c) 2008 Florian Ragwitz
You may distribute this code under the same terms as Perl itself.
To install Class::C3::Adopt::NEXT, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::C3::Adopt::NEXT
CPAN shell
perl -MCPAN -e shell install Class::C3::Adopt::NEXT
For more information on module installation, please visit the detailed CPAN module installation guide.