From: Matt S Trout Date: Sat, 21 Jul 2012 14:38:07 +0000 (+0000) Subject: remove quote_sub from SYNOPSIS and has docs, expand Sub::Quote section X-Git-Tag: v1.000001~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c9f73a6392b8a42b2b066a1994a9d8971ac8bca2;p=gitmo%2FMoo.git remove quote_sub from SYNOPSIS and has docs, expand Sub::Quote section --- diff --git a/Changes b/Changes index f990edc..87d9283 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - remove quote_sub from SYNOPSIS and has docs, expand Sub::Quote section - localize @_ when inlining quote_sub'ed isa checks (fixes lazy+isa+default) - ensure constructor gets regenerated if forced early by metaclass inflation diff --git a/lib/Moo.pm b/lib/Moo.pm index ed2549e..9ccab7f 100644 --- a/lib/Moo.pm +++ b/lib/Moo.pm @@ -185,7 +185,6 @@ Moo - Minimalist Object Orientation (with Moose compatiblity) package Cat::Food; use Moo; - use Sub::Quote; sub feed_lion { my $self = shift; @@ -207,7 +206,7 @@ Moo - Minimalist Object Orientation (with Moose compatiblity) has pounds => ( is => 'rw', - isa => quote_sub q{ die "$_[0] is too much cat food!" unless $_[0] < 15 }, + isa => sub { die "$_[0] is too much cat food!" unless $_[0] < 15 }, ); 1; @@ -418,7 +417,7 @@ Takes a coderef which is meant to validate the attribute. Unlike L Moo does not include a basic type system, so instead of doing C<< isa => 'Num' >>, one should do - isa => quote_sub q{ + isa => sub { die "$_[0] is not a number!" unless looks_like_number $_[0] }, @@ -451,7 +450,7 @@ make L happy is fine. Takes a coderef which is meant to coerce the attribute. The basic idea is to do something like the following: - coerce => quote_sub q{ + coerce => sub { $_[0] + 1 unless $_[0] % 2 }, @@ -605,6 +604,38 @@ L allows us to create coderefs that are "inlineable," giving us a handy, XS-free speed boost. Any option that is L aware can take advantage of this. +To do this, you can write + + use Moo; + use Sub::Quote; + + has foo => ( + is => quote_sub(q{ die "Not <3" unless $_[0] < 3 }) + ); + +which will be inlined as + + do { + local @_ = ($_[0]->{foo}); + die "Not <3" unless $_[0] < 3; + } + +or to avoid localizing @_, + + has foo => ( + is => quote_sub(q{ my ($val) = @_; die "Not <3" unless $val < 3 }) + ); + +which will be inlined as + + do { + my ($val) = ($_[0]->{foo}); + die "Not <3" unless $val < 3; + } + +See L for more information, including how to pass lexical +captures that will also be compiled in to the subroutine. + =head1 INCOMPATIBILITIES WITH MOOSE There is no built in type system. C is verified with a coderef, if you