From: Dave Rolsky Date: Wed, 27 Aug 2008 15:19:22 +0000 (+0000) Subject: wrote extending recipe2 (role for object base class) X-Git-Tag: 0.55_04~2^2~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f3ce05792020397f953e034e9eb181015a7e8678;hp=26acc541630dabaf30511212e3f2a599bf021814;p=gitmo%2FMoose.git wrote extending recipe2 (role for object base class) --- diff --git a/lib/Moose/Cookbook.pod b/lib/Moose/Cookbook.pod index 0f106ca..5a9d225 100644 --- a/lib/Moose/Cookbook.pod +++ b/lib/Moose/Cookbook.pod @@ -175,10 +175,12 @@ There are quite a number of ways to extend Moose. This recipe explains provides an overview of each method, and provides recommendations for when each is appropriate. -=item L - Providing a base object class role (TODO) +=item L - Providing a base object class role Many base object class extensions can be implemented as roles. This -example shows how to provide a base object class debugging role. +example shows how to provide a base object class debugging role that +is applied to any class that uses a notional C +module. =item L - Providing an alternate base object class diff --git a/lib/Moose/Cookbook/Extending/Recipe2.pod b/lib/Moose/Cookbook/Extending/Recipe2.pod new file mode 100644 index 0000000..0bdeae5 --- /dev/null +++ b/lib/Moose/Cookbook/Extending/Recipe2.pod @@ -0,0 +1,68 @@ + +=pod + +=head1 NAME + +Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class + +=head1 SYNOPSIS + + package MooseX::Debugging; + + use strict; + use warnings; + + use Moose::Exporter; + use Moose::Util::MetaRole; + use MooseX::Debugging::Role::Object; + + Moose::Exporter->setup_import_methods(); + + sub init_meta { + shift; + my %options = @_; + + Moose::Util::MetaRole::apply_base_object_roles( + for_class => $options{for_class}, + role => ['MooseX::Debugging::Role::Object'], + ); + } + + + package MooseX::Debugging::Role::Object; + + after 'BUILD' => sub { + my $self = shift; + + warn "Made a new " . ref $self . " object\n"; + } + +=head1 DESCRIPTION + +In this example, we provide a role for the base object class that adds +some simple debugging output. Every time an object is created, it +spits out a warning saying what type of object it was. + +Obviously, a real debugging role would do something more interesting, +but this recipe is all about how we apply that role. + +In this case, with the combination of L and +L, we ensure that when a module does "S", it automatically gets the debugging role applied +to its base object class. + +=head1 AUTHOR + +Dave Rolsky Eautarch@urth.orgE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2008 by Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut +