From: Stevan Little Date: Fri, 1 Sep 2006 14:46:45 +0000 (+0000) Subject: recipe touchups X-Git-Tag: 0_12~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=266fb1a5f7ec0ebba031ecba02595cb04eea0a3c;p=gitmo%2FMoose.git recipe touchups --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 794719d..6fc288f 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -219,7 +219,12 @@ sub _load_all_classes { # loaded in the symbol table next if _is_class_already_loaded($super); # otherwise require it ... - ($super->require) + # NOTE: + # just in case the class we are + # loading has a locally defined + # &require, we make sure that we + # use the on in UNIVERSAL + ($super->UNIVERSAL::require) || confess "Could not load module '$super' because : " . $UNIVERSAL::require::ERROR; } } diff --git a/lib/Moose/Cookbook/Recipe1.pod b/lib/Moose/Cookbook/Recipe1.pod index 0e5fac8..6392fd4 100644 --- a/lib/Moose/Cookbook/Recipe1.pod +++ b/lib/Moose/Cookbook/Recipe1.pod @@ -8,8 +8,6 @@ Moose::Cookbook::Recipe1 - The (always classic) B example. =head1 SYNOPSIS package Point; - use strict; - use warnings; use Moose; has 'x' => (isa => 'Int', is => 'ro'); @@ -22,8 +20,6 @@ Moose::Cookbook::Recipe1 - The (always classic) B example. } package Point3D; - use strict; - use warnings; use Moose; extends 'Point'; @@ -43,8 +39,8 @@ 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. -Of course we always use C and C (don't forget -that a kitten will die if you don't) and then we C. +Moose now handles turning C and C on for you, so +all you need do is say C, and no kittens will die. By loading Moose, we are enabeling the Moose "environment" to be loaded within our package. This means that we export some functions diff --git a/lib/Moose/Cookbook/Recipe2.pod b/lib/Moose/Cookbook/Recipe2.pod index 2b8c4d9..73088cb 100644 --- a/lib/Moose/Cookbook/Recipe2.pod +++ b/lib/Moose/Cookbook/Recipe2.pod @@ -8,8 +8,6 @@ Moose::Cookbook::Recipe2 - A simple B example =head1 SYNOPSIS package BankAccount; - use strict; - use warnings; use Moose; has 'balance' => (isa => 'Int', is => 'rw', default => 0); @@ -28,8 +26,6 @@ Moose::Cookbook::Recipe2 - A simple B example } package CheckingAccount; - use strict; - use warnings; use Moose; extends 'BankAccount'; diff --git a/lib/Moose/Cookbook/Recipe3.pod b/lib/Moose/Cookbook/Recipe3.pod index 19e7d0e..6fe43b1 100644 --- a/lib/Moose/Cookbook/Recipe3.pod +++ b/lib/Moose/Cookbook/Recipe3.pod @@ -8,8 +8,6 @@ Moose::Cookbook::Recipe3 - A lazy B example =head1 SYNOPSIS package BinaryTree; - use strict; - use warnings; use Moose; has 'node' => (is => 'rw', isa => 'Any'); @@ -125,15 +123,13 @@ in a CODE ref. In the second recipe the B's C slot had a default value of C<0>. Since Perl will copy strings and numbers by value, this was all we had to say. But for any other item -(ARRAY ref, HASH ref, object instance, etc) Perl will copy by -reference. This means that if I were to do this: +(ARRAY ref, HASH ref, object instance, etc) you would need to +wrap this into a CODE reference, so this: has 'foo' => (is => 'rw', default => []); -Every single instance of that class would get a pointer to the -same ARRAY ref in their C slot. This is almost certainly -B the behavior you intended. So, the solution is to wrap -these defaults into an anon-sub, like so: +is actually illegal in Moose. Instead, what you really want is +to do this: has 'foo' => (is => 'rw', default => sub { [] }); diff --git a/lib/Moose/Cookbook/Recipe4.pod b/lib/Moose/Cookbook/Recipe4.pod index 5ae2daa..ac499d3 100644 --- a/lib/Moose/Cookbook/Recipe4.pod +++ b/lib/Moose/Cookbook/Recipe4.pod @@ -8,8 +8,6 @@ Moose::Cookbook::Recipe4 - Subtypes, and modeling a simple B class hier =head1 SYNOPSIS package Address; - use strict; - use warnings; use Moose; use Moose::Util::TypeConstraints; @@ -37,8 +35,6 @@ Moose::Cookbook::Recipe4 - Subtypes, and modeling a simple B class hier has 'zip_code' => (is => 'rw', isa => 'USZipCode'); package Company; - use strict; - use warnings; use Moose; use Moose::Util::TypeConstraints; @@ -67,8 +63,6 @@ Moose::Cookbook::Recipe4 - Subtypes, and modeling a simple B class hier }; package Person; - use strict; - use warnings; use Moose; has 'first_name' => (is => 'rw', isa => 'Str', required => 1); @@ -88,8 +82,6 @@ Moose::Cookbook::Recipe4 - Subtypes, and modeling a simple B class hier } package Employee; - use strict; - use warnings; use Moose; extends 'Person'; diff --git a/lib/Moose/Cookbook/Recipe5.pod b/lib/Moose/Cookbook/Recipe5.pod index e352246..47ac281 100644 --- a/lib/Moose/Cookbook/Recipe5.pod +++ b/lib/Moose/Cookbook/Recipe5.pod @@ -8,8 +8,6 @@ Moose::Cookbook::Recipe5 - More subtypes, coercion in a B class =head1 SYNOPSIS package Request; - use strict; - use warnings; use Moose; use Moose::Util::TypeConstraints; @@ -165,9 +163,9 @@ and pass it into the B constructor along with the default 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); + + 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. diff --git a/lib/Moose/Cookbook/Recipe6.pod b/lib/Moose/Cookbook/Recipe6.pod index 88911a3..7424dfe 100644 --- a/lib/Moose/Cookbook/Recipe6.pod +++ b/lib/Moose/Cookbook/Recipe6.pod @@ -8,8 +8,6 @@ Moose::Cookbook::Recipe6 - The Moose::Role example =head1 SYNOPSIS package Eq; - use strict; - use warnings; use Moose::Role; requires 'equal_to'; @@ -20,8 +18,6 @@ Moose::Cookbook::Recipe6 - The Moose::Role example } package Comparable; - use strict; - use warnings; use Moose::Role; with 'Eq'; @@ -54,15 +50,11 @@ Moose::Cookbook::Recipe6 - The Moose::Role example } package Printable; - use strict; - use warnings; use Moose::Role; requires 'to_string'; package US::Currency; - use strict; - use warnings; use Moose; with 'Comparable', 'Printable';