PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

[FIXED] How do i implement Erlang intermodule communication using import or exref

 August 09, 2022     decimal, erlang, erlang-shell     No comments   

Issue

I cannot import and therefore cannot call a function of another module in my 'main' module. I am an Erlang newbie. Below is my 'main' module.

-module('Sysmod').
-author("pato").

%% API
-export([ hello_world/0,sayGoodNight/0]).
-import('Goodnight',[sayGoodNight/0]).

hello_world()->
  io:fwrite("Hello World\n").

Below is the other module which is being imported.

-module('Goodnight').
-author("pato").

%% API
-export([sayGoodNight/0]).


sayGoodNight() ->
  io:format("Good night world\n"). 

I cannot even compile my 'main' module(Sysmod), once I export the imported function, as it throws an undefined shell command error. It compiles when I have imported the module and function, but cannot execute the function and it throws a undefined function error. I have checked out [this answer]1 and also looked at [erlang man pages on code server]2. In addition, I managed to add_path of the Erlang Decimal library successfully as below attempting my app to talk to an external library.

12> code:add_path("/home/pato/IdeaProjects/AdminConsole/erlang-decimal-master/ebin").         true

But cannot run any Decimal function successfully as shown below.

 decimal:add("1.3", "1.07").** exception error: undefined function decimal:add/2

In short, the first method,(I know its not recommended due to unreadability) of intermodule communication using import is not working. As I looked for a solution, I realized the add-path only works with modules that have an ebin directory.

I am stuck. How do I make my modules talk to each other including an external library added successfully by add_path? I have heard of exref, i cannot grasp the Erlang man pages. I need someone to explain it to me like a five year old. Thank you.


Solution

I am an Erlang newbie

Don't use import. No one else does. It's bad programming practice. Instead, call functions by their fully qualified names, i.e. moduleName:functionName

throws a undefined function error

You didn't compile the module you are trying to import. Also, you can't export a function that is not defined within a module.

Here's an example:

~/erlang_programs$ mkdir test1
~/erlang_programs$ cd test1
~/erlang_programs/test1$ m a.erl  %%creates a.erl
~/erlang_programs/test1$ cat a.erl
-module(a).
-compile(export_all).

go()->
    b:hello().

~/erlang_programs/test1$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.3  (abort with ^G)
1> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

2> a:go().
** exception error: undefined function b:hello/0
3> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution

~/erlang_programs/test1$ mkdir test2
~/erlang_programs/test1$ cd test2
~/erlang_programs/test1/test2$ m b.erl  %%creates b.erl
~/erlang_programs/test1$ cat b.erl
-module(b).
-compile(export_all).

hello() ->
    io:format("hello~n").

~/erlang_programs/test1/test2$ erlc b.erl
b.erl:2: Warning: export_all flag enabled - all functions will be exported
~/erlang_programs/test1/test2$ ls
b.beam  b.erl

~/erlang_programs/test1/test2$ cd ..
~/erlang_programs/test1$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.3  (abort with ^G)
1> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
2> a:go().
** exception error: undefined function b:hello/0
3> code:add_path("./test2").
true
4> a:go().                  
hello
ok


Answered By - 7stud
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing