use Load module

After installing the module , There will be corresponding documents , It can be done through perldoc MODULE_NAME To get help using the module .

for example : obtain File::Utils How to use .
perldoc File::Utils
To be in perl Using modules in the program , Need to use use To load (load) modular . for example ,File::Basename modular :
#!/usr/bin/perl use File::Basename;

generally speaking , All modules to be loaded are written in perl The beginning of the program , because use Statement is executed during program compilation , And check it out later , It is also convenient to modify the modules used in the program . of course , This is not necessary , You can write anywhere use sentence .

After loading the module , The properties within the module are imported into the current program's namespace for use by the current program . for example ,File::Basename
The two subroutines provided by the module are basename() and dirname(), They get the basename and dirname.
#!/usr/bin/perl use File::Basename; my $basename = basename( "/usr/bin/perl"
); my $dirname = dirname( "/usr/bin/" ); print "$basename\n$dirname";
perhaps , Import only the given function :
use File::Basename ('basename','dirname');
It is recommended to use directly qw() How to list , Even if only one function is imported :
use File::Basename qw(basename dirname);
After the function is imported into the current namespace , A function in a module may conflict with a subroutine of the same name defined in the current program file . You need to specify the full name of the function :
my $dirname = File::Basename::dirname( "/usr/bin/" );
If , An empty list is given when importing modules , It's not the same as not giving lists . in other words , The following two ways are not equivalent :
use File::Basename; use File::Basename ();
The former represents the default properties of the import module , Some default functions are included . The latter means nothing is imported , In this case, if you want to use the function in this module , Only full name can be used .

in addition , Some modules are more complex , function , attribute , There are many methods , Generally, tag grouping is provided at this time , They're actually a collection of functions . In this way, you can import according to the group label when importing . A common label is
:all, It represents the import of all properties .
use CGI qw(:all);
If the module also provides a tag Label for , Then you can import this tag :
use MODULE_NAME qw(:tag);
Want to know if the module provides tags , And what labels are available , sure man MODULE obtain . for example ,CGI Modular man CGI There is a paragraph in the document :
Here is a list of the function sets you can import: :cgi Import all
CGI-handling methods, such as param(), path_info() and the like. :form Import
all fill-out form generating methods, such as textfield(). :html2 Import all
methods that generate HTML 2.0 standard elements. :html3 Import all methods
that generate HTML 3.0 elements (such as <table>, <super> and <sub>). :html4
Import all methods that generate HTML 4 elements (such as <abbrev>, <acronym>
and <thead>). :netscape Import the <blink>, <fontsize> and <center> tags. :html
Import all HTML-generating shortcuts (i.e. 'html2', 'html3', 'html4' and
'netscape') :standard Import "standard" features, 'html2', 'html3', 'html4',
'form' and 'cgi'. :all Import all the available methods.
Module lookup path and @INC

By default ,perl From @INC Find the module in the specified path , It's like shell Of PATH Environment variables are the same .

Here are @INC The path of :
[[email protected] ~]# perl -e 'foreach (@INC){print "$_\n"};' /etc/perl
/usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1
/usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5
/usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26
/usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base
If we manually install the package , Or installed in a non default search path ( For example, different users are installed in different home directories ), This can be done by using the shell Set in PERL5LIB
environment variable ,perl The module is temporarily searched from this environment variable .
[[email protected] ~]# export PERL5LIB="/home/fairy/myperl"
perhaps , stay perl In the command line -I option , Explicitly specify the module lookup path of the program to be run .
[[email protected] ~]# perl -I/home/fairy/myperl PERL_PROGRAM
There are several more complex approaches :

* (1). stay perl In the process , stay use Before referencing a module , use BEGIN {unshift @INC,"/home/fairy/myperl"};
sentence , bring @INC The specified lookup directory is added during compilation

* (2). stay perl In the process , stay use Before referencing a module , use use lib "/home/fairy/myperl"; appoint lib Find path
These methods are inconvenient , therefore , Direct setting PERL5LIB environment variable , Or settings local::lib that will do .