There are two different implementations of object-oriented programming in Perl:
One is based on the anonymous hash table method. The essence of each object instance is a reference pointing to the anonymous hash table. In this anonymous hash table, all instance attributes are stored.
The second is the array-based method. When defining a class, we will create an array for each instance attribute, and the essence of each object instance is a reference pointing to a row index in these arrays. In these arrays, all instance properties are stored.
There are many basic concepts in object-oriented, here we accept three: objects, classes and methods.
Object : An object is a reference to a data item in a class. .
Class : A class is a Perl package that contains classes that provide object methods.
Method : A method is a Perl subroutine, and the class name is its first parameter.
Perl provides the bless() function. Bless is used to construct objects. Bless associates a reference with the class name and constructs an object by returning the reference.
A class is just a simple package.
You can use a package as a class, and use the functions in the package as methods of the class.
Perl's packages provide independent namespaces, so method and variable names in different packages will not conflict.
The file extension for Perl classes is .pm.
Next we create a Person class:
package Person;
The code range of the class reaches the last line of the script file, or before the next package keyword.
To create an instance (object) of a class we need to define a constructor. Most programs use the class name as the constructor, but in Perl you can use any name.
You can use various Perl variables as Perl objects. Most of the time we will use reference arrays or hashes.
Next we create a constructor for the Person class, using Perl's hash reference.
When creating an object, you need to provide a constructor, which is a subroutine that returns a reference to the object.
Examples are as follows:
Next we create an object:
$object = new Person( "Xiao Ming", "王", 23234345);
The method of the Perl class is just a Perl subroutine, which is commonly known as a member function.
Perl's method definition in object-oriented Perl does not provide any special syntax, but stipulates that the first parameter of the method is the object or its referenced package.
Perl does not provide private variables, but we can manage object data through auxiliary methods.
Next we define a method to get the name:
sub getFirstName { return $self->{_firstName};}
You can also write like this:
sub setFirstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName};}
Next we modify the code of the Person.pm file as follows:
The employee.pl script code is as follows:
After executing the above program, the output result is:
$ perl employee.pl Name: Xiao Ming Surname: Wang Number: 23234345 Name before setting is: Xiao Ming Name after setting: Xiao Qiang
Class methods in Perl are inherited through the @ISA array. This array contains the names of other packages (classes). The inheritance of variables must be explicitly set.
Multiple inheritance means that the @ISA array contains multiple class (package) names.
Only methods can be inherited through @ISA, not data.
Next we create an Employee class that inherits the Person class.
The Employee.pm file code looks like this:
Now the Employee class contains all the methods and properties of the Person class. We enter the following code in the main.pl file and execute it:
After executing the above program, the output result is:
$ perl main.pl Name: Xiao Ming Surname: Wang Number: 23234345 Name before setting is: Xiao Ming Name after setting: Xiao Qiang
In the above example, the Employee class inherits the Person class, but if the methods of the Person class cannot meet the needs, its methods need to be rewritten.
Next we add some new methods to the Employee class and override the methods of the Person class:
We enter the following code in the main.pl file and execute it:
After executing the above program, the output result is:
$ perl main.pl Name: Xiao Ming Last name: Wang Number: 23234345 This is the name of the subclass function before setting: Xiao Ming This is the name of the subclass function after setting: Xiaoqiang
If the requested method is not found in the current class, all base classes of the current class, and the UNIVERSAL class, a method named AUTOLOAD() is searched again. If AUTOLOAD is found, it is called and the global variable $AUTOLOAD is set to the fully qualified name of the missing method.
If that doesn't work, Perl fails with an error.
If you don’t want to inherit AUTOLOAD of the base class, it’s very simple, just one sentence:
sub AUTOLOAD;
The object is automatically destroyed when the last reference to the object is released.
If you want to do something during destruction, then you can define a method named "DESTROY" in the class. It will be automatically called at the right time and perform additional cleanup actions as you wish.
package MyClass;...sub DESTROY{ print "MyClass::DESTROY calledn";}
Perl will pass the object reference as the only parameter to DESTROY. Note that this reference is read-only, which means you cannot modify it by accessing $_[0]. (Translator's Note: See perlsub) But the object itself (such as "${$_[0]" or "@{$_[0]}" and "%{$_[0]}", etc.) can still be Written.
If you rebless an object reference before the destructor returns, Perl will call the DESTROY method of the object you reblessed after the destructor returns. This gives you the opportunity to call the destructor of the base class or other classes that you specify. It should be noted that DESTROY can also be called manually, but it is usually not necessary to do so.
After the current object is released, other objects contained in the current object will be released automatically.
We can further understand the object-oriented application of Perl through the following examples:
Executing the above program, the output result is:
Call the MyClass method MyClass::new calledMyClass::MyMethod called! Call the MySubClass method MySubClass::new calledMyClass::new calledMyClass::MyMethod called! MySubClass::MyMethod called! Create a scope object MyClass::new calledMyClass::DESTROY called Create object MyClass::new calledMyClass::DESTROY called script execution ends...MyClass::DESTROY calledMySubClass::DESTROY called