
package AutoNewAttr;

my %attr_ok;

sub import {
  my $caller = caller;
  for my $attr (@{"$caller\::ATTRS"}) {
    $attr_ok{$caller}{$attr} = "ok";
  }
  push @{"$caller\::ISA"}, 'AutoNew::New';
}

package AutoNew::New;
use Carp 'croak';

sub new {
  my $class = shift;
  my %ATTR = @_;

  my $attr_ok = $attr_ok{$class};
  if (defined $attr_ok) {
    for my $attr (keys %ATTR) {
      unless ($attr_ok->{$attr}) {
        croak "Unrecognized attribute name '$attr' in $class->new";
      }
    }
  }

  bless \%ATTR => $class;
}

1;
