Whether you understand the concept of the Perl method in the Perl language. Here we will give you a brief introduction. The Perl method of the Perl class is just a Perl subprogram, also known as a member function.
Perl Method
1. Perl Method Introduction
The Perl method of the Perl class is just a Perl subroutine, that is, a member function. The Perl method definition does not provide any special syntax, but specifies that the first parameter of the Perl method is an object or its referenced package. Perl has two methods: static Perl and virtual Perl.
The first parameter of the static Perl method is the class name, and the first parameter of the virtual Perl method is the reference of the object. The way the Perl method processes the first parameter determines whether it is static or virtual. Static Perl methods generally ignore the first parameter because they already know which class they belong to. constructors are static Perl methods. The virtual Perl method usually first shifts the first parameter to the variable self or this, and then uses this value as a common reference. For example:
Copy codeThe Code is as follows: subnameLister {
My $ this = shift;
My ($ keys, $ value );
While ($ key, $ value) = each (% $ this )){
Print "\ t $ keyis $ value. \ n ";
}
}
Ii. Perl method output
If you want to reference the Cocoa. pm package now, you will get a compilation error saying that the Perl method is not found because the Perl method of Cocoa. pm has not been output yet. The output Perl method requires the Exporter module. Add the following two lines to the beginning of the package:
RequireExporter;
@ ISA = qw (Exporter );
These two rows contain the Exporter. pm module and add the Exporter class name to the @ ISA array for search. Next, you can add your own Perl-like Method to the @ EXPORT array. For example, to output the Perl Methods closeMain and declareMain, the statement is as follows:
@ EXPORT = qw (declareMain, closeMain );
Perl class inheritance is implemented through the @ ISA array. @ ISA array does not need to be defined in any package. However, once defined, Perl regards it as a special array of directory names. It is similar to the @ INC array, where @ INC is the path to search for files. The @ ISA array contains the class (Package) name. When a Perl method is not found in the current package, find the package in @ ISA. @ ISA also contains the base class name inherited by the current class.
All Perl methods called in the class must belong to the same class or the base class defined by @ ISA array. If a Perl method is not found in the @ ISA array, Perl is searched in the AUTOLOAD () subroutine. This optional subroutine is defined by sub in the current package. If you use the AUTOLOAD subroutine, you must use the useAutoload; statement to call the autoload. pm package. The AUTOLOAD subroutine attempts to load the called Perl method from the installed Perl library. If AUTOLOAD fails, Perl goes to the UNIVERSAL class for the last attempt. If it still fails, Perl generates an error about the function that cannot be parsed.
Iii. Perl method call
There are two Perl methods for calling the Perl method of an object. One is to reference the object (Virtual Perl method), and the other is to directly use the class name (static Perl method ). Of course, this Perl method must have been output.
Now, let's write a simple Perl script to use this class's Perl method. The following is the script code for creating a Java Applet source code SKELETON:
Copy codeThe Code is as follows :#! /Usr/bin/perl
UseCocoa;
$ Cup = newCocoa;
$ Cup-> setImports ('java. io. inputstream', 'java. net .*');
$ Cup-> declareMain ("Msg", "java. applet. Applet", "Runnable ");
$ Cup-> closeMain ();
This script creates a java applet called Msg, which extends java. Applet. applet and makes it runnable. The last three lines can also be written as follows:
Cocoa: setImports ($ cup, 'java. io. inputstream', 'java. net .*');
Cocoa: declareMain ($ cup, "Msg", "java. applet. Applet", "Runnable ");
Cocoa: closeMain ($ cup );
The running result is as follows:
/*
** CreatedbyCocoa. pm
** Useatownrisk
*/
Importjava. io. InputStream;
Importjava.net .*;
PublicclassMsgextendsjava. applet. AppletimplementsRunnable {
}
Note: If you use the-> operator to call the Perl method (also called indirect call), the parameters must be enclosed in parentheses, for example, $ cup-> setImports ('java. io. inputStream ', 'java. net. * '), while double-colon calls such as: Cocoa: setImports ($ cup, 'java. io. inputStream ', 'java. net. * '); you can also remove the brackets and write them as: Cocoa: setImports $ cup, 'java. io. inputStream ', 'java. net. *';
Iv. Heavy Load
You must specify the Perl method of the class to use, for example, when two different classes have Perl methods of the same name. Suppose both Espresso and Qava have defined the Perl method grind. You can use the: operator to specify the Perl method of Qava:
$ Mess = Qava: grind ("whole", "lotta", "bags ");
Qava: grind ($ mess, "whole", "lotta", "bags ");
You can select the Perl method of the Class Based on the program running status, which can be implemented by calling with symbol reference:
$ Method = $ local? "Qava:": "Espresso ::";
$ Cup-> {$ method} grind (@ args );