我如何使用 import 或 exref 实现 Erlang 模块间通信

How do i implement Erlang intermodule communication using import or exref

我无法导入,因此无法在我的 'main' 模块中调用另一个模块的函数。我是 Erlang 新手。下面是我的 'main' 模块。

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

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

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

下面是正在导入的另一个模块。

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

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


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

我什至无法编译我的 'main' 模块 (Sysmod),一旦我导出导入的函数,因为它抛出一个 undefined shell 命令 错误。它在我导入模块和函数时编译,但无法执行该函数并抛出 undefined function 错误。我查看了 [] and also looked at [erlang man pages on code server]2。 此外,我成功地 add_path 了 Erlang Decimal 库,如下所示尝试我的应用程序与外部库通信。

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

但不能运行任何 Decimal 函数成功,如下所示。

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

简而言之,使用 import 进行模块间通信的第一种方法(我知道不推荐,因为不可读)是行不通的。当我寻找解决方案时,我意识到 add-path 仅适用于具有 ebin 目录的模块。

我卡住了。如何让我的模块相互通信,包括由 add_path 成功添加的外部库?我听说过 exref,但我无法掌握 Erlang 手册页。我需要有人像一个五岁的孩子一样向我解释。谢谢。

I am an Erlang newbie

不要使用 import。没有人这样做。这是糟糕的编程习惯。相反,通过函数的完全限定名称调用函数,即 moduleName:functionName

throws a undefined function error

您没有编译要导入的模块。此外,您不能导出未在模块中定义的函数。

这是一个例子:

~/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