How Does Pike Find the Modules?
Here is a somewhat detailed explanation of exactly how Pike finds the modules that you tell it to use. Normally you don’t really need to worry about all the details, except for what is needed to put your own modules in a place where Pike can find them. But sometimes the details can be important, for example if there are several modules with the same name, but in different places, or if you want to do something advanced and ingenious with modules.
Every module that is used somewhere in your program is loaded when
the program starts. It doesn’t matter if you are importing the entire
module, or if you are just using a “qualified” name such as
smorglemod.fnordle() (that is, the method fnordle in
the module smorglemod). This is done even before
main is called. That the module is loaded means that the file
is read from disk, and the things in the module are made accessible to
your program.
When Pike loads a module with the name module-name,
Pike will look for a module with that name in the following
directories, in this order:
If the
module-namewas prefixed with a dot, in the same directory as the program that used the moduleIn directories that have been imported with
import "directory-name"In directories added with
add_module_pathIn directories specified with
-Mon the command lineIn directories listed in the environment variable
PIKE_MODULE_PATHIn the directory with builtin modules, for example called
/usr/local/pike/7.4.1/lib/modules/orC:\Program Files\Pike\lib\pike\modules
In each of those directories, Pike will look for files or subdirectories, in this order:
If there is a file called
module-name.pmod, Pike will load that file as a Pike program, clone it, and use the newly-cloned object as a module.If there is a directory called
module-name.pmod, Pike will create a module that has all the modules in that directory as members. If there is a module calledmodule(that is, with the file namemodule.pmod,module.pmod.pike, ormodule.pmod.so) in the directorymodule-name.pmod, the members from that module will be used instead of the modules actually present in the directory.If there is a file called
module-name.pmod.pike, Pike will load that file as a Pike program, clone it, and use the newly-cloned object as a module.If there is a file called
module-name.soPike will load that file withload_module, clone it, and use the newly-cloned object as a module. The file should be a dynamically linkable library of compiled C or C++ code.
As we said before, you usually don’t need to worry about these details, except for what is needed to put your modules somewhere where Pike can find them.