Three Ways How To Run Elixir Script — Tentamen Software Testing Blog
In the previous post, we presented how to run pure Erlang functions from Elixir. Today we explain three ways how to run Elixir script. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
IEx is Elixir interactive shell. When you run in your favorite terminal
iex command, a BEAM instance is started. You immediately run Elixir expressions, like 1 + 1, or any Kernel module function. Be aware that expressions are interpreted, not compiled. So their execution is slower. Kernel module functions are already precompiled, so there is no performance loss. When you run your own module, you can compile it form
iex using useful
For what purpose you can use
iex shell? I use it in my Phoenix application to experiment with the codebase. For example, when I need quickly to update a user record subscription expiration date. Or when I want to see the actual response when I try to find none existent Braintree transaction.
elixir the command is used to run a single elixir script:
What happens under the hood is this:
- BEAM instance is started, the script is compiled, and the .beam file is loaded into the memory. .beam file is not created on disk.
- Everything outside the module definition is interpreted. This is how to bootstraps script execution
- after the script is done, BEAM is stopped.
To distinguish such a script from Elixir code, we use
exs extension. Elixir Test Framework is interpreting test scripts, and you will learn that test scripts have
Here is a script example:
You can run it with:
elixir --no-halt script_example.exs
if your script is some kind of server. In this case, the BEAM instance will not be halted after script execution.
Mix tool helps you to manage the Elixir project.
mix new my_first_elixir_project
For now, just remember that this creates a folder with several Elixir files. For example, it creates the first test in the test folder.
You can run mix generated module with this:
mix run -e "IO.puts(MyFirstElixirProject.hello())"
You have three options to run Elixir code:
Originally published at https://blog.tentamen.eu on November 19, 2020.