Do not recreate hash on every construction
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Method / Constructor.pm
index 3c228b0..1eceed7 100644 (file)
@@ -5,7 +5,6 @@ use Moose::Role;
 use namespace::autoclean;
 
 use B ();
-use Carp ();
 
 around '_generate_BUILDALL' => sub {
     my $orig = shift;
@@ -21,13 +20,16 @@ around '_generate_BUILDALL' => sub {
         map  { $_->init_arg() } @{ $self->_attributes() }
     );
 
-    $source .= <<"EOF";
-my \%attrs = (@attrs);
-
-my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
+    my $MY = 'my';
+    if ($] >= 5.009004) {
+        $source .= "use feature 'state';\n";
+        $MY = 'state';
+    }
 
-if (\@bad) {
-    Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
+    $source .= <<"EOF";
+$MY \$attrs = { @attrs };
+if (my \@bad = sort grep { ! \$attrs->{\$_} } keys %\$params) {
+    Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
 }
 EOF