From: Jesse Luehrs Date: Mon, 25 Apr 2011 06:11:00 +0000 (-0500) Subject: don't close over attrs in the constructor unless necessary X-Git-Tag: 2.0100~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3eeaf0817b310e3f973f550bd27ae4e9e41947c7;p=gitmo%2FMoose.git don't close over attrs in the constructor unless necessary --- diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 3a2ebe2..e7b9da2 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -449,7 +449,7 @@ sub _inline_triggers { push @trigger_calls, 'if (exists $params->{\'' . $init_arg . '\'}) {', - '$attrs->[' . $i . ']->trigger->(', + '$triggers->[' . $i . ']->(', '$instance,', $attr->_inline_instance_get('$instance') . ',', ');', diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index 6b1d7c2..ca47dca 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -5,6 +5,7 @@ use strict; use warnings; use Carp (); +use List::MoreUtils 'any'; use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr'; use Try::Tiny; @@ -56,6 +57,10 @@ sub _eval_environment { my $attrs = $self->_attributes; my $defaults = [map { $_->default } @$attrs]; + my $triggers = [ + map { $_->can('has_trigger') && $_->has_trigger ? $_->trigger : undef } + @$attrs + ]; # We need to check if the attribute ->can('type_constraint') # since we may be trying to immutabilize a Moose meta class, @@ -82,8 +87,11 @@ sub _eval_environment { return { '$meta' => \$self, - '$attrs' => \$attrs, + ((any { defined && $_->has_initializer } @$attrs) + ? ('$attrs' => \$attrs) + : ()), '$defaults' => \$defaults, + '$triggers' => \$triggers, '@type_constraints' => \@type_constraints, '@type_coercions' => \@type_coercions, '@type_constraint_bodies' => \@type_constraint_bodies,