1
votes

I've been wondering, if there is an environment variable to set the search path for #use and #load for the ocaml toplevel.

What I think I know so far:

  • I can use findlib instead of "raw" #use and #load. findlib looks at some environment variables for the search path.
  • I can set a search path with -I.
  • Experiments seem to indicate that CAML_LD_LIBRARY_PATH does not influence #use (script loading) and #load (byte code file loading).
  • (updated) I can use #directory to add the desired path - but unfortunately this only takes a string literal, so i can't pass something I read from the environment at run time. (Update: I forgot to mention #directory originally and why it doesn't fit my use case).

What I want to do:

  • Run ocaml programs as scripts
  • Point ocaml to script libraries and script fragments with an environment variable
  • Avoid, in some scenarios, to create a full findlib library.

Presently I'm not using ocaml as interpreter, but a wrapper that adds a path to the invocation. I want to avoid the wrapper.

(I hope the questions makes sense now, after you know my use case)

So: Is there an environment variable to set the search path for #use and #load without resorting to a wrapper?

More Details

What I'm currently doing:

#!/usr/bin/env oscript2

#use "MyScript"
#load "SomeModule.cmo"

(* ... more ocaml *)

oscript2 is a wrapper around ocaml that basically sets the search paths for #use and #load, but finally executes the toplevel ocaml withe something like

exec ocaml -I .... ...some-byte-code-modules... "$@"

MyScript and SomeModule.cmo live outside of the "normal" Ocaml search path. The actual location might change, but after login (and working through the profie scripts) there is an environment variable (today it's OSCRIPTLOAD_PATH) that tells me, where alle loadable byte code and ocaml script files might live.

This works well, a variant of that setup has been in use for years (at least 7).

The one thing that bothers me there, is: The wrapper, the simple fact of it's presence, looks homebrew, so I'd like to avoid it, to make a better impression on potential future users of the script collection. What I'd like to do

#!/usr/bin/env ocaml

#use "MyScript"
#load "SomeModule.cmo"

(* ... more ocaml *)

and have ocaml itself pick up the search path from some environment variable (I'm free to change the variable name, that is under my control, but I don't want to install script and byte code libs into the default search path, and, as already stated, I' asking if I can do that without findlib).

Basically (as already stated at the very beginning) I'm looking for an environment variable that controls the search path for #use and #load. I'm not looking for toplevel directives and not for wrappers that retrofit this feature. (Thanks everyone for those suggestions, but unfortunately I've already gone that road, it's feasible, but here I'm looking for an alternative purely for cosmetic reasons).

Recent research didn't bring up that such a variable exists, but I thought I'd be asking here, before giving up on it.

2
Note that you can also add a directory to the search path using the #directory directive.Martin Jambon

2 Answers

1
votes

From inside the OCaml toplevel you can use the #directory "foo";; primitive to add an include directory.

1
votes

Here's a shell script that runs the OCaml toplevel while adding a directory to the search path taken from an environment variable named EXTRA_OCAML_DIR.

#!/bin/sh
ocaml -I "$EXTRA_OCAML_DIR" "$@"

If you run this instead of ocaml, you will have a directory in the load path specified by an environment variable.

It seems a little obvious, but maybe it will spark an idea that is more helpful.