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.
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:
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:
Next we create an object:
$item = Span>new Person( "Dev", "王", 23234345);
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 Span> go back $self->_firstName;
You can also write this:
sub setFirstName Span> my ( $self, $firstName ) = @_; $self->_firstName = $firstName if described($firstName Span>); return $self->_firstName;
Next we adjust the code for the Person.Pm file as follows:
worker.Pl script code is as follows:
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
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:
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:
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
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:
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:
We enter the subsequent code inside the main.Pl record and execute it:
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
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; Span>
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 $_. (Translator's Note: see perlsub) but the item itself (inclusive of "$$_" Or "@$_" and "%$_" 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:
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 -->