Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <ol> <li><p>Default <code>@INC</code></p> <p>Perl interpreter is <a href="http://search.cpan.org/perldoc/INSTALL" rel="noreferrer">compiled with a specific <code>@INC</code> default value</a>. To find out this value, run <code>env -i perl -V</code> command (<code>env -i</code> ignores the <code>PERL5LIB</code> environmental variable - see #2) and in the output you will see something like this:</p> <blockquote> <pre><code>$ 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 . </code></pre> </blockquote> <p>Note <code>.</code> at the end; this is the current directory. It is missing when Perl runs with <a href="http://perldoc.perl.org/perlrun.html#*-T*" rel="noreferrer"><code>-T</code> (taint checks enabled)</a>.</p> <p>To change the default path when configuring Perl binary compilation, set the configuration option <a href="http://search.cpan.org/perldoc?INSTALL#otherlibdirs" rel="noreferrer"><code>otherlibdirs</code></a>:</p> <blockquote> <p><code>Configure -Dotherlibdirs=/usr/lib/perl5/site_perl/5.16.3</code></p> </blockquote></li> <li><p>Environmental variable <code>PERL5LIB</code> (or <code>PERLLIB</code>)</p> <p>Perl pre-pends <code>@INC</code> with a list of directories (colon-separated) contained in <code>PERL5LIB</code> (if it is not defined, <code>PERLLIB</code> is used) environment variable of your shell. To see the contents of <code>@INC</code> after <code>PERL5LIB</code> and <code>PERLLIB</code> environment variables have taken effect, run <code>perl -V</code>.</p> <blockquote> <pre><code>$ 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 . </code></pre> </blockquote></li> <li><p><code>-I</code> command-line option</p> <p>Perl pre-pends <code>@INC</code> with a list of directories (colon-separated) passed as value of the <code>-I</code> command-line option. This can be done in three ways, as usual with Perl options:</p> <ul> <li><p>Pass it on command line:</p> <pre><code>perl -I /my/moduledir your_script.pl </code></pre></li> <li><p>Pass it via the first line (shebang) of your Perl script:</p> <pre><code>#!/usr/local/bin/perl -w -I /my/moduledir </code></pre></li> <li><p>Pass it as part of <code>PERL5OPT</code> (or <code>PERLOPT</code>) environment variable (see chapter 19.02 in <a href="http://oreilly.com/catalog/9780596004927" rel="noreferrer">Programming Perl</a>)</p></li> </ul></li> <li><p>Pass it via the <a href="http://perldoc.perl.org/lib.html" rel="noreferrer"><code>lib</code> pragma</a></p> <p>Perl pre-pends <code>@INC</code> with a list of directories passed in to it via <code>use lib</code>.</p> <p>In a program:</p> <pre><code>use lib ("/dir1", "/dir2"); </code></pre> <p>On the command line:</p> <pre><code>perl -Mlib=/dir1,/dir2 </code></pre> <p>You can also <a href="http://perldoc.perl.org/lib.html#Deleting-directories-from-@INC" rel="noreferrer">remove the directories from <code>@INC</code> via <code>no lib</code></a>.</p></li> <li><p>You can directly manipulate <code>@INC</code> as a regular Perl array.</p> <p>Note: Since <code>@INC</code> is used during the compilation phase, this must be done inside of a <code>BEGIN {}</code> block, which precedes the <code>use MyModule</code> statement.</p> <ul> <li><p>Add directories to the beginning via <code>unshift @INC, $dir</code>.</p></li> <li><p>Add directories to the end via <code>push @INC, $dir</code>.</p></li> <li><p>Do anything else you can do with a Perl array.</p></li> </ul></li> </ol> <p>Note: The directories are <em>unshifted</em> onto <code>@INC</code> in the order listed in this answer, e.g. default <code>@INC</code> is last in the list, preceded by <code>PERL5LIB</code>, preceded by <code>-I</code>, preceded by <code>use lib</code> and direct <code>@INC</code> manipulation, the latter two mixed in whichever order they are in Perl code.</p> <h3>References:</h3> <ul> <li><a href="http://perldoc.perl.org/perlmod.html#Perl-Modules" rel="noreferrer">perldoc perlmod</a></li> <li><a href="http://perldoc.perl.org/lib.html" rel="noreferrer">perldoc lib</a></li> <li><a href="http://world.std.com/~swmcd/steven/perl/module_mechanics.html" rel="noreferrer">Perl Module Mechanics - a great guide containing practical HOW-TOs</a></li> <li><a href="https://stackoverflow.com/questions/185114/how-do-i-use-a-perl-module-in-a-directory-not-in-inc">How do I 'use' a Perl module in a directory not in <code>@INC</code>?</a></li> <li><a href="http://rads.stackoverflow.com/amzn/click/0596000278" rel="noreferrer">Programming Perl</a> - chapter 31 part 13, ch 7.2.41</li> <li><a href="https://stackoverflow.com/questions/2526520/how-does-a-perl-program-know-where-to-find-the-file-containing-perl-module-it-use">How does a Perl program know where to find the file containing Perl module it uses?</a></li> </ul> <p>There does not seem to be a comprehensive <code>@INC</code> FAQ-type post on Stack Overflow, so this question is intended as one.</p> <h3>When to use each approach?</h3> <ul> <li><p>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 <code>@INC</code> compiled into the Perl binary.</p></li> <li><p>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 <code>@INC</code> in previous use case), set the users' <code>PERL5LIB</code>, usually during user login.</p> <p><em>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 <code>su</code>.</em></p></li> <li><p>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 <code>PERL5LIB</code> manually, or pass the <code>-I</code> option to perl.</p></li> <li><p>If the modules need to be used only for specific scripts, by <em>all</em> users using them, use <code>use lib</code>/<code>no lib</code> 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 <a href="http://p3rl.org/FindBin" rel="noreferrer">FindBin</a> module for very nice use case).</p></li> <li><p>If the directories in <code>@INC</code> need to be manipulated according to some complicated logic, either impossible to too unwieldy to implement by combination of <code>use lib</code>/<code>no lib</code> pragmas, then use direct <code>@INC</code> manipulation inside <code>BEGIN {}</code> block or inside a special purpose library designated for <code>@INC</code> manipulation, which must be used by your script(s) before any other modules are used.</p> <p>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 <a href="https://stackoverflow.com/questions/2474588/how-do-i-use-beta-perl-modules-from-beta-perl-scripts"><em>How do I use beta Perl modules from beta Perl scripts?</em></a>.</p></li> <li><p>An additional use case for directly manipulating <code>@INC</code> is to be able to add subroutine references or object references (yes, Virginia, <code>@INC</code> can contain custom Perl code and not just directory names, as explained in <a href="https://stackoverflow.com/questions/2541119/when-is-a-subroutine-reference-in-inc-called"><em>When is a subroutine reference in @INC called?</em></a>).</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload