use fields qw/attr1 attr2/; sub new { # ... my __PACKAGE__ $self = fields::new($pkg); # construct using fields::new $self->{attr1} = "foo"; # ok $self->{attttr2} = "bar"; # compile time error } sub some_method { my __PACKAGE__ $self = shift; # use __PACKAGE__ or any class name to tell perl what class $self is $self->{foo}; # because we told it to make compile time checks based on __PACKAGE__'s fields this will be checked at compile time }