Object oriented programming (OOP) guide in Perl

Time:2021-4-20

Object oriented programming: as the name suggests, object-oriented programming or OOP refers to the language that uses objects in programming. Object oriented programming aims at realizing real-world entities such as inheritance, hiding and polymorphism in programming. The main purpose of OOP is to bind data to the function that operates on it, so that no part of the code can access the data except the function.

Oops concept:

  • class
  • Object
  • method
  • Polymorphism
  • inherit
  • Packaging form
  • Abstraction
    Object oriented programming (OOP) guide in Perl
    Let’s take a look at the different features of object-oriented programming languages

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

  1. Class name: the name should start with an initial letter (capitalized by convention).
  2. Superclass ( If so ) : the parent of the class ( Superclass ) Name of ( If so ), Followed by 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: class body surrounded by brackets {}.

Object: it is the basic unit of object-oriented programming , It represents the entity in real life. A typical Perl program creates many objects , You know these objects interact by calling methods. One object includes:

  1. State: represented by the properties of an object. It also reflects the properties of the object.
  2. Behavior: represented by the method of an object. It also reflects the response of one object to other objects.
  3. Unique identifier: it gives an object a unique name and enables an object to interact with other objects.

Object: dog
Object oriented programming (OOP) guide in Perl
Method: a method is a collection of statements that perform certain tasks and return the results to the caller. Method can perform certain tasks without returning anything. Method saves time and helps us reuse code without retyping it.

Polymorphism: polymorphism refers to the ability of OOP programming language to effectively distinguish 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
  • Rewriting in Perl

Inheritance: inheritance is OOP ( object-oriented programming ) It’s an important pillar of our society. This is Perl that allows one kind to inherit the functions of another ( Fields and methods ) The mechanism of innovation.

Key terms:

  • Superclass: a class that inherits its functions is called a superclass (or base class or parent class).
  • Subclass: a class that inherits from another class is called a subclass ( Or derived class , Extension class or subclass )。 In addition to superclass fields and methods , Subclasses can also add their own fields and methods.
  • Reusability: inheritance supports the concept of “reusability”, that is, when we want to create a new class and already have a class that contains some required code, we can derive a new class from an existing class. By doing so, we can reuse the fields and methods of an existing class.

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 that binds code and the data it processes. Another way to consider encapsulation is that it is a kind of protective shield, which can prevent data from being accessed by code outside the shield.

  • In terms of encapsulation technology, variables or data of a class are hidden from any other class, and can only be accessed through any member function that declares its own class.
  • Like encapsulation, data in one class is hidden from other classes, so it is also called data hiding
  • Encapsulation can be achieved by declaring all variables in the class as private and writing public methods in the class to set and get the values of variables.
    Object oriented programming (OOP) guide in Perl
    Abstraction: data abstraction is an attribute , With this attribute , Only the basic details are displayed to the user. Normal or nonessential units are not displayed to the user. For example, a car is regarded as a car , Not its components. Data abstraction can also be defined as the process of ignoring irrelevant details and recognizing only the required features of an object. Object’s properties and behavior distinguish it from other similar types of objects , It also helps to classify objects / grouping. Consider a real example of a man driving. This person only knew that pressing the accelerator would increase the speed of the car , Or the brake will stop the car , But he didn’t know how the speed actually increased when the accelerator was pressed , He didn’t know the internal mechanism of the car or the implementation of the accelerator on the car , Brake, etc. This is abstraction.

For more back-end development related content, please refer to: lsbin – it development technology: https://www.lsbin.com/

See more about Perl below:

  • Perl object oriented programming: encapsulation Guide: HTTPS :// www . lsbin . com / two thousand five hundred and twenty-two . html
  • Perl CGI programming: get and post methods: https://www.lsbin.com/1744.html
  • Perl constructors and destructors: https://www.lsbin.com/1266.html

Recommended Today

Analysis of super comprehensive MySQL statement locking (Part 1)

A series of articles: Analysis of super comprehensive MySQL statement locking (Part 1) Analysis of super comprehensive MySQL statement locking (Part 2) Analysis of super comprehensive MySQL statement locking (Part 2) Preparation in advance Build a system to store heroes of the Three KingdomsheroTable: CREATE TABLE hero ( number INT, name VARCHAR(100), country varchar(100), PRIMARY […]