Search⌘ K

Setters and Getters

Explore how to implement setter and getter methods in Perl packages to manage object member variables. This lesson helps you understand assigning values to object properties using setters and retrieving them with getters, exemplified through a Shape package. Gain practical skills to manipulate object data in Perl.

We'll cover the following...

Setters

In order to assign a value to a member variable of an object after it has been instantiated, we define a setter. Let’s implement setter methods for the package Shape to set the name and sides of the shape.

Perl
package Shape;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
sides => shift,
};
bless $self, $class;
return $self;
}
sub Description {
my ($self) = @_;
print "A $self->{name} has $self->{sides} sides.\n";
}
sub setName {
my ($self, $value) = @_;
$self->{name} = $value;
return $self->{name};
}
sub setSides {
my ($self, $value) = @_;
$self->{sides} = $value;
return $self->{sides};
}
1;
$Square = new Shape("square", 4);
$Square->Description();
$Square->setName("Triangle");
$Square->setSides("3");
$Square->Description();

Explanation

In the above code snippet, setName and setSides are setter methods for the package Shape.

  • In lines 21-25, the $value variable is used to assign a value to the member variable name. This is essentially done in the line $self->{name} = $value;.

  • In line 38, this setter is called with the argument Triangle to set the name of the $square object to Triangle.

  • Similarly, in lines 27-31, the $value variable is used to assign a value to the member variable sides. This is essentially done in the line $self->{sides} = $value;.

  • In line 39, this setter is called with the argument 3 to set the sides of the $square object to 3.

Getters

In order to retrieve the value from a member variable of an object, we define a getter. Let’s implement getter methods for the package Shape to get the name and sides of the shape.

Perl
package Shape;
sub new { # constructor
my $class = shift;
my $self = {
# member variables
name => shift,
sides => shift,
};
bless $self, $class;
return $self;
}
sub Description {
my ($self) = @_;
print "A $self->{name} has $self->{sides} sides.\n";
}
sub setName {
my ($self, $value) = @_;
$self->{name} = $value;
return $self->{name};
}
sub setSides {
my ($self, $value) = @_;
$self->{sides} = $value;
return $self->{sides};
}
sub getName {
my ($self) = @_;
return $self->{name};
}
sub getSides {
my ($self) = @_;
return $self->{sides};
}
1;
$Square = new Shape("square", 4);
$shapeName = $Square->getName();
$shapeSides = $Square->getSides();
print "Shape name : $shapeName and Shape Sides : $shapeSides";

Explanation

In the above code snippet, getName and getSides are getter methods for the shape package.

  • In lines 33-36, the line return $self->{name}; returns the name of our object.

  • This getter is then called in the line 47, and the value returned is stored in the variable $shapeName.

  • Similarly, in lines 38-41, the line return $self->{sides}; returns the sides of our object.

  • In line 48, this getter is called, and the value returned is stored in the variable $shapeSides.