home | tech | misc | code | bookmarks (broken) | contact | README

Erlang notes

General tips

In this section I just want to document stuff for people that, like me, are beginners. Although there is very simple stuff for someone who has already some experience with Erlang, I think it is worth to document and it may be helpful for those who are starting to learn the language.

I'm also learning the language! Tips below probably have wrong advices so, if you see something strange, please, let me known.

Node communication

I was starting a multi process application with two nodes, say node_a and node_b, and I was having problems to understand how to make both to get connected. First, when executing a node with erl, you have to pass the -sname flag (or the -name flag, but with a different syntax) with the node name. Let's first both files contents:



main() ->
    register(sendmsg, self()),
    {receivemsg, 'node_receive@tifa'} ! "a message".



main() ->
    register(receivemsg, self()),

loop() ->
        Msg -> io:format("~p~n", [Msg])

Now, how you execute it? Run:

$ erlc receivemsg.erl&& erl -noshell -sname node_receive -run receivemsg main

And in another different shell, run:

$ erlc sendmsg.erl && erl -noshell -sname node_send -run sendmsg main

Note we are executing the receive node first.

The important option we pass to erl is -sname, for the name of the node. When passing the message from one process to another, in a different node, we need to use the syntax {Process, Node} ! Msg. The Node contents name should be the same we passed as argument to the -sname option.

Unless you know the Pid of the Process, it might be a good idea to register it so you can use atoms to refer to it.

Note that the sending process should also be run in a known node, i.e., it should also have the -sname option, otherwise it will not be able to send message to the receiving process.


Error with Mnesia: "no function clause matching ..."

If you get a crypt error like that:

4> test:vai().
** exception error: no function clause matching
     in function  test:customer_all/0

Check if you insert the following line on the top of your file:


The inclusion of the qlc.hrl header is necessary if you want to use Mnesia in your module. Got this tip from this reference.