Skip to content
Related Articles

Related Articles

Improve Article

Perl | Methods in OOPs

  • Difficulty Level : Hard
  • Last Updated : 11 Jul, 2019

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 sub keyword.

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 virtual method.

A 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.

A 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.


sub Student_data
    my $self = shift
    # Calculating the result 
    my $result = $self->{'Marks_obtained'} / 
    print "Marks scored by the student are: $result"

Methods in Object-Oriented Programming require parentheses to hold the arguments whereas these methods are invoked with the use of an arrow operator(->).

get-set Methods:
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.


# Declaration and definition of Base class 
use strict; 
use warnings; 
# Creating parent class 
package vehicle; 
# Setter method
sub set_mileage
    # shift will take package name 'vehicle'  
    # and assign it to variable 'class' 
    my $class = shift
    my $self = { 
                'distance'=> shift
                'petrol_consumed'=> shift
    # Bless function to bind object to class 
    bless $self, $class
    # returning object from constructor 
    return $self
# Getter method
sub get_mileage 
    my $self = shift
    # Calculating result 
    my $result = $self->{'distance'} / 
    print "The mileage by your vehicle is: $result\n"
# Object creation and method calling
my $ob1 = vehicle -> set_mileage(2550, 170); 


My Personal Notes arrow_drop_up
Recommended Articles
Page :