From: Dave Rolsky Date: Sun, 3 Aug 2008 17:47:29 +0000 (+0000) Subject: Wrote Extending recipe 1, on using an alternate object base class. X-Git-Tag: 0_55~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d5e84b86a1326285589d2bb0f406919374e4eea8;p=gitmo%2FMoose.git Wrote Extending recipe 1, on using an alternate object base class. --- diff --git a/Changes b/Changes index fccb2e7..1486a20 100644 --- a/Changes +++ b/Changes @@ -29,6 +29,10 @@ Revision history for Perl extension Moose * Moose::Cookbook::Meta::Recipe4 - a very simple metaclass example (Dave Rolsky) + * Moose::Cookbook::Extending::Recipe1 + - how to write a Moose-alike module to use your own object base + class (Dave Rolsky) + * Moose::Cookbook::Extending::Recipe2 - how to write modules with an API just like C (Dave Rolsky) diff --git a/lib/Moose/Cookbook/Extending/Recipe1.pod b/lib/Moose/Cookbook/Extending/Recipe1.pod new file mode 100644 index 0000000..2be1127 --- /dev/null +++ b/lib/Moose/Cookbook/Extending/Recipe1.pod @@ -0,0 +1,69 @@ + +=pod + +=head1 NAME + +Moose::Cookbook::Extending::Recipe1 - Providing an alternate base object class + +=head1 SYNOPSIS + + package MyApp::Base; + use Moose; + + extends 'Moose::Object'; + + before 'new' => sub { warn "Making a new " . $_[0] }; + + no Moose; + + package MyApp::UseMyBase; + use Moose (); + + sub import { + my $caller = caller(); + + return if $caller eq 'main'; + + Moose::init_meta( $caller, + 'MyApp::Object', + ); + + Moose->import( { into => $caller }, @_ ); + } + + sub unimport { + my $caller = caller(); + + Moose->unimport( { into => $caller }, @_ ); + } + +=head1 DESCRIPTION + +Often you find that you want to share some behavior between all your +classes. One way to do that is to make a base class and simply add +C> to every class in your +application. However, that can get tedious. Instead, you can simply +create your Moose-alike module that sets the base object class to +C for you. + +Then, instead of writing C> you can write C>. + +In this particular example, our base class issues some debugging +output every time a new object is created, but you can surely think of +some more interesting things to do with your own base class. + +=head1 AUTHOR + +Dave Rolsky Eautarch@urth.orgE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2006-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