Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Changing @INC - where Perl loads its modules

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 05:41:41)

Commentatorsays ...
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 under
P209 - Subroutines in Perl
P218 - Perl - More Objects

Back to
Job vacancy - double agent wanted
Previous and next
or
Horse's mouth home
Forward to
Robust PHP user inputs
Some other Articles
NOT Gone phishing
Key facts - SQL and MySQL
Danny and Donna are getting married
Robust PHP user inputs
Changing @INC - where Perl loads its modules
Job vacancy - double agent wanted
Perl Regular Expressions - finding the position and length of the match
Looking for Python staff
Loosing breath with Gerald
Remember to process blank lines
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho