的@inc 是如何构建的? (又名: 所有影响 Perl 模块搜索位置的方法是什么?)

What are all the ways of affecting where Perl modules are searched for? or, How is Perl's @INC constructed?

As we know, Perl uses @INC array containing directory names to determine where to search for Perl module files.

There does not seem to be a comprehensive "@INC" FAQ-type post on StackOverflow, so this question is intended as one.

转载于:https://stackoverflow.com/questions/2526804/how-is-perls-inc-constructed-aka-what-are-all-the-ways-of-affecting-where-pe

csdnceshi63
elliott.david If you just want to see your @INC and you have GNU grep, this is a convenient command: perl -V | grep -A20 '@INC' (20 lines after the @INC heading should be more than enough, but you can season to taste)
接近 3 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 No, 10k lets you see deleted posts. The only moderators are the ones with the diamonds next to their names.
10 年多之前 回复
weixin_41568134
MAO-EYE Don't 10k give you full mod powers, including deletion? I had them way too briefly (1 week or so before the Great Recalc) to find out what was in scope :)
10 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Also, my rep points mean nothing in terms of moderation. I don't really have any more power to do anything that anyone else who can edit.
10 年多之前 回复
weixin_41568134
MAO-EYE - If you feel the question is inappropriate for SO, you have enough rep points to moderate it out of existence. I will understand your reasons due to the comments above and will not contest that (hey! I got killed by the best sword in the Empire!), although I happen to disagree.
10 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Well, the "comprehensive info on perl binary's compiled-in default @INC" is that it's the one the person who compiled it set up. If your asking about the various paths that get in there, then I think you have a different question than the one you answered.
10 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Some can make that answer as comprehensive by sending me a patch. It's easy because perlfaq is in Github. :)
10 年多之前 回复
weixin_41568134
MAO-EYE guessed right - the main problem with ANY and all references I found so far was a lack of comprehensive info on perl binary's compiled-in default @INC
10 年多之前 回复
csdnceshi51
旧行李 at runtime, not the full construction.
10 年多之前 回复
csdnceshi54
hurriedly% Yeah, but there's a perfectly good one at search.cpan.org/perldoc/…?
10 年多之前 回复

3个回答

We will look at how the contents of this array are constructed and can be manipulated to affect where the Perl interpreter will find the module files.

  1. Default @INC

    Perl interpreter is compiled with a specific @INC default value. To find out this value, run env -i perl -V command (env -i ignores the PERL5LIB environmental variable - see #2) and in the output you will see something like this:

    $ env -i perl -V
    ...
    @INC:
     /usr/lib/perl5/site_perl/5.18.0/x86_64-linux-thread-multi-ld
     /usr/lib/perl5/site_perl/5.18.0
     /usr/lib/perl5/5.18.0/x86_64-linux-thread-multi-ld
     /usr/lib/perl5/5.18.0
     .
    

Note . at the end; this is the current directory (which is not necessarily the same as the script's directory). It is missing in Perl 5.26+, and when Perl runs with -T (taint checks enabled).

To change the default path when configuring Perl binary compilation, set the configuration option otherlibdirs:

Configure -Dotherlibdirs=/usr/lib/perl5/site_perl/5.16.3

  1. Environmental variable PERL5LIB (or PERLLIB)

    Perl pre-pends @INC with a list of directories (colon-separated) contained in PERL5LIB (if it is not defined, PERLLIB is used) environment variable of your shell. To see the contents of @INC after PERL5LIB and PERLLIB environment variables have taken effect, run perl -V.

    $ perl -V
    ...
    %ENV:
      PERL5LIB="/home/myuser/test"
    @INC:
     /home/myuser/test
     /usr/lib/perl5/site_perl/5.18.0/x86_64-linux-thread-multi-ld
     /usr/lib/perl5/site_perl/5.18.0
     /usr/lib/perl5/5.18.0/x86_64-linux-thread-multi-ld
     /usr/lib/perl5/5.18.0
     .
    
  2. -I command-line option

    Perl pre-pends @INC with a list of directories (colon-separated) passed as value of the -I command-line option. This can be done in three ways, as usual with Perl options:

    • Pass it on command line:

      perl -I /my/moduledir your_script.pl
      
    • Pass it via the first line (shebang) of your Perl script:

      #!/usr/local/bin/perl -w -I /my/moduledir
      
    • Pass it as part of PERL5OPT (or PERLOPT) environment variable (see chapter 19.02 in Programming Perl)

  3. Pass it via the lib pragma

    Perl pre-pends @INC with a list of directories passed in to it via use lib.

    In a program:

    use lib ("/dir1", "/dir2");
    

    On the command line:

    perl -Mlib=/dir1,/dir2
    

    You can also remove the directories from @INC via no lib.

  4. You can directly manipulate @INC as a regular Perl array.

    Note: Since @INC is used during the compilation phase, this must be done inside of a BEGIN {} block, which precedes the use MyModule statement.

    • 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.

References:

There does not seem to be a comprehensive @INC FAQ-type post on Stack Overflow, 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 the default @INC compiled into the Perl binary.

  • 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' 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 pass the -I option to perl.

  • If the modules need to be used only for specific scripts, by all users using them, use use lib/no lib pragmas 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 the script's command line parameters or script's path (see the 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 @INC manipulation 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 How do I use beta Perl modules from beta Perl scripts?.

  • An additional use case for directly manipulating @INC is to be able to add subroutine references or object references (yes, Virginia, @INC can contain custom Perl code and not just directory names, as explained in When is a subroutine reference in @INC called?).

csdnceshi66
必承其重 | 欲带皇冠 Note that PERL5LIB and use lib also add arch dirs related to the specified dirs, while -I doesn't. That makes -Mlib=... much better than -I ...
大约 2 年之前 回复
csdnceshi60
℡Wang Yan Note: If you've made any modifications to $PATH, they will be stripped out by env -i -- so you may end up running a different perl.
3 年多之前 回复
csdnceshi55
~Onlooker - you're quite right. I approved the edit and added the example
大约 9 年之前 回复
csdnceshi68
local-host The output of perl -V depends on PERL5LIB and PERLLIB environment variables. I've modified the answer to reflect this.
大约 9 年之前 回复
csdnceshi55
~Onlooker I also added one more use case, the insertion of subroutine references into @INC
10 年多之前 回复
weixin_41568183
零零乙 Your answering is looking pretty good. Just keep doing what you are doing. Writing is editing :)
10 年多之前 回复
csdnceshi55
~Onlooker module files FAQ I posted and linked from here, so I put it there.
10 年多之前 回复
csdnceshi55
~Onlooker P.S. Anyone, please feel free to edit the answer to include a second architecture-specific directory inclusion via -I/use lib. I planned to do so myself at a later time but need to get offline for now.
10 年多之前 回复
weixin_41568183
零零乙 Also, to make this a really good answer, you need to tell people when they should use each one. Just giving them 10 options on how they could isn't very helpful. :)
10 年多之前 回复
csdnceshi55
~Onlooker - use::base uses it due to "require" underneath, IIRC. I'm not familiar with local::lib, will need to read more to understand what it is about
10 年多之前 回复
weixin_41568183
零零乙 don't forget PERLOPT, where you can set -I. Also, things such as base.pm and local::lib use the things you've listed implicitly.
10 年多之前 回复

In addition to the locations listed above, the OS X version of Perl also has two more ways:

  1. The /Library/Perl/x.xx/AppendToPath file. Paths listed in this file are appended to @INC at runtime.

  2. The /Library/Perl/x.xx/PrependToPath file. Paths listed in this file are prepended to @INC at runtime.

As it was said already @INC is an array and you're free to add anything you want.

My CGI REST script looks like:

#!/usr/bin/perl
use strict;
use warnings;
BEGIN {
    push @INC, 'fully_qualified_path_to_module_wiht_our_REST.pm';
}
use Modules::Rest;
gone(@_);

Subroutine gone is exported by Rest.pm.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问