PERL5 Packages and modules

Source: Internet
Author: User
Tags perl interpreter

First, Require function

You can use the Require function to split a program into multiple files and create a library of functions. For example, in myfile.pl, there is a defined Perl function, which can be require ("myfile.pl"); Included in the program. When the Perl interpreter sees this statement, it looks for the file myfile.pl in the directory specified by the built-in array variable @inc. If found, the statement in the file is executed, otherwise the program terminates and outputs an error message:
Can ' t find myfile.pl in @INC

As a subroutine invocation parameter, the value of the last expression in the file becomes the return value, and the Require function checks to see if it is zero and terminates if it is zero. For example, the final statement of myfile.pl is:
Print ("Hello, world!\n");
$var = 0;
Because the last statement value is zero, the Perl interpreter outputs the following error message and launches:
Myfile.pl did not reture true value
You can pass parameters such as simple variables or array elements to require, such as:

@reqlist = ("file1.pl", "file2.pl", "file3.pl");
Require ($reqlist [$]);
Require ($reqlist [$]);
Require ($reqlist [$]);

You can also not specify a file name, which is:

At this point, the value of the variable $_ is passed as the file name to require.
Note: If there are multiple directories in @inc that contain the same file, only the first one is included.

1. Require functions and sub-Libraries

A) Determine the directory of the storage sub-library;

b) The sub-program is extracted into a separate file, the file method sub-Library directory;

c) A statement with a value other than 0 at the end of each file;

d) include one or more required files in the main program master with require;

e) When running the main program, specify the sub-Library directory with the-I option, or add the directory to the @inc array before calling require.

For example, suppose your Perl sub-Library is stored in the directory/u/perldir, and the subroutine mysub stored in the file mysub.pl. Now to include the file:
Unshift (@INC, "/u/perldir");
Require ("mysub.pl");
The call to Unshift adds the directory/u/perldir to the @inc array, and the call to require includes the contents of the mysub.pl file as part of the program.

Ps

1. You should use Unshift to add directories to @inc instead of push. Since push is added to the end of the @inc, the directory jiangbei is finally searched;

2. If your library file has the same name as a file in/usr/local/lib/perl, it will not be included because require only contains the first of the files with the same name.

2. Specify the Perl version with require

Second, the package

Third, the module

PERL5 Packages and modules

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.