OO Perl inheritance

Source: Internet
Author: User

Wxperl is a pure oo GUI library. Today, when I looked at its example, I also tried different inheritance methods.
This is used in hello. pl of wxperl:
Use vars QW (@ ISA );

@ ISA = QW (wx: APP );
I checked the use vars Pragma in programming Perl and found that this usage is already in some degree.
The code above is equivalent:
Our (@ ISA) = QW (wx: APP );
They define a global variable for a package. Note that do not use them together, that is, do not
Use use vars and our at the same time for @ ISA in the code. (When more than one package is defined in a file)
In addition to the two methods, you can also use:
Use base QW (wx: APP );
This code is equivalent:
Begin {

Require wx: app; # note that the raw text is used here. For details, refer to the introduction of require.

Push @ Isa, QW (wx: APP );

}

We also found a small problem, using use base QW (wx: APP );
The parent class must end with 1; while the other two methods may also be OK.

This may be the reason for require.

The classes (packages) listed in the @ ISA array indicate the parent class or base class of the current class ). It is the inheritance Method on which Perl depends. @ ISA array contains a list of classes (packages). When Perl cannot find the required method in the current class (package), it searches for Classes listed in this array. If it still cannot be found, Perl will search for and call the autoload function. If it still cannot be found, Perl will perform the final search in the predefined universal package. The global base class of all packages in the universal class is also the class at the top of the class inheritance mechanism.

The @ ISA array is not searched during a normal subroutine call. However, if you call a subroutine by calling the method syntax, the program will search for the @ ISA array.

If a Perl package does not have a new function, but you need to call the functions in this package, you can add the function of this package to ISA using the following method. Then add & to the function in another package &.

Require exporter;
Use vars ('@ ISA ');
@ ISA = QW (exporter );
Our @ export = QW (function name );

The second method is to add the use package path name at the beginning, and then use the Class Name> package path name: function name to reference the function under the package ..

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.