[Moose-commits] r7677 - Moose/trunk/lib/Moose/Manual

autarch at code2.0beta.co.uk autarch at code2.0beta.co.uk
Sat Feb 14 15:17:08 GMT 2009


Author: autarch
Date: 2009-02-14 07:17:08 -0800 (Sat, 14 Feb 2009)
New Revision: 7677

Modified:
   Moose/trunk/lib/Moose/Manual/Attributes.pod
Log:
Clarify that default & builder is called as a method with no additional params


Modified: Moose/trunk/lib/Moose/Manual/Attributes.pod
===================================================================
--- Moose/trunk/lib/Moose/Manual/Attributes.pod	2009-02-14 15:13:49 UTC (rev 7676)
+++ Moose/trunk/lib/Moose/Manual/Attributes.pod	2009-02-14 15:17:08 UTC (rev 7677)
@@ -206,11 +206,23 @@
 This is dumb example, but it illustrates the point that the subroutine
 will be called for every new object created.
 
-Of course, if it's called during object construction, it may be called
-before other attributes have been set. If your default is dependent on
-other parts of the object's state, you can make the attribute
-C<lazy>. Laziness is covered in the next section.
+When you provide a C<default> subroutine reference, it is called as a
+method on the object, with no additional parameters:
 
+  has 'size' => (
+      is => 'rw',
+      default => sub {
+          my $self = shift;
+
+          return $self->height > 200 ? 'big' : 'average';
+      },
+  );
+
+When the C<default> is called during object construction, it may be
+called before other attributes have been set. If your default is
+dependent on other parts of the object's state, you can make the
+attribute C<lazy>. Laziness is covered in the next section.
+
 If you want to use a reference of any sort as the default value, you
 must return it from a subroutine. This is necessary because otherwise
 Perl would instantiate the reference exactly once, and it would be
@@ -255,6 +267,9 @@
 We strongly recommend that you use a C<builder> instead of a
 C<default> for anything beyond the most trivial default.
 
+A C<builder>, just like a C<default>, is called as a method on the
+object with no additional parameters.
+
 =head3 Builders allow subclassing
 
 Because the C<builder> is called I<by name>, it goes through Perl's




More information about the Moose-commits mailing list