Steven's Blog

A Dream Land of Peace!

Perl中的getter-setter方法

As well as getting the value of an attribute, you may well want to set or change it. The syntax you’ll use is as follows:

1
2
3
print "Old address: ", $object->address(), "\n";
$object->address("Campus Mirabilis, Pisa, Italy");
print "New address: ", $object->address(), "\n";

This kind of accessor is called a get-set method because you can use it to both get and set the attribute. Turning your current read–only accessors into accessors that can also set the value is simple. Let’s create a get–set method for address():

1
2
3
4
5
6
7
8
9
10
sub address {
	my $self = shift;
	# Receive more data
	my $data = shift;

	# Set the address if there's any data there.
	$self->{address} = $data if defined $data;
	return $self->{address};

}

If you don’t particularly want to trap calling the method as a class method (since it’ll generate an error when we try to access the hash entry anyway), you can write really miniature get–set methods like the following:

1
2
3
4
5
sub address { $_[0]->{address } = $_[1] if defined $_[1]; $_[0]->{address } }

sub lastname { $_[0]->{lastname } = $_[1] if defined $_[1]; $_[0]->{lastname } }

sub firstname { $_[0]->{firstname} = $_[1] if defined $_[1]; $_[0]->{firstname} }