Open In App
Related Articles

Perl | Methods in OOPs

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. Example: 

Perl

sub Student_data
{
    my $self = shift;
       
    # Calculating the result
    my $result = $self->{'Marks_obtained'} /
                 $self->{'Total_marks'};
                    
    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. Example: 

Perl

# 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'} /
                 $self->{'petrol_consumed'};
                    
    print "The mileage by your vehicle is: $result\n";
     
}
 
# Object creation and method calling
my $ob1 = vehicle -> set_mileage(2550, 170);
$ob1->get_mileage();

                    

Output:

In Perl, methods are functions that are associated with an object or a class. Methods provide a way to interact with the properties and behavior of an object, and can be used to modify or retrieve information from an object.

Methods in Perl are defined as subroutines that take the object or class as their first argument. This argument is usually named $self for object methods or $class for class methods. Methods can then access the properties of the object or class using the $self or $class variable.

Here is an example of how to define and use methods in Perl:

Perl

#!/usr/bin/perl
# your code here
# Define a class called "Person"
package Person;
 
# Define the constructor method for the class
sub new {
    my $class = shift;
    my $self = {
        name => shift,
        age => shift,
        gender => shift,
    };
    bless $self, $class;
    return $self;
}
 
# Define a method for the class that returns the person's name
sub get_name {
    my ($self) = @_;
    return $self->{name};
}
 
# Define a method for the class that sets the person's name
sub set_name {
    my ($self, $name) = @_;
    $self->{name} = $name;
}
 
# Create an object of the "Person" class
my $person = new Person("John", 25, "Male");
 
# Get the person's name using the "get_name" method
my $name = $person->get_name();
print "Person's name: $name\n";
 
# Set the person's name using the "set_name" method
$person->set_name("Jane");
 
# Get the person's name again using the "get_name" method
$name = $person->get_name();
print "Person's new name: $name\n";

                    

Output
Person's name: John
Person's new name: Jane

In this example, we define a class called “Person” and two methods for the class: get_name and set_name. get_name returns the person’s name, and set_name sets the person’s name to a new value.

We then create an object of the “Person” class, and use the get_name method to get the person’s name and print it to the console. We then use the set_name method to set the person’s name to “Jane”, and use the get_name method again to get the person’s new name and print it to the console.



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads