Combining Getters and Setters That is quite a bit of repetitive typing already, and we haven't even defined get_bar and set_bar. Let's combine them: sub foo { my ($self, $value) = @_; $self->{foo} = $value if defined $value; return $self->{foo}; } Caller says: print $object->foo(); # Prints "new foo". $object->foo("my foo"); # Change foo back.