如何从其他erlang文件导入erlang文件

f3temu5u  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(210)

如何从另一个Erlang文件导入Erlang文件。我想导入整个脚本,而不仅仅是特定的模块。例如:我们如何在另一个Erlang脚本中导入下面的文件??

% hello world program
-module(helloworld). 
-export([start/0]). 

start() -> 
   io:fwrite("Hello, world!\n").
wyyhbhjk

wyyhbhjk1#

只需使用-import指令将其导入即可。下面是导入helloworld模块的示例:

-module(top).
-import(helloworld, [start/0]).
-export([myfun/0]).

myfun() ->
    start().

如果你把上面的代码保存在top.erl中,你就可以在shell中这样使用它:

$ erl
Erlang/OTP 22 [erts-10.7.1] [source] [64-bit] [smp:6:6] [ds:6:6:10] [async-threads:1] [hipe] [dtrace]

Eshell V10.7.1  (abort with ^G)
1> c(top).
{ok,top}
2> top:myfun().
Hello, world!
ok
3lxsmp7m

3lxsmp7m2#

I'm able to run it from shell as you have mentioned above but not able to run by using this command:

erl -noshell -s top start -s init stop

even after creating ".beam" files.

~/erlang_programs$ ls a.*
a.erl

~/erlang_programs$ cat a.erl
-module(a).
-compile(export_all).

go() ->
   io:fwrite("hello\n").
~/erlang_programs$ erlc a.erl
a.erl:2: Warning: export_all flag enabled - all functions will be exported

~/erlang_programs$ ls a.*
a.beam  a.erl    

~/erlang_programs$ erl -noshell -s a go -s init stop
hello

Now, if your module is actually in a different directory than the directory where you are issuing the erl command, then you can use a flag to specify the path to the .beam file:

~/erlang_programs$ cd ..
~$ 

~$ erl -noshell -s a go -s init stop -pa ./erlang_programs
hello

Note that the module you posted is named helloworld and the function in that module is named start , yet your erl command is:

erl -noshell -s top start -s init stop

That erl command requires a module named top to be present in the current directory.

相关问题