foreach my $field (qw( foo bar )) { no strict 'refs'; *$field = sub { my $self = shift; $self->{$field} = shift if @_; $self->{$field}; }; }