5.6.2.2 Defining Methods that Access and Manipulate Fields of an Instance
5.6.2.2 Defining Methods that Access and Manipulate Fields of an Instance
The Person class has only one field: name. In addition to the constructor named make, in the definition of the Person class we have two more subroutines or methods: set_name and get_name. Once an empty instance of class Person is created using the make
method, the set_name method is used to set the name field in this instance. In our example, in the package main, we call the set_name method in the following manner.
$erin -> set_name ("Erin");
This call sets the name field of the instance of Person pointed to by the referencing scalar $erin to "Erin".
The get_name method takes a variable that points to an instance of the Person class and returns the value of the name field. For example, we can get the value of the name property or field of the $erin variable by writing
printf "%-7s\n", $erin -> get_name;
