Single File Elixir Programs
I was reading the learn you some Erlang for great good book and realized that escript functionality is easier to use in erlang than elixir. This post covers single file elixir programs. Single file programs are useful for problems that are a little too complicated for iex, but not complicated enough for a full blown mix project.
Thanks to everyone in #elixir-lang on Freenode who answered my questions about command line arguments and escript.
Software Versions
Instructions
All of the following scripts need to be executable to run.
A hello world erlang escript looks like this.
The corresponding elixir file looks like this.
Simple enough, but the above is only useful for programs that do not need user input. A single file erlang script that echoes command line arguments looks like this.
The corresponding elixir file looks like this.
The erlang version automatically routes commandline arguments to main. The elixir version does not. Slightly unhandy for organized programs, but this allows free floating code in elixir.
To go a step beyond single file programs, a full blow elixir escript program can be tested with a mix task.
Mix.Task.run “app.start” starts all of the applications in the project. Mix.Project.get.project[:escript][:main_module].main(args) runs the escipt main function with the supplied command line arguments.
This way the command line application can be tested without recompiling.