From: Aankhen Date: Tue, 1 May 2007 06:51:42 +0000 (+0000) Subject: Moose::Cookbook::Recipe1: X-Git-Tag: 0_21~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a9eca7dfbe0be5f34fd7fb42d821ccebe7f01e22;p=gitmo%2FMoose.git Moose::Cookbook::Recipe1: * POD work: mainly grammar tweaks. --- diff --git a/lib/Moose/Cookbook/Recipe1.pod b/lib/Moose/Cookbook/Recipe1.pod index 71ed4d0..f9382d5 100644 --- a/lib/Moose/Cookbook/Recipe1.pod +++ b/lib/Moose/Cookbook/Recipe1.pod @@ -39,38 +39,38 @@ example found in the classic K&R C book as well, and many other places. And now, onto the code: As with all Perl 5 classes, a Moose class is defined in a package. -Moose now handles turning C and C on for you, so -all you need do is say C, and no kittens will die. +Moose now handles turning on C and C for you, so +all you need to do is say C, and no kittens will die. -By loading Moose, we are enabling the Moose "environment" to be -loaded within our package. This means that we export some functions -which serve as Moose "keywords". This is nothing fancier than that, -just plain old exported functions. +By loading Moose, we are enabling the loading of the Moose +"environment" into our package. This means that we import some +functions which serve as Moose "keywords". These aren't anything +fancy, just plain old exported functions. Another important thing happens at this stage as well. Moose will automatically set your package's superclass to be L. -The reason we do this, is so that we can be sure that you will -inherit from L and get the benefits that provides -(see the L for those details). However, you don't +The reason we do this, is so that we can be sure that your class +will inherit from L and get the benefits that +provides (see L for details). However, you don't actually I to inherit from L if you don't -want to, all other features of Moose are still accessible to you. +want to. All Moose features will still be accessible to you. Now, onto the keywords. The first one we see here is C, which -defines an instance attribute in your class. +defines an instance attribute in your class: has 'x' => (isa => 'Int', is => 'ro'); -This will create an attribute named C, it will expect that the +This will create an attribute named C, which will expect the value stored in the attribute to pass the type constraint C (1), and the accessor generated for this attribute will be read-only (abbreviated as C). -The next C line is very similar, with only one difference. +The next C line is very similar, with only one difference: has 'y' => (isa => 'Int', is => 'rw'); -For the C attribute a read/write accessor will be generated -(abbreviated as C). +A read/write (abbreviated as C) accessor will be generated for +the C attribute. At this point the attributes have been defined, and it is time to define our methods. In Moose, as with regular Perl 5 OO, a method @@ -105,7 +105,7 @@ where C will C values onto the package's C<@ISA>. It is my opinion that the behavior of C is more intuitive in that it is more explicit about defining the superclass relationship. -A small digression here, both Moose and C support multiple +A small digression here: both Moose and C support multiple inheritance. You simply pass all the superclasses to C, like so: @@ -119,7 +119,7 @@ a new attribute for B called C. As with B's C and C attributes, this attribute has a type constraint of C, but it differs in that it does B ask for any autogenerated accessors. The result being (aside from -breaking object encapsulation), that C is a private attribute. +broken object encapsulation) that C is a private attribute. Next comes another Moose feature which we call method "modifiers" (or method "advice" for the AOP inclined). The modifier used here @@ -147,7 +147,7 @@ would get the same results with this code: } You could also use another Moose method modifier, C here, -and get the same results again. Here is how that would look. +and get the same results again. Here is how that would look: override 'clear' => sub { my $self = shift; @@ -159,9 +159,9 @@ The C modifier allows you to use the C keyword within it to dispatch to the superclass's method in a very Ruby-ish style. -Now of course, what use is a class if you cant instantiate objects -with it. Now since B inherits from L, it will -inherit the default L constructor called C. Here +Now, of course, what use is a class if you can't instantiate objects +with it? Since B inherits from L, it will also +inherit the default L constructor: C. Here are two examples of how that is used: my $point = Point->new(x => 1, y => 2); @@ -179,7 +179,7 @@ done, you can refer to the F test file. =head1 CONCLUSION I hope this recipe has given you some explanation of how to use -Moose to build you Perl 5 classes. The next recipe will build upon +Moose to build your Perl 5 classes. The next recipe will build upon the basics shown here with more complex attributes and methods. Please read on :) @@ -190,16 +190,16 @@ Please read on :) =item (1) Several default type constraints are provided by Moose, of which -C is one. For more information on the built-in type constraints +C is one. For more information on the builtin type constraints and the type constraint system in general, see the L documentation. =item (2) Future plans for Moose include allowing for alternate instance -structures such as blessed ARRAY refs and such. If you want you Moose -classes to be interchangeable, it is advised that you avoid direct -instance access, like that which is shown above. +structures such as blessed ARRAY refs and such. If you want your +Moose classes to be interchangeable, it is advisable to avoid +direct instance access, like that shown above. =back