Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perl interpreter (which runs your perl program) will use a special array called <code>@INC</code> to search for a file containing the module. </p> <p>Each value in the <code>@INC</code> array is a directory name (<em>but see note below</em>); Perl will search within those directories in a loop using the rules specified below. (Please refer to <a href="https://stackoverflow.com/questions/2526804/how-is-perls-inc-constructed-aka-what-are-all-the-ways-of-affecting-where-per">this SO post for details of how the contents of @INC are determined</a>).</p> <p>If the module's file is not found after exhausting <code>@INC</code>, the program's compilation will be aborted with an error. If the module's file is found in one of the directories specified in <code>@INC</code>, the search is finished without looking at the rest of <code>@INC</code>.</p> <p>The way Perl searches for a module file within each of the directories listed in <code>@INC</code> is as follows:</p> <ul> <li><p>First, it will separate the module name's hierarchical components (words separated by <code>::</code>), into the last component - which will be used to form a file name - and a hierarchy path (all the components preceding the last <code>::</code>). </p> <p>In case the module name has only one component (no <code>::</code>, e.g. <code>MyModule1</code> above), the hierarchy path is empty and the filename is the name of the module. In the second example in this question, the last component is <code>MyModule2</code> and hierarchy path will be <code>This::Here</code>.</p></li> <li><p>The expected file name will be determined by appending the last component of the module name with a <code>.pm</code> extension. E.g. <code>MyModule1.pm</code> and <code>MyModule2.pm</code> in our examples.</p> <p>NOTE: Module names are obviously case sensitive on Unix and other operating systems where file/directory naming is case sensitive.</p></li> <li><p>The module's directory will be determined by:</p> <ol> <li><p>Taking the next directory from <code>@INC</code> - let's say <code>/usr/lib/perl</code> as an example</p></li> <li><p>Forming a sub-directory of that directory by taking the hierarchy path of the module name (if any) and replacing "::" with <code>/</code> or whatever character the operating system uses as directory separator. In our two examples, the first module will be searched for in <code>/usr/lib/perl</code> (no sub-directory) and the second in <code>/usr/lib/perl/This/Here</code>.</p></li> <li><p><em>NOTE</em>: the above is a slight simplification - <code>@INC</code> <a href="https://stackoverflow.com/questions/2541119/when-is-a-subroutine-reference-in-inc-called">may also contain subroutine references and object references</a>, which load the modules as their custom code specifies instead of performing the lookup in the directory as specified in #2 logic above. That functionality appears to be very seldom used and this article assumes that entire <code>@INC</code> only contains directories.</p></li> </ol></li> </ul> <p>Let's go over a specific example, assuming that your <code>@INC</code> contains two sub-directories: <code>("/usr/lib/perl", "/opt/custom/lib")</code>. </p> <p>Then Perl would search as follows:</p> <pre> ========================================================================== | Module | Try # | File to try ========================================================================== | MyModule1 | Try 1 | /usr/lib/perl/MyModule1.pm | MyModule1 | Try 2 | /opt/custom/lib/MyModule1.pm ========================================================================== | This::Here::MyModule2 | Try 1 | /usr/lib/perl/This/Here/MyModule2.pm | This::Here::MyModule2 | Try 2 | /opt/custom/lib/This/Here/MyModule2.pm ========================================================================== </pre> <p>Please recall that Perl interpreter will STOP trying to search once it finds the file in one of the locations, without trying to see if the file is in later locations as well. E.g. if <code>/usr/lib/perl/This/Here/MyModule2.pm</code> exists, then Perl will not look for, nor care about the existence, of <code>/opt/custom/lib/This/Here/MyModule2.pm</code>.</p> <p>NOTE: @INC is used whenever Perl interpreter is using <code>require</code>-like mechanism for importing Perl modules. This includes:</p> <ul> <li><code>require</code> directive itself</li> <li><code>use MyModule</code> statement (equivalent to require+import)</li> <li><a href="http://search.cpan.org/~rgarcia/base-2.14/lib/base.pm" rel="nofollow noreferrer"><code>use base</code></a> (equivalent to require+"push @ISA")</li> <li><code>-M</code> command line parameter </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