The erlangrc file

There could be an erlang resource file named as “.erlang” in your working
directory or home directory. This file is loaded and evaled when erlang is
started. So you can do some initialization in this file.

Let’s look into start_clean.script and start_sasl.script.

...
{progress,applications_loaded},
{apply,{application,start_boot,[kernel,permanent]}},
{apply,{application,start_boot,[stdlib,permanent]}},
%% the next line is only present in start_sasl.script
{apply,{application,start_boot,[sasl,permanent]}},     
{apply,{c,erlangrc,[]}},
{progress,started}]}.

After kernel, stdlib and sasl is started, c:erlangrc/0 will be called.

%% erlangrc(Home)
%%  Try to run a ".erlang" file, first in the current directory
%%  else in home directory.

erlangrc() ->
    case init:get_argument(home) of
    {ok,[[Home]]} ->
        erlangrc([Home]);
    _ ->
        f_p_e(["."], ".erlang")
    end.

erlangrc([Home]) ->
    f_p_e([".",Home], ".erlang").

f_p_e(P, F) ->
    case file:path_eval(P, F) of
    {error, enoent} = Enoent ->
        Enoent;
    {error, E={Line, _Mod, _Term}} ->
        error("file:path_eval(~p,~p): error on line ~p: ~s~n",
          [P, F, Line, file:format_error(E)]),
        ok;
    {error, E} ->
        error("file:path_eval(~p,~p): ~s~n",
          [P, F, file:format_error(E)]),
        ok;
    Other ->
        Other
    end.

Something we should know about file:path_eval:

path_eval(Path, Filename) -> {ok, FullName} | {error, Reason}

Searches the path Path (a list of directory names) until the file
Filename is found. If Filename is an absolute file name, Path is
ignored. Then reads and evaluates Erlang expressions, separated
by ’.’ (or ’,’, a sequence of expressions is also an expression),
from the file. The actual result of evaluation is not returned; any
expression sequence in the file must be there for its side effect.
Returns one of the following:

c:erlangrc/0 is wrapped in shell_default, so you can type erlangrc([“.”]).
in the erlang shell to reload your .erlang file at any time.

Advertisements
This entry was posted in erlang and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s