Introduction to Perl Method usage in Constructors _ Application Tips

Source: Internet
Author: User
Tags autoload array definition perl script
The concept of the Perl method in the Perl language do you know, here's a quick introduction to Perl's perl approach as a Perl subroutine, or a commonly called member function.

Perl method

A brief introduction of Perl method

The Perl method of the Perl class is nothing more than a Perl subroutine, which is usually called a member function. The Perl method definition does not provide any special syntax, but the first parameter of the Perl method is the object or the package to which it is referenced. Perl has two Perl methods: The static Perl method and the 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 a reference to the object. The way the Perl method handles the first argument determines whether it is static or virtual. Static Perl methods generally ignore the first argument because they already know which class they are in, and the constructor is the static Perl method. The virtual Perl method usually first shift the first argument to the variable self or this, and then use the value as a normal reference. Such as:

Copy Code code as follows:

subnamelister{
My$this=shift;
My ($keys, $value);
while (($key, $value) =each (% $this)) {
print "\t$keyis$value.\n";
}
}

Second, the output of the Perl method

If you want to refer to the COCOA.PM package now, you will get a compile error saying that the Perl method was not found because the cocoa.pm Perl method has not yet been exported. The output Perl method requires the exporter module, with the following two lines at the beginning of the package:
Requireexporter;
@ISA =qw (exporter);
These two lines contain the upper exporter.pm module and add the exporter class name to the @isa array for lookup. The next step is to list your own class Perl methods in the @export array. For example, to output Perl methods Closemain and Declaremain, the statements are as follows:
@EXPORT =qw (Declaremain,closemain);
An inheritance of the Perl class is implemented through a @isa array. @ISA array does not need to be defined in any package, but once it is defined, Perl considers it to be 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 name of the class (package), which is searched for when a Perl method is not found in the current package. The @ISA also contains the name of the base class that the current class inherits.

All Perl methods called in a class must belong to the base class of the same class or @isa array definition. If a Perl method is not found in the @isa array, Perl looks in the AutoLoad () subroutine, and the optional subroutine is defined in the current package with a sub. If you use the AutoLoad subroutine, you must invoke the AUTOLOAD.PM package with the Useautoload statement. The AutoLoad subroutine attempts to mount the called Perl method from the installed Perl library. If AutoLoad also fails, Perl makes the last attempt to the universal class, and if it fails, Perl generates an error about the unresolved function.

Iii. invocation of the Perl method

The Perl method of calling an object has two Perl methods, one through the object's reference (the virtual Perl method) and the other by using the class name directly (the static Perl method). Of course, the Perl method must have been exported.

Now, we're writing a simple Perl script that uses the Perl method of this class, and here's the scripting code to create a Javaapplet source skeleton:

Copy Code code 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 javaapplet called MSG, which expands (extend) the Java.applet.Applet applet and makes it run (runnable), and the last three lines can also be written as follows:
Cocoa::setimports ($cup, ' java.io.InputStream ', ' java.net.* ');
Cocoa::d eclaremain ($cup, "MSG", "Java.applet.Applet", "Runnable");
Cocoa::closemain ($cup);
The results of the operation are as follows:
/*
**createdbycocoa.pm
**useatownrisk
*/
Importjava.io.InputStream;
importjava.net.*;

publicclassmsgextendsjava.applet.appletimplementsrunnable{
}

Note: If you invoke the Perl method (also called an indirect call) with the-> operator, the arguments must be enclosed in parentheses, such as: $cup->setimports (' Java.io.InputStream ', ' java.net.* '); and double colon calls such as: Cocoa::setimports ($cup, ' java.io.InputStream ', ' java.net.* '), or remove the brackets written in: Cocoa::setimports$cup, ' Java.io.InputStream ', ' java.net.* ';

Iv. Heavy-duty

Sometimes you need to specify which Perl method to use, such as when two different classes have the same name as the Perl method. Suppose classes Espresso and Qava both define the Perl method grind, you can specify the Perl method using the 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 program's operation, which can be done by using a symbolic reference:
$method = $local? " Qava:: ":" Espresso:: ";
$cup->{$method}grind (@args);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.