@ ISA array and exporter in Perl

Source: Internet
Author: User

The inheritance of Perl classes (packages) is implemented through the @ ISA array. In short, Perl regards it as a special array of directory names, similar to the @ INC array (the @ INC array contains a reference path ). When Perl cannot find the required method in the current class (package), it will search for the classes listed in this array.

Whether it sounds abstract ...... I will release an example for your reference. I usually see many examples. @ ISA works with exporter.

Arithmetic. PM

Package arithmetic; use strict; Use exporter; # Use vars to predefine global variables # If use vars is not used, a global error is reported or our is used. # Of course, if you do not use strict, no error will be reported. Use vars QW (@ ISA @ export @ export_ OK % export_tags); # This is what we call. Assign the exproter value to @ ISA array @ ISA = QW (exporter); # default export symbol @ export = QW (add subtract); # export the symbol as required, that is, when using, you must specify QW () @ export_ OK = QW (multiply divide); # export marked with key-value symbols. The export elements in the array are, it must exist in @ export and @ export_ OK. % Export_tags = (SPEC => [QW (& add & subtract & multiply & divide)]); sub add {My ($ No1, $ NO2) = @_; my $ result; $ result = $ No1 + $ NO2; return $ result;} sub subtract {My ($ No1, $ NO2) =@_; my $ result; $ result = $ No1-$ NO2; return $ result;} sub multiply {My ($ No1, $ NO2) =@_; my $ result; $ result = $ No1 * $ NO2; return $ result;} sub divide {My ($ No1, $ NO2) =@_; my $ result; $ result = $ No1/$ NO2; return $ result;} 1;

The @ export array contains the default exported variables and function names, which are obtained when you use the package. The variables and functions in @ export_ OK are exported only when the use package in the program has special requirements.
The key-value pairs in % export_tags only allow the program to contain specific symbol groups listed in @ export and @ export_ OK.

Script. pl

#! /Usr/bin/perl-wuse strict; Use warnings; Use Arithmetic; # The Special Import mentioned above, that is, @ export_ OK # Use Arithmetic is a Use Arithmetic QW (multiply divide) that does not contain @ export_ OK; # It is imported as a key-value symbol ,! Add stands for the use of arithmetic QW (: spec! Add); print add (1, 2), "\ n"; print multiply (1, 2), "\ n"; print divide (4, 2), "\ n ";

In the preceding example, the @ ISA array is used. What is the significance of this example?

There are two types of modules: traditional and object-oriented.

The traditional style module imports and uses defined subroutines and variables for callers.

An Object-Oriented module called as a class.

Therefore, the module has two methods for its interface providers to use: Export symbols and class method calls.

This example belongs to the former.

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.