Resove the problem of @INC of perl.
2012-01-16 22:51
281 查看
Default
Perl interpreter is
compiled with a specific default value of
Perl pre-pends
Pass it on command line:
Pass it via the first (shebang) line of your Perl script:
Pass it as part of
Pass it via the
Perl pre-pends
In a program:
use lib ("/dir1", "/dir2")
On the command line:
perl -Mlib=/dir1,/dir2
You can also
remove the directories from
You can directly manipulate
NOTE: Since
Add directories to the beginning via
Add directories to the end via
Do anything else you can do with a Perl array.
NOTE The directories are unshifted onto @INC in the order listed in this answer, e.g. default @INC is last in the list, preceded by PERL5LIB, preceded by -I, preceded by "use lib" and direct @INC manipulation, the latter two mixed
in whichever order they are in Perl code.
The directories are unshifted onto @INC in the order listed in this answer, e.g. default @INC is last in the list, preceded by PERL5LIB, preceded by -I, preceded by "use lib" and direct @INC manipulation, the latter two mixed in whichever order they
are in Perl code.
perldoc lib
Perl Module Mechanics - a great guide containing practical HOW-TOs
How do I 'use' a Perl module in a directory not in
Programming Perl - chapter 31 part 13, ch 7.2.41
How does a Perl program know where to find the file containing Perl module it uses?
There does not seem to be a comprehensive "@INC" FAQ-type post on StackOverflow, so this question is intended as one.
If the modules in the directory will be used exclusively by a specific user for all the scripts that user runs (or if recompiling Perl is not an option to change default @INC in previous use case), set the users'
NOTE: Please be aware of the usual Unix environment variable pitfalls - e.g. in certain cases running the scripts as a particular user does not guarantee running them with that user's environment set up, e.g. via
If the modules in the directory need to be used only in specific circumstances (e.g. when the script(s) is executed in development/debug mode, you can either set PERL5LIB manually, or use "-I" perl parameter.
If the modules need to be used only for specific scripts, by ALL users using them, use
nice use case)
If the directories in @INC need to be manipulated according to some complicated logic, either impossible to too unwieldy to implement by combination of "use lib/no lib" pragmas, then use direct
An example of this is automatically switching between libraries in prod/uat/dev directories, with waterfall library pickup in prod if it's missing from dev and/or UAT (the last condition makes the standard "use lib + FindBin" solution fairly complicated.A
detailed illustration of this scenario is in this SO post.
An additional use case for directly manipulating
explained here)
Original Link: http://stackoverflow.com/questions/2526804/how-is-perls-inc-constructed-aka-what-are-all-the-ways-of-affecting-where-pe
@INC
Perl interpreter is
compiled with a specific default value of
@INCthat it was compiled with. To find out this value, run
env -i perl -Vcommand (`env -i ignores PERL5LIB environmental variable - see #2) and in the output you will see something like this:
$ perl -V ... %ENV: PERL5LIB="/home/myuser/test" @INC: /home/myuser/test /usr/local/lib/perl5/5.8.6/i686-linux /usr/local/lib/perl5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/lib/perl5/site_perl
-Icommand line parameter
Perl pre-pends
@INCwith a list of directories (colon-separated) passed in to it as a value of
-Icommand line parameter. This can be done in one of two ways, as usual with Perl parameters:
Pass it on command line:
perl -I /my/moduledir your_script.pl
Pass it via the first (shebang) line of your Perl script:
#!/usr/local/bin/perl -w -I /my/moduledir
Pass it as part of
PERL5OPT(or
PERLOPT) environmantal variable (see chapter 19.02 in Programming Perl)
Pass it via the
use libpragma
Perl pre-pends
@INCwith a list of directories passed in to it via
use libpragma.
In a program:
use lib ("/dir1", "/dir2")
On the command line:
perl -Mlib=/dir1,/dir2
You can also
remove the directories from
@INCvia
no lib
You can directly manipulate
@INCas a regular Perl array.
NOTE: Since
@INCus used during compilation phase, this must be done inside of a
BEGIN {}block, which precedes the
use MyModulestatement;
Add directories to the beginning via
unshift @INC, $dir
Add directories to the end via
push @INC, $dir
Do anything else you can do with a Perl array.
NOTE The directories are unshifted onto @INC in the order listed in this answer, e.g. default @INC is last in the list, preceded by PERL5LIB, preceded by -I, preceded by "use lib" and direct @INC manipulation, the latter two mixed
in whichever order they are in Perl code.
40 down voteaccepted | We will look at how the contents of this array are constructed and can be manipulated to affect where Perl interpreter will find the module files. Default @INC Perl interpreter is compiled with a specific default value of @INCthat it was compiled with. To find out this value, run env -i perl -Vcommand (`env -i ignores PERL5LIB environmental variable - see #2) and in the output you will see something like this: $ env -i perl -V ... @INC: /usr/local/lib/perl5/5.8.6/i686-linux /usr/local/lib/perl5/5.8.6 /usr/local/lib/perl5/site_perl/5.8.6/i686-linux /usr/local/lib/perl5/site_perl To change the default path when configuring Perl binary compilation, use otherlibdirsvariable: Configure -Dotherlibdirs=/usr/lib/perl5/site_perl/5.8.1 Environmental variable PERL5LIB(or PERLLIB) Perl pre-pends @INCwith a list of directories (colon-separated) contained in PERL5LIB(if it is not defined, PERLLIBis used) environmental variable of your shell. To see the contents of @INC after PERL5LIB and PERLLIB environment variables have taken effect, run perl -V. |
are in Perl code.
References:
perldoc perlmodperldoc lib
Perl Module Mechanics - a great guide containing practical HOW-TOs
How do I 'use' a Perl module in a directory not in
@INC?
Programming Perl - chapter 31 part 13, ch 7.2.41
How does a Perl program know where to find the file containing Perl module it uses?
There does not seem to be a comprehensive "@INC" FAQ-type post on StackOverflow, so this question is intended as one.
When to use each approach?
If the modules in a directory need to be used by many/all scripts on your site, especially run by multiple users, that directory should be included in default @INC compiled into Perl binaryIf the modules in the directory will be used exclusively by a specific user for all the scripts that user runs (or if recompiling Perl is not an option to change default @INC in previous use case), set the users'
PERL5LIB, usually during user login.
NOTE: Please be aware of the usual Unix environment variable pitfalls - e.g. in certain cases running the scripts as a particular user does not guarantee running them with that user's environment set up, e.g. via
su.
If the modules in the directory need to be used only in specific circumstances (e.g. when the script(s) is executed in development/debug mode, you can either set PERL5LIB manually, or use "-I" perl parameter.
If the modules need to be used only for specific scripts, by ALL users using them, use
use lib/no libpragmas in the program itself. It also should be used when the directory to be searched needs to be dynamically determined during runtime - e.g. from script's command line parameters or script's path (see FindBin module for very
nice use case)
If the directories in @INC need to be manipulated according to some complicated logic, either impossible to too unwieldy to implement by combination of "use lib/no lib" pragmas, then use direct
@INCmanipulation inside
BEGIN {}block or inside a special purpose library designated for @INC manipulation, which must be used by your script(s) before any other modules are used.
An example of this is automatically switching between libraries in prod/uat/dev directories, with waterfall library pickup in prod if it's missing from dev and/or UAT (the last condition makes the standard "use lib + FindBin" solution fairly complicated.A
detailed illustration of this scenario is in this SO post.
An additional use case for directly manipulating
@INCis to be able to add subroutine references or object references (yes, Virgina,
@INCcan contain custom Perl code and not just directory names, as
explained here)
Original Link: http://stackoverflow.com/questions/2526804/how-is-perls-inc-constructed-aka-what-are-all-the-ways-of-affecting-where-pe
相关文章推荐
- Error:A problem was found with the configuration of task ':app:packageDebug' 问题解决
- 《Continuous Delivery》 Notes 1: The problem of delivering software
- The problem of Titanium
- zoj 2818 Root of the Problem
- machine learning(13) -- solving the problem of overfitting:regularization
- Problem : 1202 ( The calculation of GPA )
- TFS2010-Closed the problem of 【TF201077】 【TF212000】
- Problem 7 of What is the 10 001st prime number?
- Problem 19 of How many Sundays fell on the first of the month during the twentieth century
- 欧拉项目 Problem 12 of What is the value of the first triangle number to have over five hundred divisors
- 【Stanford机器学习笔记】4-Regularization for Solving the Problem of Overfitting
- Error:Unable to start the daemon process. This problem might be caused by incorrect configuration of
- [转] Strategy: Solve Only 80 Percent of the Problem
- Error:Unable to start the daemon process. This problem might be caused by incorrect configuration of
- ZOJ Problem Set - 1090 The Circumference of the Circle
- Redis无法启动You may fix this problem by either reducing the size of the Redis heap with the --maxheap
- poj 3100 Root of the Problem
- WOJ-Problem 1009 - The Legend of Valiant Emigration
- the method of resolve the problem that easyuefi can not select other hard disk as a bootable disk
- Self-Service Linux(R) : Mastering the Art of Problem Determination