Loading and compiling modules
It is possible to load code from source files into an IEx session. Multiple modules may be loaded and used, allowing us to incorporate existing code into our prototyping or idea testing session.
Getting ready
In this recipe, we will be importing two files that define the Greeter
and Echoer
modules into our IEx session.
In the following lines, we will list the contents of these modules:
code\greeter.ex defmodule Greeter do def greet(name \\ "you") do "Hello #{name} !" end end code/echoer.ex defmodule Echoer do def echo(msg) do IO.puts "#{msg} ... #{msg} ...... #{msg}" end end
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
How to do it…
We will follow these steps to load and compile the modules:
- Start IEx:
iex
- Load the
Greeter
module defined ingreeter.ex
:iex(1)> c("greeter.ex") [Greeter]
- Load the
Echoer
module defined inechoer.ex
:iex(2)> c("echoer.ex") [Echoer]
- Use the
greet
function defined in theGreeter
module:iex(3)> Greeter.greet("Me") "Hello Me !"
- Use the
echo
function defined in theEchoer
module:iex(4)> Echoer.echo("hello") hello ... hello ...... hello :ok
- Combine the functions defined in both modules:
iex(7)> Greeter.greet("Me") |> Echoer.echo Hello Me ! ... Hello Me ! ...... Hello Me ! :ok
Note
Some functions may have default values. They are denoted by the use of \\
. In the Greeter
module, the greet
function is defined as def greet(name \\ "you")
, which means that if we omit the argument passed to the function, it will default to you
.
How it works…
When c("file_name.ex")
is invoked from IEx, the file is loaded and compiled (a corresponding file with the .beam
extension will be created).
The module (or modules) defined on each imported file become available. It is possible to invoke functions on these modules using the ModuleName.function_name(args)
syntax.
If a module_name.beam
file exists for a given module, then every time you import that module into an IEx session, you will see the following warning:
module_name.ex:1: warning: redefining module ModuleName
The warning means that a new compiled .beam
file is being created, potentially redefining the module. If no changes were made to the source code, the code will be the same, although the warning is still issued.
In step 6, the pipe operator (|>
) is used to simplify the code. This operator means that the output of the left operation will be fed as the first argument to the right operation.
This is equivalent to writing the following:
Echoer.echo(Greeter.greet("Me"))
There's more…
In steps 2 and 3, the greeter.ex
and echoer.ex
files are imported without indicating the path because they are under the same directory from where the IEx session was started.
It is possible to use relative or full paths when loading files:
- We can use relative paths like this:
iex(1)> c("../greeter.ex")
- We can use full paths like this:
iex(2)> c("/home/user/echoer.ex")
Note
Note that the
c
IEx function accepts a string as an argument.