package Foo; use strict; use Hash::Util qw( lock_keys ); my @attrs = qw( name alias race ); sub new { my $class = shift; my %args = @_; my $self = bless {}, $class; lock_keys(%$self, @attrs); foreach my $k ( @attrs ) { if ( exists $args{$k} ) { $self->{$k} = $args{$k}; } } return $self; } package main; my $c = Foo->new(name => 'Aragorn', alias => 'Strider', race => 'Human'); print "name: $c->{name}\n"; print "job: $c->{job}\n"; #### $ perl test.pl name: Aragorn Attempt to access disallowed key 'job' in a restricted hash at test.pl line 26.