From: Jesse Luehrs Date: Thu, 21 Apr 2011 05:55:51 +0000 (-0500) Subject: stop using a package global for enums, just close over it X-Git-Tag: 2.0100~57 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c44971f087c3b69a46685ba068b2f1f42120935;p=gitmo%2FMoose.git stop using a package global for enums, just close over it --- diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index caf0ef8..ab6e6e7 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -50,6 +50,12 @@ __PACKAGE__->meta->add_attribute('inlined' => ( predicate => '_has_inlined_type_constraint', )); +__PACKAGE__->meta->add_attribute('inline_environment' => ( + init_arg => 'inline_environment', + accessor => 'inline_environment', + default => sub { {} }, +)); + sub parents { my $self; $self->parent; @@ -252,7 +258,8 @@ sub _actually_compile_type_constraint { if ( $self->has_inlined_type_constraint ) { return eval_closure( - source => 'sub { ' . $self->_inline_check('$_[0]') . ' }', + source => 'sub { ' . $self->_inline_check('$_[0]') . ' }', + environment => $self->inline_environment, ); } diff --git a/lib/Moose/Meta/TypeConstraint/Enum.pm b/lib/Moose/Meta/TypeConstraint/Enum.pm index 65b7753..2a7a5ad 100644 --- a/lib/Moose/Meta/TypeConstraint/Enum.pm +++ b/lib/Moose/Meta/TypeConstraint/Enum.pm @@ -13,19 +13,13 @@ __PACKAGE__->meta->add_attribute('values' => ( accessor => 'values', )); -our %ENUMS; - my $inliner = sub { my $self = shift; my $val = shift; - my $name = $self->name(); - $ENUMS{$name} ||= { map { $_ => 1 } @{ $self->values() } }; - return 'defined(' . $val . ') ' . '&& !ref(' . $val . ') ' - . '&& $' . __PACKAGE__ . '::ENUMS{' . B::perlstring($name) . '}' - . '{' . $val . '}'; + . '&& $enums{' . $val . '}'; }; sub new { @@ -52,6 +46,7 @@ sub new { my %values = map { $_ => 1 } @{ $args{values} }; $args{constraint} = sub { $values{ $_[0] } }; + $args{inline_environment} = { '%enums' => \%values }; my $self = $class->_new(\%args); diff --git a/t/type_constraints/util_std_type_constraints.t b/t/type_constraints/util_std_type_constraints.t index d98f16c..89c35ba 100644 --- a/t/type_constraints/util_std_type_constraints.t +++ b/t/type_constraints/util_std_type_constraints.t @@ -1014,6 +1014,7 @@ sub test_constraint { if ( $type->has_inlined_type_constraint ) { $inlined = eval_closure( source => 'sub { ( ' . $type->_inline_check('$_[0]') . ' ) }', + environment => $type->inline_environment, ); }