Sam Houston
Sam Houston

Reputation: 3651

Run exs file from within an exs file in elixir

I would like to run two elixir scripts, first.exs and second.exs. I would like second to also run first. What would be a nice way to run this?

For example, the code could look something like this:

first.exs

IO.puts "first"

second.exs

IO.puts "second"
System.cmd("mix run first.exs")

And would output something like this:

mix run first.exs
first

mix run second
first
second

I would like to avoid using System.cmd though and use the Mix module if possible

Upvotes: 4

Views: 3545

Answers (1)

Dogbert
Dogbert

Reputation: 222108

You can use Code.eval_file:

Code.eval_file "first.exs"
$ cat first.exs
IO.puts "first"
$ cat second.exs
IO.puts "second"
Code.eval_file "first.exs"
$ mix run second.exs
second
first

Upvotes: 12

Related Questions