foo.pm: package foo; use Hash::Util; use strict; use warnings; sub new { my $self = {}; $self->{XBCDE} = 1; $self->{AXCDE} = 2; $self->{ABXDE} = 3; $self->{ABCXE} = 4; $self->{ABCDX} = 5; bless($self); Hash::Util::lock_keys(%{$self}); return $self; } sub bar_1 { my $self = shift; $self->{AXCDE} = 10; } sub bar_2 { my $self = shift; print $self->{AXCDE}; } sub bar_3 { my $self = shift; $self->{XXXXX} = 10; } sub bar_4 { my $self = shift; print $self->{XXXXX}; } 1; foo.pl: use foo; use strict; use warnings; my $foo = new foo; $foo->bar_1; #okay as the key exists $foo->bar_2; #okay as the key exists $foo->bar_3; #no good, comment out this, and try for the second time $foo->bar_4; #no good, comment out this, and try for the third time