Wednesday, January 16, 2008

Reloading all code from the Erlang shell (take two)

So, it turns out that the snippet from earlier doesn't quite work right.

Thanks to #erlang on freenode, I found out that you can add commands to the shell by putting them in the user_default module. So here's the improved version:

In ~/erl/user_default.erl


-module(user_default).

-export([la/0]).

la() ->
Modules = [M || {M, P} <- code:all_loaded(), is_list(P) andalso string:str(P, "amiest") > 0],
[shell_default:l(M) || M <- Modules].


Then compile that module using c(user_default) from within ~/erl.

In your ~/.erlang:

code:load_abs("/home/amiest/erl/user_default").

2 comments:

Eugene said...

Hey Todd,
Take a look at:
reloader.erl

You just start your erlang shell with
-run reloader start for development purposes, and your code is reloaded when it needs to be.

marco m said...

I am new to Erlang and just discovered this blog, that I really appreciate.

I have a suggestion to avoid hardcoding the user home dir, use POSIX getenv:

instead of
string:str(P, "/home/amiest")
use
string:str(P, os:getenv("HOME"))