Including own modules in Strawberry Perl.
With most programming languages (at least on Windows), the simplest way to make sure that the compiler/interpreter finds the custom modules, units, libraries, that you wrote yourself, or that you downloaded from some Internet site, and that you want to include into the main source (in Perl, this is done using the instruction use), is to place them together with the main source file. This also works with some releases of Perl, but not with Strawberry Perl. When I tried to run my program perlcrt1.pl, that uses the module perlcrt.pm, both files placed in X:\test, I got the error message Can't locate perlcrt.pm in @INC.
@INC is an array that contains the path of the actually used Perl libraries, i.e. the path of the directories, where the Perl interpreter searches for modules to be included. As you can see on the screenshot, X:\test is not part of @INC, what means that by default, Strawberry Perl does not search the current directory for modules.
Thus, if we place our own or other modules needed by a program together with the program, we'll have to tell Perl about this custom modules include directory. This can
be done with the -I command line parameter. In our example, putting perlcrt1.pl and perlcrt.pm in X:\test, we could
use the include directive
-I X:\test
However, this is only useful, if the program is always launched from this specific directory.
If the module is used by several programs, you should create a custom library, and indicating the path to this library with the
-I parameter. If the module is only used with a given program, you can put it together with this program, specifying the
current directory. This supposes, of course, that when launching Perl, you are in the directory containing the files. This will be mostly the
case. If you run Perl in the console, you normally cd to the directory with your script before launching Perl (otherwise you would have to
specify the path to your source file). And, when a CGI script is launched on the Apache webserver, the current directory is automatically set to the directory containing
the script. To run Perl with inclusion of the current directory to the search path for modules, use the command:
perl -I.
Specifying the -I parameter on the command line is not really convenient, as you have to always enter it when running the program. A better
way is to add to the shebang line of your script; then you can launch Perl without any parameters. Here the shebang line (on Windows):
#!<perl-binaries-directory>/perl.exe -I.
Note: You may or may not insert a space between the -I and the dot (resp. the directory path). On the other hand, you must not include the dot (or the path) between quotes (at least not with Strawberry Perl version 5.32, that I actually use; I think to remember that there were versions where the quotes were accepted?).
If you find this text helpful, please, support me and this website by signing my guestbook.