Comparing
version 8 and
version 7 backh1. Random Erlang Pieces of Code
See also:
* [[erlangconnect|Interconnecting Erlang Nodes]]
h2. Add directory to the binaries search path
Add these lines to your _~/.erlang_
code:add_path("/ebin").
code:add_path("/ebin").
h2. Working on the remote node
h3. Open a shell
pr@local> {Press Ctrl+G}
User switch command
--> j
1* {shell,start,[init]}
--> r 'dummy@remote'
--> j
1 {shell,start,[init]}
2* {dummy@remote,shell,start,[]}
--> c 2
Eshell V5.5.4 (abort with ^G)
(dummy@remote)1>
h3. Spawn a process
remote$ % cat dummy.erl
-module(dummy).
-export([loop/0]).
loop() ->
receive
{Node, Msg} ->
io:format("~s : ~s~n", [Node, Msg]),
loop();
_ -> io:format('not defined~n')
end.
remote$ erl -sdummy
dummy@remote> c(dummy).
local$ erl -sname hello
hello@local> Node = 'dummy@remote'.
hello@local> net_adm:ping(Node).
%% Format: Pid = spawn(Node, mod_name, func_name, [arg1,arg2]).
hello@local> Pid = spawn(Node, dummy, loop, []).
hello@local> Pid ! {self(), 'Hello'}.
h3. Call a procedure
hello@local> rpc:call('dummy@remote', dummy, some_func, [10, arg2]).
h2. Convert command line argument to integer
-module(dummy).
-export([main/1]).
main([Arg|_]) ->
Val = list_to_integer(atom_to_list(Arg)),
...
host$ erl -noshell -s dummy main 1000 -s init stop
h2. String2Pid
1> self().
<0.29.0>
2> list_to_pid("<0.29.0>") ! hello.
h2. for(1,N,Fun) cycle (repeat some function N times)
for(N, N, _) -> done;
for(N, J, Fun) -> Fun(J), for(N, J+1, Fun).