From: Shawn M Moore Date: Sun, 21 Oct 2007 06:05:20 +0000 (+0000) Subject: A few more recipe 11 cleanups and rephrasings X-Git-Tag: 0_27~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=94acbcd7c857b1b977ce643c8de512b8a19d0bf6;p=gitmo%2FMoose.git A few more recipe 11 cleanups and rephrasings --- diff --git a/lib/Moose/Cookbook/Recipe11.pod b/lib/Moose/Cookbook/Recipe11.pod index e4cd60f..ef67479 100644 --- a/lib/Moose/Cookbook/Recipe11.pod +++ b/lib/Moose/Cookbook/Recipe11.pod @@ -1,6 +1,3 @@ - -=pod - =head1 NAME Moose::Cookbook::Recipe11 - The meta-attribute example @@ -12,7 +9,7 @@ Moose::Cookbook::Recipe11 - The meta-attribute example extends 'Moose::Meta::Attribute'; has label => ( - is => 'ro', + is => 'rw', isa => 'Str', predicate => 'has_label', ); @@ -139,29 +136,28 @@ somewhat ungloriously. extends 'Moose::Meta::Attribute'; You subclass metaclasses the same way you subclass regular classes. (Extra -credit: how in the actual hell can you use the MOP to extend itself?) Moving -on. +credit: how in the actual hell can you use the MOP to extend itself?) has label => ( - is => 'ro', + is => 'rw', isa => 'Str', predicate => 'has_label', ); -Now things get a little icky. We're adding a attribute to the attribute -metaclass. For clarity, I'm going to call this a meta-attribute. - -This creates a new meta-attribute in the C -metaclass. The new meta-attribute's name is 'label'. The predicate just creates -a method that asks the question "Does this attribute have a value?" +Hey, this looks pretty reasonable! This is plain jane Moose code. Recipe 1 +fare. This is merely making a new attribute. An attribute that attributes have. +A meta-attribute. It may sound scary, but it really isn't! Reread +L if this really is terrifying. -Of course, if you step a foot back, you can see that this is really just adding -an attribute to a class. Don't be alarmed! +The name is "label", it will have a regular accessor (except of course at +create time), and is a tring. C is a standard part of C. It +just creates a method that asks the question "Does this attribute have a +value?" package Moose::Meta::Attribute::Custom::Labeled; sub register_implementation { 'MyApp::Meta::Attribute::Labeled' } -This registers the new metaclass with Moose. That way attributes can actually +This registers our new metaclass with Moose. That way attributes can actually use it. More on what this is doing in a moment. Note that we're done defining the new metaclass! Only nine lines of code, and @@ -196,7 +192,11 @@ use C<< metaclass => 'MyApp::Meta::Attribute::Labeled' >> on every attribute. As usual, Moose optimizes in favor of the end user, not the metaprogrammer. :) Finally, we see that C is setting our new meta-attribute, C