Archive

Posts Tagged ‘load module erlang’

How to load a module to a connected node in Erlang

July 12, 2010 Leave a comment

If you are in Erlang, you should have used somehow one or more Erlang nodes, even running in different computers.

A couple of weeks ago I was running a test of mine with two interconnected nodes, when I step into a problem: I had a module in the directory where I started the first node, but I wanted to use it also in the other node.

I didn’t want to replicate the beam file, so I took a look at c module documentation, and there I found the solution. The c module enables users to enter the short form of some commonly used commands; these functions are intended for interactive use in the Erlang shell only therefore the module prefix may be omitted.

I strongly recommend you this module, in test cases it is very useful.

I will show you how I fixed my problem.

First thing, start two nodes in different directories, in one directory  put the beam file of your module.

erl -sname node1@localhost -setcookie cookie
1>ls().
....
....
module.beam
2>nl(module).
abcast

In the example above, we started the first node (the one in the same directory of  module.beam), then using the function ls() we listed all the files in the directory. Using the function nl(module) we loaded module on all connected nodes (note that there is also nc(module) which compiles and then loads the code for a file on all nodes.

Here we have the second node (remember to start it before using nl()!):

erl -sname node2@localhost -setcookie cookie
1>m().
Module      File
....           ....
module      yourpath
3>module:function().
output of the function

As you can see the function m() displays information about the loaded modules, including the files from which they have been loaded, in the list given as output from m() you should have also your module, and from now you can use it as a normal module.

Advertisements