I wrote some modules, and trying to run them. How I tell where to take the module from?
I am writing in Windows, and tried to put it in c:\perl\lib
but it didn't help.
Can I put it in the same directory, as the file that calls the module?
I wrote some modules, and trying to run them. How I tell where to take the module from?
I am writing in Windows, and tried to put it in c:\perl\lib
but it didn't help.
Can I put it in the same directory, as the file that calls the module?
Perl uses the paths in:
@INC
to search for modules. (.pm files)
If you:
perl -e 'print join "\n", @INC;'
you'll see what paths are currently being searched for modules. (This @INC list is compiled into the perl executable)
Then you can:
BEGIN { unshift @INC, 'C:\mylibs' }
or
use lib 'C:\mylibs'
and place MyModule.pm inside C:\mylibs to enable:
use MyModule;
This is exactly what local::lib is designed to handle. By default, use local::lib;
will prepend ~/perl5
to your module search path (@INC
), but you can easily tell it to add another directory.
If you're doing it with a fixed directory (rather than one relative to your home directory), then you're probably just as well off with use lib 'PATH'
.
If this is for code that will only be run from the command line, another option would be to create a PERL5LIB
environment variable which points to your personal module directory. This would affect all Perl run by your user account from the command line, so you wouldn't need to modify your Perl code at all with this method, but it's trickier to set up for non-command-line code (e.g., web scripts) and, if the code will be run by multiple users, PERL5LIB
will need to be set for all of them.
I wouldn't recommend mucking about with @INC
directly in any case. There are plenty of easier ways to do it these days.
For application-specific libraries the common approach is to use the FindBin
module to locate the application directory and then use lib
to add the application's library to @INC
:
use FindBin;
use lib "$FindBin::Bin/lib";
use AppModule;
For general-purpose modules, I recommend developing them the same way you would for a CPAN release (e.g. start with module-starter
) and install them with perl (usually under site/lib
). See also: Which framework should I use to write modules?
If you can't install with perl (i.e. you don't have the necessary permissions) you can have a personal library instead. See How do I keep my own module/library directory? in perlfaq8.