Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I install Perl modules that I wrote?

Tags:

perl

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?

like image 232
Night Walker Avatar asked Nov 28 '22 04:11

Night Walker


2 Answers

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;
like image 181
zen Avatar answered Dec 18 '22 22:12

zen


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.

like image 41
Dave Sherohman Avatar answered Dec 18 '22 22:12

Dave Sherohman