From: Dave Rolsky Date: Tue, 6 Jan 2009 05:32:40 +0000 (+0000) Subject: tidy code in pod for consistency, also tidy some text X-Git-Tag: 0.65~34 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c765b2544b476fef219f603d02f81fc2f697e413;p=gitmo%2FMoose.git tidy code in pod for consistency, also tidy some text --- diff --git a/lib/Moose/Cookbook/Basics/Recipe10.pod b/lib/Moose/Cookbook/Basics/Recipe10.pod index d8bd36a..3b1fdf1 100644 --- a/lib/Moose/Cookbook/Basics/Recipe10.pod +++ b/lib/Moose/Cookbook/Basics/Recipe10.pod @@ -8,32 +8,33 @@ Moose::Cookbook::Basics::Recipe10 - Operator overloading, subtypes, and coercion =head1 SYNOPSIS package Human; - + use Moose; use Moose::Util::TypeConstraints; - + subtype 'Gender' => as 'Str' => where { $_ =~ m{^[mf]$}s }; - + has 'gender' => ( is => 'ro', isa => 'Gender', required => 1 ); - + has 'mother' => ( is => 'ro', isa => 'Human' ); has 'father' => ( is => 'ro', isa => 'Human' ); - + use overload '+' => \&_overload_add, fallback => 1; - + sub _overload_add { - my ($one, $two) = @_; - + my ( $one, $two ) = @_; + die('Only male and female humans may create children') - if ($one->gender() eq $two->gender()); - - my ( $mother, $father ) = ( $one->gender eq 'f' ? ($one, $two) : ($two, $one) ); - + if ( $one->gender() eq $two->gender() ); + + my ( $mother, $father ) + = ( $one->gender eq 'f' ? ( $one, $two ) : ( $two, $one ) ); + my $gender = 'f'; - $gender = 'm' if (rand() >= 0.5); - + $gender = 'm' if ( rand() >= 0.5 ); + return Human->new( gender => $gender, mother => $mother, @@ -106,12 +107,12 @@ make classes for these genes. =head2 bey2 package Human::Gene::bey2; - + use Moose; use Moose::Util::TypeConstraints; - + type 'bey2Color' => where { $_ =~ m{^(?:brown|blue)$}s }; - + has 'color' => ( is => 'ro', isa => 'bey2Color' ); This class is really simple. All we need to know about the bey2 @@ -122,12 +123,12 @@ which validates for the two possible colors. =head2 gey package Human::Gene::gey; - + use Moose; use Moose::Util::TypeConstraints; - + type 'geyColor' => where { $_ =~ m{^(?:green|blue)$}s }; - + has 'color' => ( is => 'ro', isa => 'geyColor' ); The gey gene is nearly identical to the bey2, except that it @@ -142,31 +143,31 @@ won't get all cluttered up with the details behind the different characteristics that makes up a Human. package Human::EyeColor; - + use Moose; use Moose::Util::TypeConstraints; - + subtype 'bey2Gene' => as 'Object' => where { $_->isa('Human::Gene::bey2') }; - + coerce 'bey2Gene' => from 'Str' => via { Human::Gene::bey2->new( color => $_ ) }; - + subtype 'geyGene' => as 'Object' => where { $_->isa('Human::Gene::gey') }; - + coerce 'geyGene' => from 'Str' => via { Human::Gene::gey->new( color => $_ ) }; - + has 'bey2_1' => ( is => 'ro', isa => 'bey2Gene', coerce => 1 ); has 'bey2_2' => ( is => 'ro', isa => 'bey2Gene', coerce => 1 ); - - has 'gey_1' => ( is => 'ro', isa => 'geyGene', coerce => 1 ); - has 'gey_2' => ( is => 'ro', isa => 'geyGene', coerce => 1 ); + + has 'gey_1' => ( is => 'ro', isa => 'geyGene', coerce => 1 ); + has 'gey_2' => ( is => 'ro', isa => 'geyGene', coerce => 1 ); So, we now have a class that can hold the four genes that dictate eye color. This isn't quite enough, as we also need to calculate @@ -178,10 +179,16 @@ recessive to the brown bey gene and dominant to the blues. Finally, the bey and gey2 blue genes are recessive to both brown and green. sub color { - my ( $self ) = @_; - - return 'brown' if ($self->bey2_1->color() eq 'brown' or $self->bey2_2->color() eq 'brown'); - return 'green' if ($self->gey_1->color() eq 'green' or $self->gey_2->color() eq 'green'); + my ($self) = @_; + + return 'brown' + if ( $self->bey2_1->color() eq 'brown' + or $self->bey2_2->color() eq 'brown' ); + + return 'green' + if ( $self->gey_1->color() eq 'green' + or $self->gey_2->color() eq 'green' ); + return 'blue'; } @@ -202,16 +209,16 @@ create a new EyeColor that is derived in a similar manner as the gene selection in human reproduction. use overload '+' => \&_overload_add, fallback => 1; - + sub _overload_add { - my ($one, $two) = @_; - + my ( $one, $two ) = @_; + my $one_bey2 = 'bey2_' . _rand2(); my $two_bey2 = 'bey2_' . _rand2(); - + my $one_gey = 'gey_' . _rand2(); my $two_gey = 'gey_' . _rand2(); - + return Human::EyeColor->new( bey2_1 => $one->$one_bey2->color(), bey2_2 => $two->$two_bey2->color(), @@ -219,7 +226,7 @@ the gene selection in human reproduction. gey_2 => $two->$two_gey->color(), ); } - + sub _rand2 { return 1 + int( rand(2) ); } @@ -242,29 +249,28 @@ attribute called eye_color, and just for the sake of simple code we'll coerce an arrayref of colors in to an EyeColor object. use List::MoreUtils qw( zip ); - + subtype 'EyeColor' => as 'Object' => where { $_->isa('Human::EyeColor') }; - + coerce 'EyeColor' => from 'ArrayRef' - => via { - my @genes = qw( bey2_1 bey2_2 gey_1 gey_2 ); - return Human::EyeColor->new( zip( @genes, @$_ ) ); - }; - - has 'eye_color' => ( is => 'ro', isa => 'EyeColor', coerce => 1, required => 1 ); + => via { my @genes = qw( bey2_1 bey2_2 gey_1 gey_2 ); + return Human::EyeColor->new( zip( @genes, @$_ ) ); }; + + has 'eye_color' => + ( is => 'ro', isa => 'EyeColor', coerce => 1, required => 1 ); And then in the _overload_add() of the Human class we modify the creation of the child object to include the addition of the mother and father's eye colors. return Human->new( - gender => $gender, + gender => $gender, eye_color => ( $one->eye_color() + $two->eye_color() ), - mother => $mother, - father => $father, + mother => $mother, + father => $father, ); =head1 CONCLUSION diff --git a/lib/Moose/Cookbook/Basics/Recipe3.pod b/lib/Moose/Cookbook/Basics/Recipe3.pod index 9782548..f92ae7b 100644 --- a/lib/Moose/Cookbook/Basics/Recipe3.pod +++ b/lib/Moose/Cookbook/Basics/Recipe3.pod @@ -9,35 +9,35 @@ Moose::Cookbook::Basics::Recipe3 - A lazy B example package BinaryTree; use Moose; - - has 'node' => (is => 'rw', isa => 'Any'); - + + has 'node' => ( is => 'rw', isa => 'Any' ); + has 'parent' => ( is => 'rw', - isa => 'BinaryTree', + isa => 'BinaryTree', predicate => 'has_parent', weak_ref => 1, ); - + has 'left' => ( - is => 'rw', - isa => 'BinaryTree', - predicate => 'has_left', + is => 'rw', + isa => 'BinaryTree', + predicate => 'has_left', lazy => 1, - default => sub { BinaryTree->new(parent => $_[0]) }, + default => sub { BinaryTree->new( parent => $_[0] ) }, ); - + has 'right' => ( - is => 'rw', - isa => 'BinaryTree', - predicate => 'has_right', - lazy => 1, - default => sub { BinaryTree->new(parent => $_[0]) }, + is => 'rw', + isa => 'BinaryTree', + predicate => 'has_right', + lazy => 1, + default => sub { BinaryTree->new( parent => $_[0] ) }, ); - + before 'right', 'left' => sub { - my ($self, $tree) = @_; - $tree->parent($self) if defined $tree; + my ( $self, $tree ) = @_; + $tree->parent($self) if defined $tree; }; =head1 DESCRIPTION @@ -55,7 +55,7 @@ and finally a C slot to hold a reference back up the tree. Now, let's start with the code. Our first attribute is the C slot, defined as such: - has 'node' => (is => 'rw', isa => 'Any'); + has 'node' => ( is => 'rw', isa => 'Any' ); If you recall from the previous recipes, this slot will have a read/write accessor generated for it, and has a type constraint on it. The new item here is @@ -72,7 +72,7 @@ Next, let's move on to the C slot: has 'parent' => ( is => 'rw', - isa => 'BinaryTree', + isa => 'BinaryTree', predicate => 'has_parent', weak_ref => 1, ); @@ -100,11 +100,11 @@ Now, onto the C and C attributes. They are essentially identical, save for different names, so I will just describe one here: has 'left' => ( - is => 'rw', - isa => 'BinaryTree', - predicate => 'has_left', + is => 'rw', + isa => 'BinaryTree', + predicate => 'has_left', lazy => 1, - default => sub { BinaryTree->new(parent => $_[0]) }, + default => sub { BinaryTree->new( parent => $_[0] ) }, ); You already know what the C, C and C options do, but now we @@ -122,11 +122,11 @@ by value, this was all we had to say. But for any other item (ARRAY ref, HASH ref, object instance, etc) you would need to wrap it in a CODE reference, so this: - has 'foo' => (is => 'rw', default => []); + has 'foo' => ( is => 'rw', default => [] ); is actually illegal in Moose. Instead, what you really want is this: - has 'foo' => (is => 'rw', default => sub { [] }); + has 'foo' => ( is => 'rw', default => sub { [] } ); This ensures that each instance of this class will get its own ARRAY ref in the C slot. @@ -136,7 +136,7 @@ the subroutine is executed (to get the default value), we pass in the instance where the slot will be stored. This can come in quite handy at times, as illustrated above, with this code: - default => sub { BinaryTree->new(parent => $_[0]) }, + default => sub { BinaryTree->new( parent => $_[0] ) }, The default value being generated is a new C instance for the C (or C) slot. Here we set up the correct relationship by passing @@ -173,10 +173,10 @@ the parental relationships that we need. We could write our own accessors, but that would require us to implement all those features we got automatically (type constraints, lazy initialization, and so on). Instead, we use method modifiers again: - + before 'right', 'left' => sub { - my ($self, $tree) = @_; - $tree->parent($self) if defined $tree; + my ( $self, $tree ) = @_; + $tree->parent($self) if defined $tree; }; This is a C modifier, just like we saw in the second recipe, but with diff --git a/lib/Moose/Cookbook/Basics/Recipe5.pod b/lib/Moose/Cookbook/Basics/Recipe5.pod index 17fa51d..1526969 100644 --- a/lib/Moose/Cookbook/Basics/Recipe5.pod +++ b/lib/Moose/Cookbook/Basics/Recipe5.pod @@ -10,61 +10,61 @@ Moose::Cookbook::Basics::Recipe5 - More subtypes, coercion in a B class package Request; use Moose; use Moose::Util::TypeConstraints; - + use HTTP::Headers (); use Params::Coerce (); use URI (); - + subtype 'Header' => as 'Object' => where { $_->isa('HTTP::Headers') }; - + coerce 'Header' => from 'ArrayRef' - => via { HTTP::Headers->new( @{ $_ } ) } + => via { HTTP::Headers->new( @{$_} ) } => from 'HashRef' - => via { HTTP::Headers->new( %{ $_ } ) }; - + => via { HTTP::Headers->new( %{$_} ) }; + subtype 'Uri' => as 'Object' => where { $_->isa('URI') }; - + coerce 'Uri' => from 'Object' - => via { $_->isa('URI') - ? $_ - : Params::Coerce::coerce( 'URI', $_ ) } + => via { $_->isa('URI') + ? $_ + : Params::Coerce::coerce( 'URI', $_ ); } => from 'Str' => via { URI->new( $_, 'http' ) }; - + subtype 'Protocol' - => as Str + => as 'Str' => where { /^HTTP\/[0-9]\.[0-9]$/ }; - - has 'base' => (is => 'rw', isa => 'Uri', coerce => 1); - has 'uri' => (is => 'rw', isa => 'Uri', coerce => 1); - has 'method' => (is => 'rw', isa => 'Str'); - has 'protocol' => (is => 'rw', isa => 'Protocol'); + + has 'base' => ( is => 'rw', isa => 'Uri', coerce => 1 ); + has 'uri' => ( is => 'rw', isa => 'Uri', coerce => 1 ); + has 'method' => ( is => 'rw', isa => 'Str' ); + has 'protocol' => ( is => 'rw', isa => 'Protocol' ); has 'headers' => ( is => 'rw', isa => 'Header', coerce => 1, - default => sub { HTTP::Headers->new } + default => sub { HTTP::Headers->new } ); =head1 DESCRIPTION -This recipe introduces the idea of type coercions, and the C -keyword. Coercions can be attached to existing type constraints, -and can be used to transform input of one type into input of another -type. This can be an extremely powerful tool if used correctly, which -is why it is off by default. If you want your accessor to attempt -a coercion, you must specifically ask for it with the B option. +This recipe introduces the idea of type coercions, and the C +keyword. Coercions can be attached to existing type constraints, and +can be used to transform input of one type into input of another +type. This can be an extremely powerful tool if used correctly, which +is why it is off by default. If you want your accessor to attempt a +coercion, you must specifically ask for it with the B option. -Now, onto the coercions. +Now, onto the coercions. -First we need to create a subtype to attach our coercion to. Here we -create a basic I
subtype, which matches any instance of the +First we need to create a subtype to attach our coercion to. Here we +create a basic I
subtype, which matches any instance of the class B: subtype 'Header' @@ -74,60 +74,60 @@ class B: The simplest thing from here would be create an accessor declaration like this: - has 'headers' => ( + has 'headers' => ( is => 'rw', isa => 'Header', - default => sub { HTTP::Headers->new } + default => sub { HTTP::Headers->new } ); -We would then have a self-validating accessor whose default value is -an empty instance of B. This is nice, but it is not +We would then have a self-validating accessor whose default value is +an empty instance of B. This is nice, but it is not ideal. The constructor for B accepts a list of key-value pairs -representing the HTTP header fields. In Perl, such a list could -easily be stored in an ARRAY or HASH reference. We would like our -class's interface to be able to accept this list of key-value pairs -in place of the B instance, and just DWIM. This is where +representing the HTTP header fields. In Perl, such a list could easily +be stored in an ARRAY or HASH reference. We would like our class's +interface to be able to accept this list of key-value pairs in place +of the B instance, and just DWIM. This is where coercion can help. First, let's declare our coercion: coerce 'Header' => from 'ArrayRef' - => via { HTTP::Headers->new( @{ $_ } ) } + => via { HTTP::Headers->new( @{$_} ) } => from 'HashRef' - => via { HTTP::Headers->new( %{ $_ } ) }; + => via { HTTP::Headers->new( %{$_} ) }; We first tell it that we are attaching the coercion to the 'Header' -subtype. We then give it a set of C clauses which map other -subtypes to coercion routines (through the C keyword). Fairly -simple really; however, this alone does nothing. We have to tell -our attribute declaration to actually use the coercion, like so: +subtype. We then give it a set of C clauses which map other +subtypes to coercion routines (through the C keyword). Fairly +simple really; however, this alone does nothing. We have to tell our +attribute declaration to actually use the coercion, like so: - has 'headers' => ( + has 'headers' => ( is => 'rw', isa => 'Header', coerce => 1, - default => sub { HTTP::Headers->new } + default => sub { HTTP::Headers->new } ); This will coerce any B or B which is passed into the C accessor into an instance of B. So the the following lines of code are all equivalent: - $foo->headers(HTTP::Headers->new(bar => 1, baz => 2)); - $foo->headers([ 'bar', 1, 'baz', 2 ]); - $foo->headers({ bar => 1, baz => 2 }); + $foo->headers( HTTP::Headers->new( bar => 1, baz => 2 ) ); + $foo->headers( [ 'bar', 1, 'baz', 2 ] ); + $foo->headers( { bar => 1, baz => 2 } ); -As you can see, careful use of coercions can produce a very open -interface for your class, while still retaining the "safety" of -your type constraint checks. +As you can see, careful use of coercions can produce a very open +interface for your class, while still retaining the "safety" of your +type constraint checks. -Our next coercion takes advantage of the power of CPAN to handle -the details of our coercion. In this particular case it uses the +Our next coercion takes advantage of the power of CPAN to handle the +details of our coercion. In this particular case it uses the L module, which fits in rather nicely with L. -Again, we create a simple subtype to represent instances of the -B class: +Again, we create a simple subtype to represent instances of the B +class: subtype 'Uri' => as 'Object' @@ -137,50 +137,50 @@ Then we add the coercion: coerce 'Uri' => from 'Object' - => via { $_->isa('URI') - ? $_ - : Params::Coerce::coerce( 'URI', $_ ) } + => via { $_->isa('URI') + ? $_ + : Params::Coerce::coerce( 'URI', $_ ); } => from 'Str' => via { URI->new( $_, 'http' ) }; -The first C clause we introduce is for the 'Object' subtype. An 'Object' -is simply any Ced value. This means that if the coercion encounters -another object, it should use this clause. Now we look at the C block. -First it checks to see if the object is a B instance. Since the coercion -process occurs prior to any type constraint checking, it is entirely possible -for this to happen, and if it does happen, we simply want to pass the instance -on through. However, if it is not an instance of B, then we need to coerce -it. This is where L can do its magic, and we can just use its -return value. Simple really, and much less work since we used a module from CPAN -:) - -The second C clause is attached to the 'Str' subtype, and -illustrates how coercions can also be used to handle certain -'default' behaviors. In this coercion, we simple take any string -and pass it to the B constructor along with the default -'http' scheme type. - -And of course, our coercions do nothing unless they are told to, -like so: - - has 'base' => (is => 'rw', isa => 'Uri', coerce => 1); - has 'uri' => (is => 'rw', isa => 'Uri', coerce => 1); - -As you can see, re-using the coercion allows us to enforce a +The first C clause we introduce is for the 'Object' subtype. An +'Object' is simply any Ced value. This means that if the +coercion encounters another object, it should use this clause. Now we +look at the C block. First it checks to see if the object is a +B instance. Since the coercion process occurs prior to any type +constraint checking, it is entirely possible for this to happen, and +if it does happen, we simply want to pass the instance on +through. However, if it is not an instance of B, then we need to +coerce it. This is where L can do its magic, and we +can just use its return value. Simple really, and much less work since +we used a module from CPAN :) + +The second C clause is attached to the 'Str' subtype, and +illustrates how coercions can also be used to handle certain 'default' +behaviors. In this coercion, we simple take any string and pass it to +the B constructor along with the default 'http' scheme type. + +And of course, our coercions do nothing unless they are told to, like +so: + + has 'base' => ( is => 'rw', isa => 'Uri', coerce => 1 ); + has 'uri' => ( is => 'rw', isa => 'Uri', coerce => 1 ); + +As you can see, re-using the coercion allows us to enforce a consistent and very flexible API across multiple accessors. =head1 CONCLUSION -This recipe illustrated the power of coercions to build a more -flexible and open API for your accessors, while still retaining -all the safety that comes from using Moose's type constraints. -Using coercions it becomes simple to manage (from a single -location) a consistent API not only across multiple accessors, -but across multiple classes as well. +This recipe illustrated the power of coercions to build a more +flexible and open API for your accessors, while still retaining all +the safety that comes from using Moose's type constraints. Using +coercions it becomes simple to manage (from a single location) a +consistent API not only across multiple accessors, but across multiple +classes as well. -In the next recipe, we will introduce roles, a concept originally -borrowed from Smalltalk, which made its way into Perl 6, and -now into Moose. +In the next recipe, we will introduce roles, a concept originally +borrowed from Smalltalk, which made its way into Perl 6, and now into +Moose. =head1 AUTHOR diff --git a/lib/Moose/Cookbook/Basics/Recipe7.pod b/lib/Moose/Cookbook/Basics/Recipe7.pod index fdb0f1c..eda3c63 100644 --- a/lib/Moose/Cookbook/Basics/Recipe7.pod +++ b/lib/Moose/Cookbook/Basics/Recipe7.pod @@ -10,8 +10,8 @@ Moose::Cookbook::Basics::Recipe7 - Making Moose fast with immutable package Point; use Moose; - has 'x' => (isa => 'Int', is => 'ro'); - has 'y' => (isa => 'Int', is => 'rw'); + has 'x' => ( isa => 'Int', is => 'ro' ); + has 'y' => ( isa => 'Int', is => 'rw' ); __PACKAGE__->meta->make_immutable;