1. Perl
  2. Object Oriented
  3. What is object-oriented programming?

What is object - oriented programming?

Before describing what is object-oriented, let's use a module written in object-oriented.

How to use modules written in object orientation

To use a module written in object orientation, first load the module using use . Let's load the CGI module.

use CGI;

To create an object, write module name->new . Assigning the created object to $cgi.

my $cgi = CGI->new();

new is a method for creating objects and is called a constructor. At first, you can think of a method as a function that can be called from->.

Let's call methods other than new.

my $str = $cgi->h1('a');

h1 method receives a string

<h1>...</h1>

Will be returned.

Example

The description so far is summarized in an example.

use strict;
use warnings;

use CGI;
my $cgi = CGI->new;
my $str = $cgi->h1('a');

# <h1>a</h1> is output
print $str;

Object - oriented features

In object orientation, methods are called from module names or objects using->.

Description How to call the method
CGI->new() Call from module name
$cgi->h1 Call from object

One of the object-oriented features is that the method is called from the module name or the object.

How to use classes in Java

You can see that the description in java is almost the same as Perl.

import java.awt.Button;
import java.lang.Math;

# Call the constructor (corresponding to new) from the Button class
Button btn = new Button ();

# Call the paint method to draw from the btn object
btn.paint();

# Call the abs method that returns the absolute value from the Math class
Math.abs(-3);

What is a Perl class?

Perl doesn't have a dedicated grammar for creating classes. When we talk about classes in Perl, we just mean modules written in an object-oriented way.

Related Informatrion