Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load all erlang modules in path

Tags:

module

erlang

erl

Using the answer from Easy way of loading projects with rebar dependencies, dependencies are now automatically resolved, but they are not automatically loaded.

So, how can I load all the modules in my ebin and /deps/*/bin path automatically? That way they are available when using the Erlang shell tab completion, which speeds up my dev process considerably.

My solution based on the great answer of Adam Lindberg: https://gist.github.com/1131312 It will only load the project modules automagically, so almost no delay in erl startup.

like image 560
Ward Bekker Avatar asked Aug 03 '11 08:08

Ward Bekker


People also ask

Which function is used to compile an Erlang file?

compile:file(File, Options). File is a file name, and Options is a list of compiler options. Refer to the Reference Manual for a complete list of Options . You can also enter the command c(File) from the Erlang shell.

Is Erlang interpreted or compiled?

Erlang programs must be compiled to object code. The compiler can generate a new file that contains the object code. The current abstract machine, which runs the object code, is called BEAM, therefore the object files get the suffix .

How do I exit Erlang shell?

Well if you're in the shell of the node you want to turn down, even if it's not receiving input, you can still press Ctrl-g (which takes you to JCL mode). Once there you can use the command q to quit the Erlang shell. This is similar in effect to erlang:halt(0).


1 Answers

This snippet would do the trick:

[code:ensure_loaded(list_to_atom(filename:rootname(filename:basename(F))))
 || P <- code:get_path(), F <- filelib:wildcard(P ++ "/*.beam")].

Put it in your ~/.erlang file as one row (including the dot: .) and it will be executed upon starting any Erlang shell. Be warned though, it's hideously slow!

» time erl -noshell -s init stop
erl -noshell -s init stop  0.11s user 0.02s system 11% cpu 1.143 total # Without
» time erl -noshell -s init stop
erl -noshell -s init stop  7.31s user 1.08s system 88% cpu 9.480 total # With 
like image 123
Adam Lindberg Avatar answered Oct 10 '22 08:10

Adam Lindberg