Search code examples
erlangerlang-shell

How to execute a function of an Erlang module from a shell?


I have a novice question. I'm reading the book "Erlang Programming" and working on exercises after every chapter.

In the situation that I have a db.erl module, and inside it I've placed several functions for several exercises. After a few days of playing with exercises I've grown tired from repeating "compile->execute exercise-function" actions in the erl shell. Let's say I'm working on new() function in the db.erl module; after each modification of this function, I'm forced to do next in erl:

c(db.erl).
db:new().

again and again. Sometimes I forget to re-load my module and the results are confusing. I can compile/load through OS shell using: erl -compile file.erl; erl -make, but I did not find a way to run specific function from specific module. Any suggestions?


Solution

  • You can use -eval option:

    $ erl -eval 'io:format("printing ~p~n", [{any, term}]), init:stop()' -noshell
    printing {any,term}
    

    Read documentation here: http://www.erlang.org/doc/man/erl.html

    And you can use just erlc to compile files: http://erlang.org/doc/man/erlc.html