Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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, updated 2006-06-05)

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 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)


Back to
Job vacancy - double agent wanted
Previous and next
or
Horse's mouth home
Forward to
Robust PHP user inputs
Some other Articles
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
4759 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, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

Comment: "Might be worth pointing out -I is capital i not lowercase L ..."
Visitor Ranking 4.0 (5=excellent, 1=poor)

Comment by Marc Flamank (published 2011-01-16)
Might be worth pointing out -I is capital i not lowercase L [#3841]

Comment by Lael Daire (published 2010-03-04)
I wanted to have my main program in a base directory, and the modules in a subdir. Based off your example, I ended up making this:

BEGIN {
@cmd = split(/\//,$0); # gets the program path
pop @cmd; # strips the program name
$libpath = join("\/",@cmd)."/modulelib"; # appends module subdirectory
unshift(@INC,$libpath); # puts modulelib as the first directory to search
}

It worked nicely in my ActivePerl environment and AIX environment. [#3479]

You can Add a comment or ranking or edit your own comments

Average page ranking - 4.0

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

PAGE: http://www.wellho.net/mouth/588_Chan ... dules.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb