package MooseX::StrictConstructor::Trait::Class;
-use strict;
-use warnings;
+use Moose::Role;
+
+use namespace::autoclean;
use B ();
-use Carp ();
-use Moose::Role;
+my %pkg_attrs;
+
+around new_object => sub {
+ my $orig = shift;
+ my $self = shift;
+ my $params = @_ == 1 ? $_[0] : {@_};
+ my $instance = $self->$orig(@_);
+
+ my $attrs =
+ $pkg_attrs{ref($instance)} ||= {
+ __INSTANCE__ => 1,
+ map { $_ => 1 }
+ grep {defined}
+ map { $_->init_arg() } $self->get_all_attributes()
+ };
+
+ if (my @bad = sort grep { !$attrs->{$_} } keys %$params) {
+ $self->throw_error(
+ "Found unknown attribute(s) init_arg passed to the constructor: @bad"
+ );
+ }
+
+ return $instance;
+};
around '_inline_BUILDALL' => sub {
my $orig = shift;
map { $_->init_arg() } $self->get_all_attributes()
);
+ my $MY = 'my';
+ if ($] >= 5.009004) {
+ push @source, "use feature 'state';";
+ $MY = 'state';
+ }
+
return (
@source,
- 'my %attrs = (' . join(' ', @attrs) . ');',
- 'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
- 'if (@bad) {',
- 'Carp::confess "Found unknown attribute(s) passed to the constructor: @bad";',
+ $MY.' $attrs = {' . ( join ' ', @attrs ) . '};',
+ 'if (my @bad = sort grep { !$attrs->{$_} } keys %$params) {',
+ 'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");',
'}',
);
-};
-
-no Moose::Role;
+} if $Moose::VERSION >= 1.9900;
1;