In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the Perl method in Perl object-oriented programming. I hope you will get something after reading this article. Let's discuss it together.
1. Perl method
The Perl method of the Perl class is nothing more than a Perl subroutine, commonly known as a member function. Perl's Perl method definition does not provide any special syntax, but specifies that the * * parameters of the Perl method are objects or their referenced packages. Perl has two Perl methods: static Perl method and virtual Perl method.
The static Perl method * parameters are class names, and the virtual Perl method * parameters are references to the object. The way the Perl method handles * parameters determines whether it is static or virtual. Static Perl methods generally ignore * * parameters because they already know which class they are in, and the constructor is the static Perl method. Virtual Perl methods usually first shift * * parameters into variables self or this, and then use the value as a normal reference. Such as:
1.subnameLister {2. T$keyis$value transfer; 3.my ($keys,$value); 4.while (($key,$value) = each (% $this)) {5.print "\ t$keyis$value.\ n"; 6.} 7.}
Second, the output of Perl method
If you want to reference the Cocoa.pm package now, you will get a compilation error saying that the Perl method was not found because Cocoa.pm 's Perl method has not been output yet. The output Perl method requires the Exporter module, adding the following two lines to 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. Then list your own Perl-like methods 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 achieved through the @ Isa array. The @ 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 is similar to the @ Inc array, where @ INC is the path to the containing file. The @ Isa array contains the name of the class (package). When a Perl method is not found in the current package, look for the package in @ ISA. The @ ISA also contains the base class name inherited by the current class.
All Perl methods called in a 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 looks in the AUTOLOAD () subroutine, an optional subroutine defined with sub in the current package. If you use the AUTOLOAD subroutine, you must call the autoload.pm package with the useAutoload; statement. The AUTOLOAD subroutine attempts to load the called Perl method from the installed Perl library. If AUTOLOAD also fails, Perl makes another attempt to the UNIVERSAL class, and if it still fails, Perl generates an error about the unparsed function.
Third, the call of Perl method
There are two Perl methods for calling an object's Perl method, one is through the object's reference (virtual Perl method), and the other is directly using the class name (static Perl method). Of course, the Perl method must have been output.
Now, let's write a simple Perl script that uses the class's Perl method. Here is the script code to create a skeleton of the Javaapplet source code:
#! / 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 extends (extend) the java.applet.Applet applet and makes it runnable (runnable), where the 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 results are as follows:
/ * * CreatedbyCocoa.pm * * Useatownrisk * / importjava.io.InputStream; importjava.net.*; publicclassMsgextendsjava.applet.AppletimplementsRunnable {}
Note: if you call the Perl method (also called indirectly) with the-> operator, the parameters 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.*'); you can also remove the parentheses and write: Cocoa::setImports$cup,'java.io.InputStream','java.net.*'
After reading this article, I believe you have a certain understanding of "how to use the Perl method in Perl object-oriented programming". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.