package attribute; use strict; use warnings; sub import { my ($self, $sub, $names) = @_; my $class = caller(); no strict 'refs'; # set the general method *{$class . '::_attribute_ref' } = $sub; for my $name (@$names) { my $string = $class . '::' . $name; *{$string} = sub { my $self = shift; # set method for each attribute return ${ $self->_attribute_ref($name) } unless @_; ${ $self->_attribute_ref($name) } = shift; }; } } 1;