DEATH TO ALL zionist ELLIPSES
[gitmo/Moose.git] / lib / Moose / Cookbook / Meta / Recipe2.pod
index 013ed4c..e94032b 100644 (file)
@@ -22,7 +22,6 @@ Moose::Cookbook::Meta::Recipe2 - A meta-attribute, attributes with labels
 
   package MyApp::Website;
   use Moose;
-  use MyApp::Meta::Attribute::Labeled;
 
   has url => (
       metaclass => 'Labeled',
@@ -39,30 +38,30 @@ Moose::Cookbook::Meta::Recipe2 - A meta-attribute, attributes with labels
   sub dump {
       my $self = shift;
 
-      # iterate over all the attributes in $self
+      my $dump = '';
+
       my %attributes = %{ $self->meta->get_attribute_map };
-      while ( my ( $name, $attribute ) = each %attributes ) {
+      for my $name ( sort keys %attributes ) {
+          my $attribute = $attributes{$name};
 
-          # print the label if available
           if (   $attribute->isa('MyApp::Meta::Attribute::Labeled')
               && $attribute->has_label ) {
-              print $attribute->label;
+              $dump .= $attribute->label;
           }
-
-          # otherwise print the name
           else {
-              print $name;
+              $dump .= $name;
           }
 
-          # print the attribute's value
           my $reader = $attribute->get_read_method;
-          print ": " . $self->$reader . "\n";
+          $dump .= ": " . $self->$reader . "\n";
       }
+
+      return $dump;
   }
 
   package main;
+
   my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
-  $app->dump;
 
 =head1 SUMMARY
 
@@ -108,7 +107,7 @@ you can call methods on it like this:
 
 To add a label to our attributes there are two steps. First, we need a
 new attribute metaclass that can store a label for an
-attribute. Second, we nede to create attributes that use that
+attribute. Second, we need to create attributes that use that
 attribute metaclass.
 
 =head1 RECIPE REVIEW
@@ -136,7 +135,7 @@ Then we need to register our metaclass with Moose:
   sub register_implementation { 'MyApp::Meta::Attribute::Labeled' }
 
 This is a bit of magic that lets us use a short name, "Labeled", when
-referring to our new metaclas.
+referring to our new metaclass.
 
 That was the whole attribute metaclass.
 
@@ -157,7 +156,7 @@ Finally, we use it for an attribute:
       label     => "The site's URL",
   );
 
-This looks like a normal attribute declaraion, except for two things,
+This looks like a normal attribute declaration, except for two things,
 the C<metaclass> and C<label> parameters. The C<metaclass> parameter
 tells Moose we want to use a custom metaclass for this (one)
 attribute. The C<label> parameter will be stored in the meta-attribute
@@ -201,14 +200,15 @@ attribute's label if it has one.
   sub dump {
       my $self = shift;
 
-      # iterate over all the attributes in $self
+      my $dump = '';
+
       my %attributes = %{ $self->meta->get_attribute_map };
-      while ( my ( $name, $attribute ) = each %attributes ) {
+      for my $name ( sort keys %attributes ) {
+          my $attribute = $attributes{$name};
 
-          # print the label if available
           if (   $attribute->isa('MyApp::Meta::Attribute::Labeled')
               && $attribute->has_label ) {
-              print $attribute->label;
+              $dump .= $attribute->label;
           }
 
 This is a bit of defensive code. We cannot depend on every
@@ -218,17 +218,17 @@ superclass could add an attribute without a label.
 
 We also check that the attribute has a label using the predicate we
 defined. We could instead make the label C<required>. If we have a
-label, we print it, otherwise we print the attribute name:
+label, we use it, otherwise we use the attribute name:
 
-          # otherwise print the name
           else {
-              print $name;
+              $dump .= $name;
           }
 
-          # print the attribute's value
           my $reader = $attribute->get_read_method;
-          print ": " . $self->$reader . "\n";
+          $dump .= ": " . $self->$reader . "\n";
       }
+
+      return $dump;
   }
 
 The C<get_read_method> is part of the L<Moose::Meta::Attribute>
@@ -277,5 +277,16 @@ L<http://www.iinteractive.com>
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
+=begin testing
+
+my $app = MyApp::Website->new( url => "http://google.com", name => "Google" );
+is(
+    $app->dump, q{name: Google
+The site's URL: http://google.com
+}, 'got the expected dump value'
+);
+
+=end testing
+
 =cut