Methods are used to access and modify the data of an object. These are the entities which are invoked with the use of objects of a class or a package itself. Methods are basically a subroutine in Perl, there is no special identity of a method. Syntax of a method is the same as that of a subroutine. Just like subroutines, methods are declared with the use of
The method takes an object or the package on which it is invoked as its first argument. OOPs uses these methods to manipulate the object’s data and not interact with the object directly, this is done to maintain the security of data to prevent the programmer from changing the data of an object directly. This can be done by using various helper methods, which take the object as argument and store its value into another variable. Further, modifications are performed on the second variable. These modifications don’t affect the object’s data hence making it more secure.
Types of Methods in Perl:
Based on the arguments passed, methods can be classified into two types-
static method and
static method is one in which the first argument passed to the method is the class name. Functionalities of a static method are applied to the whole class because it takes the name of the class as an argument. These methods are also called class methods. Since most of the methods are in the same class and hence, there is no need to pass the class name as an argument.
Example: Constructors of a class are considered to be static methods.
virtual method is one in which the reference to the object is passed as the first argument to the function. In a virtual function, the first argument is shifted to a local variable and then this value is used as a reference.
Methods in Object-Oriented Programming require parentheses to hold the arguments whereas these methods are invoked with the use of an arrow operator(->).
Methods are used to provide security to the object’s data and hence used with either the object’s reference or the value is stored in some other variable and then used.
get-set methods are used in the OOPs to provide this data security to the objects.
get-method helps to get the current value of the object and the
set value method is used to set a new value to the object.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.