Quick Start
2 Executing Code
Once you've got LFE built, you want to play with it, right? Let's take a look at the REPL (interactive shell) first.
2.1 The REPL
To start the REPL, simply run the lfe
script and tell erl
(which is
being run in the script) which additional code paths it should look for (in this
case, the compiled LFE code):
$ ./bin/lfe -pa ./ebin
This assumes that you are still in the lfe
directory where you build LFE.
Running that command will dump you into the LFE REPL, looking something like
this:
Erlang R15B03 (erts-5.9.3.1) [source] [64-bit] [smp:8:8] [async-threads:0] ...
LFE Shell V5.9.3.1 (abort with ^G)
>
Note that you can also start the the LFE shell manually from an existing Erlang
shell (as we did earlier in this guide) or you can pass parameters to erl
to start it up:
$ erl -pa ebin -s lfe_boot -noshell -noinput
Now try doing some basic operations:
Next, let's operate on some variables:
Looking good!
2.2 Running Scripts
Okay, so now that you can run things in the REPL, you want to run them as a script, yes? Here's how.
Let's create a temporary subdirectory to play in without fear of messing up our LFE directory:
$ mkdir tmp
$ cd tmp
Then, in that directory, let's create the following file and save it as
hello.lfe
:
To compile that script and then run it, we can do this:
$ ../bin/lfec hello.lfe
$ ../bin/lfe -s hello start -s erlang halt
Or, if we want to use Erlang directly, we could do this:
$ erl -pa ../ebin -s lfe_comp file hello -s erlang halt
$ erl -pa ../ebin -s hello start -s erlang halt
Or, we could compile it and run it in the same command:
$ erl -pa ../ebin -s lfe_comp file hello -s hello start -s erlang halt
Note that this is the command line equivalent of the following:
$ erl -pa ../ebin
1> lfe_comp:file(hello).
{ok,hello}
2> hello:start().
Lfe says 'Hello, World!'
ok
3>
2.3 Running Scripts from the REPL
You can also use your new hello.lfe
script in the REPL itself. There are
two ways you can do this, using slurp
or compiling the file. If you use
slurp
, all the functions are pulled into the shell namespace, and you
won't have to reference the module name. Again, assuming that you are in
lfe/tmp
:
If you choose to compile your module instead, you will use it like so:
Note that in the second example, you need to reference the module.
For more information on the LFE shell, be sure to see the "REPL" section of the User Guide Introduction.