Object-Oriented Programming: As the name suggests, object-oriented programming or OOP refers to a language that uses objects in programming. Object-oriented programming aims to implement real world entities such as inheritance, concealment, polymorphism in programming. The main purpose of OOP is to bind data to the functionality that acts on it so that no other part of the code can access the data.

OOPs concept:

  • class
  • Object
  • methods
  • polymorphism
  • inheritance
  • Packaging form
  • Abstraction lets us look at the different characteristics of object-oriented programming languages:

Classes: A class is a user-defined blueprint or prototype from which objects can be created. It represents a set of properties or methods that are common to all objects of a type. In general, a class declaration can include the following components in order:

  1. Class name: Names should begin with an initial letter (capitalized as is the convention).
  2. Superclass (if any) : The name (if any) of the class’s parent (superclass), followed by the keyword “use”.
  3. Constructor (if any) : The constructor in the Perl subroutine returns an object that is an instance of the class. In Perl, the convention is to name the constructor “new”.
  4. Body: The body of the class surrounded by parentheses {}.

Object: It is the basic unit of object-oriented programming and represents real life entities. A typical Perl program creates a lot of objects that you know interact with each other by calling methods. An object includes:

  1. State: Represented by an object’s properties. It also reflects the properties of the object.
  2. Behavior: Represented by methods of an object. It also reflects the response of one object to another.
  3. Unique identity: It gives an object a unique name and enables an object to interact with other objects.

Object example: dog



Methods: A method is a collection of statements that perform some specific task and return the results to the caller. Methods can perform certain tasks without having to return anything. Methods save time and help us reuse code without having to retype it.

Polymorphism: Polymorphism refers to the ability of an OOP programming language to effectively distinguish between entities with the same name. This is done by Perl with the help of the signatures and declarations of these entities.

There are two main types of polymorphism in Perl:

  • Overloading in Perl
  • Rewritten in Perl

Inheritance: Inheritance is an important pillar of OOP(object-oriented programming). This is a mechanism in Perl that allows one class to inherit the functionality (fields and methods) of another class.

Key terms:

  • Superclass: A class that inherits its functionality is called a superclass (or base or superclass).
  • Subclass: A class that inherits another class is called a subclass (or derived, extended, or subclass). In addition to the fields and methods of the superclass, subclasses can add their own fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, that is, when we are creating a new class and we already have a class that contains some of the required code, we can derive a new class from an existing class. By doing so, we can reuse the fields and methods of existing classes.

You can create a class in Perl using the following methods

Encapsulation: and can be inherited by using the “use” keyword.

The syntax is as follows:

use package_name

Encapsulation form: Encapsulation is defined as wrapping data in a single cell. It is a mechanism for binding code together with the data it processes. Another way to think about encapsulation is that it is a protective mask that prevents data from being accessed by code outside the mask.

  • In encapsulation, variables or data of one class are hidden from any other class and can only be accessed through any member function of the class that declares them.
  • As with encapsulation, data in one class is hidden from other classes and is therefore also known as data hiding.
  • Encapsulation can be achieved by declaring all variables in the class private and writing public methods in the class to set and get the values of the variables. Abstract: Data abstraction is an attribute by which only basic details are shown to the user. Normal or non-essential units are not displayed to the user. A car is treated as a car, not as its components. Data abstraction can also be defined as the process of ignoring irrelevant details to identify only the desired characteristics of an object. The properties and behavior of an object distinguish it from other similar types of objects and also help to classify/group objects. Consider a real-life example of a man driving a car. This person only knows that pressing the accelerator will increase the speed of the car, or that pressing the brake will stop the car, but he does not know how the speed actually increases when he presses the accelerator, he does not know the internal mechanisms of the car or the application of the throttle, brake, etc on the car. That’s the abstraction.

For more information on backend development, please refer to: lsbin-it development techniques: https://www.lsbin.com/

Check out the following for more Perl-related content:

  • Perl object-oriented programming: packaging guidelines: https://www.lsbin.com/2522.html
  • Perl CGI programming: the GET and POST methods: https://www.lsbin.com/1744.html
  • Perl constructor and destructor: https://www.lsbin.com/1266.html