home PYTHONJAVA
 

Perl object-oriented

There are exceptional implementations of item-orientated programming in Perl:

  • One is based on the way an anonymous hash desk is. The essence of each item example is a connection with an anonymous hash desk. In this nameless hash table, all example homes are stored.

  • The 2nd is an array-based totally technique. When defining a category, we are able to create an array for every instance belongings, and the essence of every item instance is a reference to a row index in those arrays. In these arrays, all example homes are saved.


Object-Oriented Foundation Concepts

Object-Oriented has many basic ideas, right here we receive three: objects, classes, and strategies.

  • Object: An object is a reference to a records object in a category. .

  • Class: A elegance is a Perl package deal that includes lessons that provide item techniques.

  • Method: The technique is a Perl subroutine, and the class name is its first argument.

Perl provides the bless() characteristic, bless is used to assemble an object, a reference is related to the elegance call through bless, and an item is constructed by using returning the reference.


Class definition

A magnificence is only a easy bundle.

You can use a package deal as a class and use the features within the package as a class method.

Perl's bundle affords a separate namespace, so strategies of different programs do no longer warfare with variable names.

The record suffix of the Perl class is .Pm.

Next we create a Person class:

bundle Person;

The code variety of the magnificence goes to the ultimate line of the script report, or to the following package deal keyword.


Create and use objects

Create an instance of a class (object) We want to define a constructor. Most programs use the magnificence name as a constructor. Any call can be used in Perl.

You can use a spread of Perl variables as gadgets for Perl. In maximum cases we are able to use a reference array or hash.

Next we create a constructor for the Person class, the usage of a hash connection with Perl.

When creating an item, you need to offer a constructor, that is a subroutine that returns a reference to the item.

Examples are as follows:

Instance

bundle Person ; sub new my $magnificence = shift; my $self = _firstName => shift, _lastName => shift, _ssn => shift, ; # Output User Information print "Name:$self->_firstName n"; print "Last name:$self->_lastName n"; print "Number:$self->_ssn n"; bless $self, $elegance; go back $self;

Next we create an object:

$item = new Person( "Dev", "王", 23234345);

Defining technique

The approach of the Perl magnificence is just a Perl subroutine, also referred to as a member characteristic.

Perl's item-orientated Perl approach definition does no longer offer any special syntax, however the first parameter of the specified approach is the object or its referenced bundle.

Perl does now not provide non-public variables, but we will control object data in an auxiliary manner.

Next we define a way to get the name:

sub getFirstName 
    go back $self->_firstName;

You can also write this:

sub setFirstName 
    my ( $self, $firstName ) = @_;
    $self->_firstName = $firstName if  described($firstName);
    return $self->_firstName;

Next we adjust the code for the Person.Pm file as follows:

Instance

#!/usr/bin/perl package Person; sub new my $class = shift; my $self = _firstName => shift, _lastName => shift, _ssn => shift, ; # Output User Information print "Name:$self->_firstName n"; print "Last name:$self->_lastName n"; print "Number:$self->_ssn n"; bless $self, $elegance; return $self; sub setFirstName my ( $self, $firstName ) = @_; $self->_firstName = $firstName if sub getFirstName my( $self ) = @_; return $self->_firstName; 1;

worker.Pl script code is as follows:

Instance

#!/usr/bin/perl use Person; $object = new Person( "Dev", "king", 23234345); # Get Name $firstName = $item->getFirstName(); print "The name before the putting is: $firstNamen"; # Set a call using a helper characteristic $item->setFirstName( "Dev" ); # Get a name through a helper feature $firstName = $object->getFirstName(); print "The call after setting is: $firstNamen";

After executing the above program, the output is:

$ perl employee.pl
Name: Dev
Last Name: King
Number:23234345
Set the call earlier than : Dev
Name after putting  : Xiaoqiang

inherit

Perl elegance methods are inherited by using the @ISA array, which incorporates the names of different applications (training), and the inheritance of variables should be explicitly set.

Multiple inheritance is that this @ISA array includes multiple elegance (bundle) names.

You can most effective inherit methods via @ISA and can not inherit information.

Next we create an Employee magnificence that inherits the Person magnificence.

The

Employee.Pm file code is as follows:

Instance

#!/usr/bin/perl bundle Employee; use Person; use strict; our @ISA = qw(Person); # Inherit from Person

The Employee class now includes all the methods and homes of the Person elegance. We enter the subsequent code inside the important.Pl record and execute it:

Instance

#!/usr/bin/perluse Employee; $object = new Employee( "Dev", "king", 23234345); # Get Name $firstName = $object->getFirstName(); print "The name earlier than the putting is: $firstNamen"; # Set a name the use of a helper function $item->setFirstName( "Dev" ); # Get a name thru a helper feature $firstName = $item->getFirstName(); print "The name after putting is: $firstNamen";

After executing the above program, the output is:

$ perl principal.pl
Name: Dev
Last Name: King
Number:23234345
Set the call before : Dev
Name after placing  : Xiaoqiang

Method Rewriting

In the above example, the Employee class inherits the Person magnificence, however if the techniques of the Person class do now not meet the requirements, you want to override their techniques.

Next we upload some new techniques to the Employee class and override the methods of the Person class:

Instance

In the above instance, the Employee class inherits the Person elegance, but if the techniques of the Person elegance do now not meet the necessities, you need to override their techniques.

Next we add a few new methods to the Employee elegance and override the strategies of the Person magnificence:

Instance

#!/usr/bin/perl bundle Employee; use Person; use strict; our @ISA = qw(Person); # Inherit from Person # Rewrite constructor sub new my ($elegance) = @_; # Call the constructor of the figure class my $self = $elegance->SUPER::new( $_[1], $_[2], $_ [3] ); # Add greater attributes $self->_id = undef; $self->_title = undef; bless $self, $class; return $self; # Rewrite approach sub getFirstName my( $self ) = @_; # This is a subclass characteristic print "This is a subclass functionn"; return $self->_firstName; # Add Method sub setLastName my ( $self, $lastName ) = @_; $self->_lastName = $lastName if defined($lastName); go back $self->_lastName; sub getLastName my( $self ) = @_; return $self->_lastName; 1;

We enter the subsequent code inside the main.Pl record and execute it:

Instance

#!/usr/bin/perl use Employee; $item = new Employee( "Dev", "king", 23234345); # Get the call, use the changed constructor $firstName = $item->getFirstName(); print "The call earlier than the setting is: $firstNamen"; # Set a call the use of a helper characteristic $item->setFirstName( "Dev" ); # Get a name thru a helper feature $firstName = $object->getFirstName(); print "The call after placing is: $firstNamen";

After executing the above application, the output is:

$ perl important.pl
Name: Dev
Last Name: King
Number:23234345
This is a subclass characteristic
Set the call before : Dev
This is a subclass characteristic
Name after setting  : Xiaoqiang

Default loading

If the requested method isn't located within the modern-day class, in all base classes of the cutting-edge class, and within the UNIVERSAL elegance, This will discover a technique named AUTOLOAD() once more. If you locate AUTOLOAD, then Called at the same time as putting the global variable $AUTOLOAD to the absolutely certified call of the missing technique.

If it doesn't paintings, Perl will fail and make a mistake.

If you don't need to inherit the AUTOLOAD of the bottom class, it's quite simple, simply one sentence:

sub AUTOLOAD;

Destructor and garbage collection

When the closing reference to an object is released, the object is robotically destroyed.

If you want to do something during destructuring, you can define a technique known as "DESTROY" within the magnificence. It will be called routinely at the right time and will carry out additional cleanup movements as you desire.

package MyClass;
...
sub DESTROY

    print "MyClass::DESTROY calledn" ;

Perl will take a connection with the object as The only parameter exceeded to DESTROY. Note that this reference is study-handiest, because of this you cannot modify it by way of getting access to $_[0]. (Translator's Note: see perlsub) but the item itself (inclusive of "$$_[0]" Or "@$_[0]" and "%$_[0]" etc.) are still writable.

If you re-bless the item reference earlier than the destructor returns, Perl will then call the DESTROY method of the object you re-bless after the destructor returns. This offers you the possibility to call the base class or the destructor of other classes you specify. It should be cited that DESTROY can also be called manually, but it is also no longer important to achieve this.

After the contemporary item is released, different objects contained inside the modern object are robotically released.


Perl object-oriented instance

We can use the following examples to similarly understand Perl's item-oriented programs:

Instance

#!/usr/bin/perl # The following is a simple elegance implementation package deal MyClass; sub new print "MyClass::new calledn"; my $kind = shift; # package call my $self = ; # Quote empty hash return bless $self, $type; sub DESTROY print "MyClass::DESTROY known asn"; sub MyMethod print "MyClass::MyMethod called!n"; # Inheritance Implementation package MySubClass; @ISA = qw( MyClass ); sub new print "MySubClass::new calledn"; my $type = shift; # package deal name my $self = MyClass->new; # quoting empty hashes go back bless $self, $type; sub DESTROY print "MySubClass::DESTROY known asn"; sub MyMethod my $self = shift; $self->SUPER::MyMethod(); print " MySubClass::MyMethod known as!n"; # Call the main software of the above class package fundamental; print "Invoke the MyClass techniquen"; $myObject= MyClass ->new(); $myObject->MyMethod(); print "Invoke the MySubClass approachn"; $myObject2= MySubClass ->new(); $myObject2->MyMethod(); print "Create a scope objectn"; my $myObject2 = MyClass->new(); # Calling Destructors Automatically print "Create objectn"; $myObject3= MyClass ->new(); undef $myObject3; print "End of script execution...n"; # Automating Destructors

Execute the above program, the output is:

call MyClass approach
MyClass::newreferred to as
MyClass::MyMethodknown as!
name MySubClass technique
MySubClass::newknown as
MyClass::newknown as
MyClass::MyMethodcalled!
   MySubClass::MyMethodcalled!
Create a scope object
MyClass::newcalled
MyClass::DESTROY called
Create an object
MyClass::newreferred to as
MyClass::DESTROY referred to as
End of script execution...
MyClass::DESTROY referred to as
MySubClass::DESTROY referred to as
< !-- Other extensions -->





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.