Perl method
1. Introduction to Perl method
The Perl method of the Perl class is just a Perl subroutine, which is commonly referred to as a member function. The Perl method definition does not provide any special syntax, but stipulates that the first parameter of the Perl method is an object or its referenced package. There are two Perl methods: static Perl method and virtual Perl method.
The first parameter of the static Perl method is the class name, and the first parameter of the virtual Perl method is the reference to the object. The way the Perl method handles 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 are in. The constructor is the static Perl method. Virtual Perl methods usually first shift the first parameter to the variable self or this, and then use the value as a normal reference. like:
subnameLister{
my$this=shift;
my($keys,$value);
while(($key,$value)=each(%$this)){
print"\t$keyis$value.\n";
}
}
2. The output of Perl method
If you want to reference the package now, you will get a compilation error saying that the Perl method is not found, because the Perl method has not yet been output. The output Perl method requires the Exporter module, and add the following two lines to the beginning of the package:
requireExporter;
@ISA=qw(Exporter);
These two lines contain the upper module and add the Exporter class name to the @ISA array for searching. Next, list your own Perl method in the @EXPORT array. For example, if you want to output Perl methods closeMain and declareMain, the statement is as follows:
@EXPORT=qw(declareMain,closeMain);
The inheritance of the Perl class is implemented through the @ISA array. @ISA array does not need to be defined in any package, however, once it is defined, Perl treats it as a special array of directory names. It's similar to the @INC array, which is the search path for the included file. The @ISA array contains the class (package) name. When a Perl method is not found in the current package, it will search for 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 the @ISA array. If a Perl method is not found in the @ISA array, Perl will search in the AUTOLOAD() subroutine, which is defined in the current package with sub. If you use the AUTOLOAD subroutine, the package must be called with the useAutoload; statement. The AUTOLOAD subroutine tries to load the called Perl method from the installed Perl library. If AUTOLOAD also fails, Perl goes to UNIVERSAL class for the last attempt. If it still fails, Perl generates an error about the unresolvable function.
3. Calling of Perl method
There are two Perl methods to call an object. One is to refer to the object (virtual Perl method), and the other is to use the class name directly (static Perl method). Of course, the Perl method must have been output.
Now, we write a simple Perl script to use the Perl method of this class. Here is the script code to create a Javaapplet source code skeleton:
#!/usr/bin/perl
useCocoa;
$cup=newCocoa;
$cup->setImports('','.*');
$cup->declareMain("Msg","","Runnable");
$cup->closeMain();
This script creates a Javaapplet called Msg, which extends the applet and makes it runnable. The last three lines can also be written as follows:
Cocoa::setImports($cup,'','.*');
Cocoa::declareMain($cup,"Msg","","Runnable");
Cocoa::closeMain($cup);
The operation results are as follows:
/*
**
**Useatownrisk
*/
;
.*;
{
}
Note: If the Perl method (also called indirect call) is called with the -> operator, the parameters must be enclosed in parentheses, such as: $cup->setImports('','.*'); and double colon calls such as: Cocoa::setImports($cup,'','.*'); can also be removed from the brackets and written as: Cocoa::setImports$cup,'','.*';
4. Reload
Sometimes you need to specify which class Perl method to use, such as when two different classes have the Perl method of the same name. Assuming that both the classes Espresso and Qava define the Perl method grind, you can use the :: operator to specify the Perl method using Qava:
$mess=Qava::grind("whole","lotta","bags");
Qava::grind($mess,"whole","lotta","bags");
You can choose which class of Perl method to use according to the operation of the program, which can be achieved by using symbolic references:
$method=$local?"Qava::":"Espresso::";
$cup->{$method}grind(@args);