From: Dave Rolsky Date: Sat, 14 Feb 2009 15:17:08 +0000 (+0000) Subject: Clarify that default & builder is called as a method with no additional params X-Git-Tag: 0.70~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9e8cb6f7a1c1bf4beca868b38fb1e70a7f6552d3;p=gitmo%2FMoose.git Clarify that default & builder is called as a method with no additional params --- diff --git a/lib/Moose/Manual/Attributes.pod b/lib/Moose/Manual/Attributes.pod index 149c0b9..45c9f3e 100644 --- a/lib/Moose/Manual/Attributes.pod +++ b/lib/Moose/Manual/Attributes.pod @@ -206,10 +206,22 @@ reference will be called as a method on the object. 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. Laziness is covered in the next section. +When you provide a C 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 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. 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 @@ -255,6 +267,9 @@ organization. We strongly recommend that you use a C instead of a C for anything beyond the most trivial default. +A C, just like a C, is called as a method on the +object with no additional parameters. + =head3 Builders allow subclassing Because the C is called I, it goes through Perl's