24.21 Modules

It is common for pickles to depend on stuff defined in other pickles. In such cases, the load language construction can be used to load pickles from your Poke program. The syntax is:

load module;

where module is the name of the pickle to load.

For example, your Poke program may want to access some ELF data structures. In that case, we can just do:

/* Pickle to poke the contents of some ELF section.  */

load elf;

/* ... code  ... */

When asked to open a module, poke assumes it is implemented in a file named module.pk. In the example above, it will try to load elf.pk.

The pickles are searched first in the current working directory. If not found, then prefix/share/poke/module.pk is tried next.

If the environment variable POKEDATADIR is defined, it replaces prefix/share/poke. This is mainly intended to test a poke program before it gets installed in its final location.

Nothing prevents you to load the same pickle twice. This will re-define all the definitions. The new definitions are distinct from previous definitions.

It is also possible to use load construction inside a lexical block. Obviously new definitions are not available outside of the lexical block.

while (1)
  {
    load color;

    assert (color_name (color_blue) == "blue");
    break;
  }

/* color_blue is not accessible here.  */

There is an alternate syntax of the load construction that is useful when the module is implemented in a file whose name doesn’t conform to a Poke identifier. This happens, for example, when the file name contains hyphens. Example:

load "my-pickle.pk";

Note that if you use this variant of load, you must specify the full file name, including whatever extension it uses (usually .pk).