Elixir multi-file scripting

507 views
Skip to first unread message

Yixin Jin

unread,
Apr 15, 2015, 4:33:10 PM4/15/15
to elixir-l...@googlegroups.com
Hi folks,

As a fan of Erlang, I am naturally drawn to Elixir, especially its meta-programming capability with macros. I'd like to create a DSL out of it but wanted to avoid compilation phase. Is there a way to create an application using multiple Elixir script files, just like other script languages do, such as tcl, perl, etc? I know .exs can be run as a script. So the question focus more on getting the job done using multiple of them. In fact, one part of the whole project is a library that contains supporting code that seldom changes. This part will be compiled. The other part is a set of Elixir scripts that make use of the DSL, the aforementioned library. They will change often. This part is the one I want to spare the compilation.

Thanks
Yixin

Booker Bense

unread,
Apr 15, 2015, 6:20:43 PM4/15/15
to elixir-l...@googlegroups.com
You can load code at runtime using  Code.require_file. This still requires compiling the .exs file 
to erlang bytecode. ( BTW this is more or less how tcl,perl, ruby, etc... work, they just keep the byte
code in memory, rather than in a file. )

There really isn't any way to run elixir code currently other than compiling it. 
It's just a question of when you compile it. 

What you are describing sounds very much like Mix Tasks. 

- Booker C. Bense

Yixin Jin

unread,
Apr 16, 2015, 11:08:41 AM4/16/15
to elixir-l...@googlegroups.com
Thanks, Booker. I guess what I was asking may be a bit out of Elixir's normal practice.

Booker Bense

unread,
Apr 16, 2015, 11:39:52 AM4/16/15
to elixir-l...@googlegroups.com
Not really, you can feed elixir "scripts" to iex. In fact iex, does pretty much exactly what you 
want. 

iex -S script

The tricky part is that the imperative flow that is implied in the idea of script doesn't always translate
to elixir functions. 

- Booker C. Bense

Yixin Jin

unread,
Apr 16, 2015, 11:47:48 AM4/16/15
to elixir-l...@googlegroups.com
The DSL I am contemplating is similar to what ExUnit does, but for system-wide testing. So any imperative flow will be used in a limited scope.

I recall that erlang's compile:file() has binary option, which makes it compile the file into object code without generating the object file. I wonder if there is something similar in Elixir. Or is there a way to first translate scripts into erlang code, then use compile:file() to translate it to binary object code, then use Code.require_file() to load it in memory?

Eric Meadows-Jönsson

unread,
Apr 16, 2015, 2:25:40 PM4/16/15
to elixir-l...@googlegroups.com
`Code.require_file` and `Code.load_file` both load elixir files without producing compiled modules. The files are being compiled and then evaluated but the compilation process only loads the modules into memory, they do not produce compiled .beam files. 

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/b593b077-9e25-41e4-b3f4-e2de297274ad%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson

Booker Bense

unread,
Apr 16, 2015, 2:25:59 PM4/16/15
to elixir-l...@googlegroups.com
Code.require_file does load it into memory. It also returns an list of tuples which include the module name and BEAM byte code for the module. 

Once you Code.require_file, those modules and functions are available for use. 

- Booker C. Bense 

Yixin Jin

unread,
Apr 16, 2015, 2:45:32 PM4/16/15
to elixir-l...@googlegroups.com
Eric/Booker,

Thanks for the explanation. Littering beam files was what I was worried about. From the doc page of Code.require_file(), it isn't obvious to me that it doesn't generate beam file. Now, it appears to be exactly what I wanted. I will certainly try it out.

Thanks again. :)
Reply all
Reply to author
Forward
0 new messages