Here you will find tutorials, examples, and other accompanying material.
Erlang can be installed on multiple platforms by either compiling it from source, or by downloading and installing one of the pre-compiled packages hosted on erlang-solutions.com.
On Linux you can also install Erlang using apt:
sudo apt-get install erlang
macOS users can install Erlang using Homebrew as well:
brew install erlang
In any case, after the Erlang distribution has been installed, make sure that the PATH variable has been configured correctly.
Try running erl or erlc to determine whether the Erlang binaries have been properly included in your system path.
In this course, we will not use sophisticated code-building mechanisms like make or rebar.
Instead, we shall stick to the simplest possible compilation mechanism: namely compilation through the Erlang shell.
This allows us to get used to the basic compilation commands, while at the same time, eliminating unnecessary complexities related to source code dependency management and others.
Compilation can be done by invoking the c() function on the Erlang shell like so:
> c(hello).
This compiles the module hello.erl (note we did not specify the .erl extension in the above example), resulting in the creation of an object code file hello.beam.
When a module is successfully compiled, it is automatically loaded by Erlang into the Erlang shell.
We can verify this by calling the m() function that displays the list of all loaded modules, together with the fully-qualified path pointing to the corresponding .beam file on the local file system.
Compiling modules in this way can become cumbersome when the number of modules is beyond a couple.
Erlang makes it possible for the user to configure and expose a number of utility functions that are automatically loaded and made accessible from the Erlang shell. We will use this mechanism to facilitate the compilation and loading of Erlang modules:
- Create a file named
user_default.erlin your user home directory (mine is/Users/duncan), and paste in the following Erlang code:
-module(user_default).
-export([lm/0, cm/0]).
lm() ->
[purge_load(F) || F <- (fun() -> List = filelib:wildcard("*.beam"), lists:map(fun(I) -> list_to_atom(lists:takewhile(fun(C) -> C =/= $. end, I)) end, List) end)()].
cm() ->
[compile:file(F) || F <- (fun() -> List = filelib:wildcard("*.erl"), lists:map(fun(I) -> list_to_atom(lists:takewhile(fun(C) -> C =/= $. end, I)) end, List) end)()].
purge_load(M) ->
code:purge(M), code:load_file(M).
- Create a
.erlangfile in your user home directory, and paste in the following code:
code:load_abs("/Users/duncan/user_default").
io:format("Loading my Erlang shell..~n").
- Fire up the Erlang shell (make sure you are in your home directory) by typing
erlon the command prompt, and compile theuser_defaultmodule (note the absence of the.erlextension when compiling from within the shell).
> c(user_default).
You can also compile the user_default module from the terminal by directly invoking the Erlang compiler from the terminal (note the presence of the .erl extension when compiling from without the Erlang shell):
$> erlc user_default.erl
Both methods should result in the creation of a user_default.beam file.
- Close the Erlang shell (by typing
q().) and reopen it again, to allow the shell to take into account these new changes; if compilation was done usingerlc, open a new shell instead. Once loaded, you should see something like the following in your shell:
Erlang/OTP 18 [erts-7.3] [source-d2a6d81] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Loading my Erlang shell..
Eshell V7.3 (abort with ^G)
The first file user_default.erl exports two functions (you can add your own if you like):
-
lm/1loads all the compiled modules (the.beamfiles) found in the current directory. -
cm/1compiles all the Erlang source code files, but does not load them.
You will find these two utility functions useful especially whilst working on your assignment. To compile and load the Erlang modules in one step, you can type the following on the Erlang shell:
> cm(), lm().
A more neat way of doing this would be to add a new function, say clm/0, into the user_default.erl file like so:
clm() -> cm(), lm().
You can add it if you like. Don't forget to export the function correctly (i.e. -export([clm/0])), save the file, and recompile it again. Remember that you need to restart the current Erlang shell or open a new one for the changes to take effect. Test your new function by invoking clm/0 on the shell, for instance:
clm().
[{module,log},
{module,mobile},
{module,server_gen},
{module,switch},
{module,switch_naive},
{module,util}]
This completes the Erlang shell configuration guide.
We shall now configure our preferred lightweight text editor that will allows us code Erlang and GO.
VSC can be downloaded from code.visualstudio.com.
We provide Erlang language support by downloading the Erlang VSC plugin here.
With our Erlang and source code editor configuration complete, it's time to start programming 😁