NAME
Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class
SYNOPSIS
package MooseX::Debugging; use Moose (); use Moose::Exporter; use Moose::Util::MetaRole; Moose::Exporter->setup_import_methods; sub init_meta { shift; my %options = @_; my $meta = Moose->init_meta(%options); Moose::Util::MetaRole::apply_base_class_roles( for_class => $options{for_class}, roles => ['MooseX::Debugging::Role::Object'], ); return $meta; } package MooseX::Debugging::Role::Object; use Moose::Role;;
This creates an
import method in the
MooseX::Debugging package. Since we are not actually exporting anything, we do not pass
setup_import_methods any parameters. However, we need to have an
import method to ensure that our
init_meta method is called.
Then in our
init_meta method we have this line:
Moose->init_meta(%options);
This is a bit of boilerplate that almost every extension will use. This ensures that the caller has a normal Moose metaclass before we go and add traits to it.
The
Moose->init_meta method does ensures that the caller has a sane metaclass, and we don't want to replicate that logic in our extension. If the
Moose->init_meta was already called (because the caller did
use Moose before using our extension), then calling
Moose->init_meta again is effectively a no-op.
AUTHOR
Dave Rolsky <autarch@urth.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.