Where does Perl load modules from in its
use and
require statements? It loads them from directorys in a special list called
@INC, from files with a .pm extension in those directorys.
When Perl's installed, @INC is set to a list of directorys that includes generic locations for its standard modules, some release specific directories, and "." the current directory, which are checked in order each time you do a
use or
require.
Some ways to modify @INC
** You can add to the list in @INC by using the -I command line option:
perl -I /Users/grahamellis/jan06 i2
says "run the perl program i2, additionally checking the jan06 directory for modules"
** You can add to the list within your program by doing so in a BEGIN block prior to the
use statements:
BEGIN {
push @INC,"/Users/grahamellis/jan06";
}
use demo;
print "hello world";
Rather curiously,
use calls are run at compile time not at run time ... but then so are BEGIN blocks ... so you put your manipulation of @INC into one of those to get it to happen early enough.
** You can add to the beginning of the list by setting the PERL5LIB environment variable prior to running your program:
export PERL5LIB=/Users/grahamellis/jan06
and you can use a colon separated list for that if you want to pre-pend more than one directory.
Footnote - don't you just love Perl's "There's more than one way to do it". Thanks to Dave Cross - author of various Perl books - for reminding me of use lib; - see the comments he's added for a description of this in his own words (written 2006-02-02, updated 2006-06-05)
Commentator | says ... | Dave Cross: | There is also the "use lib" pragma - this is probably the most common approach. And $PERL5LIB can also be called just $PERLLIB. (comment added 2006-02-02 06:43:36) |
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P218 - Perl - More Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[246] When to bless a Perl variable - (2005-03-15)
[531] Packages in packages in Perl - (2005-12-16)
[592] NOT Gone phishing - (2006-02-05)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[930] -> , >= and => in Perl - (2006-11-18)
[1217] What are factory and singleton classes? - (2007-06-04)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1664] Example of OO in Perl - (2008-06-03)
[1665] Factory method example - Perl - (2008-06-04)
[1819] Calling base class constructors - (2008-10-03)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2811] Igloos melt in the summer, but houses do not - (2010-06-15)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3097] Making Perl class definitions more conventional and shorter - (2010-12-20)
[3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
[3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
[3941] Building an object based on another object in Perl - (2012-12-03)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
P209 - Subroutines in Perl [96] Variable Scope - (2004-10-22)
[308] Call by name v call by value - (2005-05-11)
[357] Where do Perl modules load from - (2005-06-24)
[775] Do not duplicate your code - (2006-06-23)
[969] Perl - $_ and @_ - (2006-12-07)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1921] Romeo and Julie - (2008-12-04)
[2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[3066] Separating groups of variables into namespaces - (2010-11-24)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3833] Learning to use existing classes in Perl - (2012-08-10)
Some other Articles
Key facts - SQL and MySQLDanny and Donna are getting marriedRobust PHP user inputsChanging @INC - where Perl loads its modulesJob vacancy - double agent wantedPerl Regular Expressions - finding the position and length of the matchLooking for Python staffLoosing breath with GeraldRemember to process blank lines