Definition and use of the Perl Module

Source: Internet
Author: User
Tags vars


Do you think the module documentation in Perl is somewhat difficult? Okay, here is the simplest module in the world. It will be used to demonstrate all the features of the (demonstrate) Exporter module. There is also a script for using this module. At the same time, we will also provide a brief description of @ INC. Finally, we will also talk about the use of using warnings and use modules.



The content of this module is as follows:


Mymodule. PM

package MyModule;

use strict;
use Exporter;
use vars qw ($ VERSION @ISA @EXPORT @EXPORT_OK% EXPORT_TAGS);

$ VERSION = 1.00;
@ISA = qw (Exporter);
@EXPORT = ();
@EXPORT_OK = qw (func1 func2);
% EXPORT_TAGS = (DEFAULT => [qw (& func1)],
                 Both => [qw (& func1 & func2)]);

sub func1 {return reverse @_}
sub func2 {return map {uc} @_}

1;
    First, we will obtain a namespace by declaring the "package" name. This will ensure that the methods and variables in the module are separated from the code that calls them.

Use strict is a very good practice in modules, which will make Perl make certain restrictions on the use of global variables.

    We need to use the Exporter module to export our functions from MyModule :: namespace to main :: namespace so that programs that use MyModule can use these functions.

    In order to use strict, we must use use vars to declare some variables. Of course, in version 5.6 and above we can also use our to declare variables.

    We now set a value of $ VERSION and then use @ISA to make Exporter a part of MyModule ?. For details on what @ISA is and how to use it, please refer to "perlboot".

    @EXPORT contains a list of functions that we need to output by default. Here, it is empty. In general, the less you output by using @EXPROT by default, the better. Because the program that calls the module, there may be a function or code that conflicts with the function. If the program needs to call a specified function, then please let it actively request.

    @EXPORT_OK contains a list of functions that we need to output when we call. We only output & func1 and & func2. This method takes precedence over blindly using @EXPORT to output functions. You can also output global variables like $ CONFIG that are not in the semantic category defined by my. (Refer to using "our" or use vars to declare global variables)

    % EXPORT_TAGS. For convenience, we have defined two sets of output tags. The ‘: DEFAULT’ tag outputs only & func1; the ‘: Both’ tag outputs & func1 and & func2. This hash table stores tags that refer to array references. Note: The array here is anonymous.

    Finally, we need to add a "1;" at the end of the module. Because when Perl loads a module, it will check whether the module can return a true value at the end, and judge whether the module has been loaded successfully based on this. Of course, you can add any truth value at the end (see "Code :: Police"), but 1 is the most convenient.

MySciprt.pl (an example of using MyModule)
#! / usr / bin / perl -w

use strict;

# you may need to set @INC here (see below)

my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r!);

# case 1
# use MyModule;
# print func1 (@list), "\n";
# print func2 (@list), "\n";

# case 2
# use MyModule qw (& func1);
# print func1 (@list), "\n";
# print MyModule :: func2 (@list), "\n";

# case 3
# use MyModule qw (: DEFAULT);
# print func1 (@list), "\n";
# print func2 (@list), "\n";

# case 4
# use MyModule qw (: Both);
# print func1 (@list), "\n";
# print func2 (@list), "\n";
As seen above, we used MyModule in MyScript.pl. Remove the comment symbols in the middle to see what happens. Just remove it once.

Case 1: Since our module outputs nothing by default (& func1 and & func2 are not output), we will get an error that they do not exist in the main :: namespace.

Case 2: This is operating normally. We let the module output & func1, so we can use it normally. Although we did not output & func2, we used the complete package path of & func2, so it will work as well.

Case 3: The ‘: DEFAULT’ tag should output & func1, so you should want to return an error that is missing the & func2 function. But in fact, perl just found trouble with & func1 (the error message indicates that the & func1 function is not defined). Well, what's wrong here? It turns out that the DEFAULT tag name is special. In our module, the% EXPORT_TAGS hash table will be automatically set to DEFAULT => \ @ EXPROT. That is to say, DEFAULT exports by default from the @EXPROT array .

Case 4: We specified that both functions are output through the ‘: Both’ tag, and he did it. * Notes on @ INC * When you submit a use MyModule, you will instruct perl to search for the module name in the @INC array. @INC usually contains:

/ perl / lib
/ perl / site / lib
.
"." This directory represents the current working directory. The core modules are installed in the perl / lib directory, and the non-core modules are installed in the perl / site / lib directory. You can add custom directories to @INC. Like this:

BEGIN {push @INC, ’/ my / dir’}
# or
BEGIN {unshift @INC, ’/ my / dir’}
# or
use lib ’/ my / dir’;
We need to use the BEGIN block to add values to @INC at compile time. This is the time for Perl to check the module.

    If you wait too late when the program is compiled, perl will throw an exception saying "MyModule cannot be found in @INC". The difference between using push or unshift to add values is that the order in which perl searches @INC is The first directory in @INC starts. If you have a MyModule module in / perl / lib /, / perl / site / lib / and ./, the module in / perl / lib will be found and used first. use lib usage can have the same effect as BEGIN {unshift @ INC, $ dir}-please see "perlman: lib: lib": http: //www.perlmonks.org/? node = perlman% 3Alib% 3Alib. * What does use Foo :: Bar mean * use Foo :: Bar does not mean looking for a module file called Foo :: Bar.pm in the @INC directory. It means looking for a "subdirectory" called "Foo" in the directory of @INC, and then looking for a "module" called "Bar.pm" in it. Now, if we successfully "use" a module, then we can use all the functions in this module with the complete package path syntax & PACKAGE :: FUNCTION. When we say & Foo :: Bar :: some_func, we are referring to the "package name" rather than the file name that contains the path used in use. This will allow you to include many package names in a used file. These names are usually the same in actual use.

use warnings;
    You should turn on warnings to detect your module, because it can detect many subtle errors. You can turn on the warning option by adding the -w parameter to the test module code. If you add use warnings to the module, then your module must be required to run above perl 5.6, otherwise it is not supported. If you add $ ^ W ++ at the top of the module, then you will turn on the warning option globally-this will affect other modules, you better use it only in your own program, because it is slightly overbearing some. There is a code written by an expert called "tye": http: //www.perlmonks.org/?node=tye to test the warning option, but it is not directly included in his / her own module. Hope these will clarify how it works.

From: http://www.perlchina.org/archive/archive.php?action=archive&page=33

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.