1 Introduction

1.6 Loading Files

1.6.1 Loading Files in the REPL

There are several ways in which one may load files in LFE. slurp

As mentioned in the section on LFE's REPL, slurping a file makes all functions and macros defined in the file available in the shell. One does not need to reference the module (and, in fact, attempting to do so will result in an error. Also, note that only one file can be slurped at a time; slurping a new one removes all data about the previous file.

> (slurp "my-module.lfe")
#(ok my-module)
> c

Compiling a module from the REPL is what you need if you wish to work with multiple modules simultaneously:

> (c "my-module")
#(module my-module)
> ec

You can also load Erlang files in LFE:

> (ec "../lfe/src/lfe_macro.erl")
#(ok lfe_macro)
> l

If a module is in your Erlang/LFE path, you can load that too:

> (l 'mochiweb)
(#(module mochiweb))

1.6.2 Loading Files in Modules

Code may be included wholesale into LFE modules by either using include-file or include-lib. include-file

If you have records or data that you would like to be available to more than one module, you can put those in a dedicated file and pull them in to your modules. For example, let's say I had defined the following constants in the file include/consts.lfe:

(defmacro *base-cool-factor* _ `0.8)
(defmacro *earth-adjustment* _ `0.3)

Then, in the following two files I could easily use those constants by including them:

(defmodule zaphod
  (export all))

(include-file "include/consts.lfe")

(defun get-coolness ()
  (let ((zaphod-cool-factor 0.9))
    (* (*base-cool-factor*) zaphod-cool-factor)))
(defmodule arthur
  (export all))

(include-file "include/consts.lfe")

(defun get-coolness ()
  (let ((arthur-cool-factor 0.1))
    (* (*base-cool-factor*) (*earth-adjustment*) arthur-cool-factor))) include-lib