From: Jesse Luehrs Date: Thu, 30 Jul 2009 22:42:40 +0000 (-0500) Subject: use around for BUILDARGS in the manual, rather than SUPER X-Git-Tag: 0.89~26 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c30bceb83927b91523f9b07f4fa14b75809a651d;p=gitmo%2FMoose.git use around for BUILDARGS in the manual, rather than SUPER --- diff --git a/lib/Moose/Cookbook/Basics/Recipe10.pod b/lib/Moose/Cookbook/Basics/Recipe10.pod index ed0b7d3..7d18b6f 100644 --- a/lib/Moose/Cookbook/Basics/Recipe10.pod +++ b/lib/Moose/Cookbook/Basics/Recipe10.pod @@ -31,16 +31,17 @@ Moose::Cookbook::Basics::Recipe10 - Using BUILDARGS and BUILD to hook into objec isa => 'Str', ); - sub BUILDARGS { + around BUILDARGS => sub { + my $orig = shift; my $class = shift; if ( @_ == 1 && ! ref $_[0] ) { - return { ssn => $_[0] }; + return $class->$orig(ssn => $_[0]); } else { - return $class->SUPER::BUILDARGS(@_); + return $class->$orig(@_); } - } + }; sub BUILD { my $self = shift; @@ -73,14 +74,14 @@ social security number: The key part of our C is this conditional: if ( @_ == 1 && ! ref $_[0] ) { - return { ssn => $_[0] }; + return $class->$orig(ssn => $_[0]); } By default, Moose constructors accept a list of key-value pairs, or a hash reference. We need to make sure that C<$_[0]> is not a reference before assuming it is a social security number. -We call C<< $class->SUPER::BUILDARGS(@_) >> to handle all the other +We call the original C method to handle all the other cases. You should always do this in your own C methods, since L provides its own C method that handles hash references and a list of key-value pairs. diff --git a/lib/Moose/Manual/Construction.pod b/lib/Moose/Manual/Construction.pod index fe18792..be2d3e2 100644 --- a/lib/Moose/Manual/Construction.pod +++ b/lib/Moose/Manual/Construction.pod @@ -50,18 +50,19 @@ Without a C method, Moose will complain, because it expects a hash or hash reference. We can use the C method to accommodate this calling style: - sub BUILDARGS { + around BUILDARGS => sub { + my $orig = shift; my $class = shift; if ( @_ == 1 && ! ref $_[0] ) { - return { ssn => $_[0] }; + return $class->$orig(ssn => $_[0]); } else { - return $class->SUPER::BUILDARGS(@_); + return $class->$orig(@_); } - } + }; -Note the call to C. This will call the default +Note the call to C<< $class->$orig >>. This will call the default C in L. This method handles distinguishing between a hash reference and a plain hash for you.