I. Introduction of the Module
Module is the Perl package. The Perl object is based on a reference to the data item in the package.
When you use object-oriented programming in other languages, you declare a class and then create an object (instance) of that class, and all objects of a particular class behave the same way, determined by the class method, and you can create a class by defining a new class or inheriting from an existing class.
- Class is a Perl package that contains classes that provide object methods;
- The method is a Perl subroutine, and the class name is its first parameter;
- An object is a reference to a data item in a class.
Second, the class in Perl
A Perl class is a package. PERL5 uses a double colon (::) to identify the base class and the inheriting class (and so on).
Inheritance in Perl only inherits methods, and you must use your own mechanism to implement the inheritance of the data.
Because each class is a package, it has its own namespace and its own symbolic name associative array (see chapter X associative arrays), so each class can use its own set of independent symbol names. In combination with a reference to a package, you can use the single quotation mark (') operator to locate a variable in a class, with the positioning of members in a class such as: $class ' $member. In Perl5, you can use double colons instead of single quotes to get references, such as: $class ' $member and $class:: $member the same.
Third, create the class
Package Cocoa;
#
# Put "require" statements in for all required,imported packages
#
#
# Just Add code here
#
Next, we add a method into the package to make it a class. The first method to be added is new (), which must be called when the object is created, and the new () method is the constructor of the object.
Four, the constructor function
Sub New {
My $this = {}; # Create An anonymous hash, and #self points to it.
Bless $this; # Connect The hash to the package Cocoa.
return $this; # Return The reference to the hash.
}
1 #!/usr/bin/perl
2 push (@INC, ' pwd ');
3 Use Cocoa;
4 $cup = new Cocoa;
Ps
1. Be sure to initialize the variable in the constructor;
2. Be sure to use the My function to create variables in the method;
3. Be sure not to use local in the method unless you really want to pass the variable to the other subroutine;
4. Be sure not to use global variables in class modules.
V. Methods
The Perl class method is simply a Perl subroutine, also known as a member function. The Perl method definition does not provide any special syntax, but specifies that the first parameter of the method is the object or its referenced package. Perl has two methods, static methods and virtual methods.
The first parameter of a static method is the class name, and the first argument of the virtual method is the reference to the object.
Vi. output of the method
If you want to refer to the COCOA.PM package now, you will get a compile error saying that no method is found because the COCOA.PM method has not yet been output. The output method requires the exporter module, adding the following two lines at the beginning of the package:
Require exporter;
@ISA = QW (exporter);
These two lines contain the EXPORTER.PM module and add the exporter class name to the @isa array for lookup. Next, you can list your own class methods in the @export array. For example, to output methods Closemain and Declaremain, the statements are as follows:
@EXPORT = QW (Declaremain, Closemain);
The inheritance of Perl classes is implemented through the @isa array. @ISA array does not need to be defined in any package, however, once it is defined, Perl considers it as a special array of directory names. It is similar to the @inc array, @INC is the search path that contains the file. The @ISA array contains the class (package) name, which is searched for in the @isa package when a method is not found in the current package. The @ISA also contains the base class name for the current class inheritance.
All methods called in a class must belong to the same class or base class defined by the @isa array. If a method is not found in the @isa array, Perl looks in the AutoLoad () subroutine, and this optional subroutine is defined in the current package with a sub. If using the Autoload subroutine, you must use the use Autoload; The statement calls the AUTOLOAD.PM package. The AutoLoad subroutine attempts to mount the called method from the installed Perl library. If AutoLoad also fails, Perl then makes the last attempt to the universal class, and if it still fails, Perl generates an error about the unresolved function.
Vii. Invocation of methods
There are two ways to invoke an object, one is through the object's reference (virtual method), and the second is to use the class name directly (static method). Of course the method must already be output.
Package Cocoa;
Require exporter;
@ISA = QW (exporter);
@EXPORT = QW (Setimports, Declaremain, Closemain);
#
# This routine creates the references for imports in Java functions
#
Sub setimports{
My $class = Shift @_;
my @names = @_;
foreach (@names) {
Print "Import". $_ . "; \ n";
}
}
#
# This routine declares the main function in a Java script
#
Sub declaremain{
My $class = Shift @_;
My ($name, $extends, $implements) = @_;
print "\ n public class $name";
if ($extends) {
Print "Extends". $extends;
}
if ($implements) {
Print "Implements". $implements;
}
print "{\ n";
}
#
# This routine declares the main function in a Java script
#
Sub closemain{
Print "} \ n";
}
#
# This subroutine creates the header for the file.
#
Sub New {
My $this = {};
print "\ n \ n * * Created by COCOA.PM \ n * * use at own risk \ n \ n";
Bless $this;
return $this;
}
1;
Now, let's write a simple Perl script that uses this class method, and here's the scripting code that creates a Java applet source code skeleton:
#!/usr/bin/perl
Use Cocoa;
$cup = new Cocoa;
$cup->setimports (' Java.io.InputStream ', ' java.net.* ');
$cup->declaremain ("MSG", "Java.applet.Applet", "Runnable");
$cup->closemain ();
Eight, heavy
There are times when you need to develop a method that uses a class, such as two different classes with the same name method. Suppose the class espresso and Qava both define method grind, you can use the:: operator to specify a method that uses Qava:
$mess = Qava::grind ("Whole", "Lotta", "bags");
Qava::grind ($mess, "whole", "Lotta", "bags");
You can choose which class to use depending on how the program is running, which can be done by using a symbolic reference to invoke:
$method = $local? "Qava::": "Espresso::";
$cup->{$method}grind (@args);
Nine, the destruction function
The number of links to the Perl tracking object, which is automatically destroyed when the last application of an object is released to the memory pool. The object's Gou occurs when the code stops and the script is about to end. For global variables, destructors occur after the last line of code runs.
If you want to gain control before the object is freed, you can define the Destroy () method. DESTROY () is called before the object is freed, allowing you to do some cleanup work. The DESTROY () function does not automatically call other DESTROY () functions, and Perl does not do the built-in destruction work. If the constructor is multiple Bless,destroy () from the base class, you may need to call the DESTROY () function of the other class. When an object is disposed, all its contained object references are automatically freed and destroyed.
In general, you do not need to define the destroy () function, if necessary, in the form of the following:
Sub DESTROY {
#
# ADD code here.
#
}
For a variety of purposes, Perl uses a simple, reference-based garbage collection system. The number of references to any object must be greater than 0, otherwise the object's memory will be freed. When the program exits, Perl has a thorough search and destroy function for garbage collection, and everything in the process is simply deleted. This is redundant in Unix class systems, but it is really necessary in an embedded system or in a multithreaded environment.
X. Inheritance
Xi. Inheritance of subclass methods
12. Some comments on Perl classes and objects
The biggest benefit of OOP is code reuse. OOP uses data encapsulation to hide some complex code, and Perl's packages and modules provide data encapsulation through the My function, but Perl does not guarantee that the variables of the base class will be directly accessed, which does reduce the benefits of data encapsulation, although this action can be done, but it is a very bad programming style.
1. Be sure to access class variables by means of methods;
2. Do not access class variables directly from outside the module.
When you write a package, you should ensure that the conditions required by the method are already available or passed to it through parameters. Inside the package, you should ensure that access to global variables is only accessed using references passed by methods. For static or global data to be used by the method, the local () definition should be used in the base class, which is obtained by invoking the base class. Sometimes subclasses may need to change this data, and at this point the base class may not know how to look for new data, so it is better to define a reference to that data, and the subclass and base class to change that data by reference.
Object-oriented programming in Perl