6.1开启和停止erlang shell
最简单的方法是Ctrl+c(windows下ctrl+break也可以) 然后按下A
也可以erlang:half(),不过这种方法停止有时会有问题,比如数据库程序,情已停止,下次打开要修复错误,在程序中最好用q(). 他会先刷新开启的文件,关闭开启的数据库,关闭OTP应用。q()是init:stop()命令在shell的别名。
6.2 配置开发环境
启动erlang shell 可以输入命令code:get_path()获取当前加载路径的设定值。
在开头增加新目录 code:add_patha(Dir)
在末尾增加新路径 code:add_pathz(Dir).
code:all_loaded(). [{io,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/io.beam"}, {erl_distribution,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/erl_distribution.beam"}, {edlin,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/edlin.beam"}, {zlib,preloaded}, {error_handler,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/error_handler.beam"}, {io_lib,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/io_lib.beam"}, {filename,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/filename.beam"}, {unicode,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/unicode.beam"}, {orddict,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/orddict.beam"}, {gb_sets,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/gb_sets.beam"}, {inet_db,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/inet_db.beam"}, {inet,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/inet.beam"}, {ordsets,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/ordsets.beam"}, {group,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/group.beam"}, {gen,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/gen.beam"}, {erl_scan,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/erl_scan.beam"}, {kernel,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/kernel.beam"}, {erl_eval,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/erl_eval.beam"}, {prim_file,preloaded}, {ets,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/ets.beam"}, {lists,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/lists.beam"}, {sets,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/sets.beam"}, {inet_udp,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/inet_udp.beam"}, {io_lib_pretty,"/usr/lib64/erlang/lib/stdlib-1.17.1/ebin/io_lib_pretty.beam"}, {code,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/code.beam"}, {ram_file,"/usr/lib64/erlang/lib/kernel-2.14.1/ebin/ram_file.beam"}, {dict,[...]}, {packages,...}, {...}|...]
如果怀疑加载了错误的模块 可以用code:all_loaded().返回加载的模块列表。
也可以启动时添加
erl -pa Dir1 -pa Dir2 -pz Dir3 .....
关于.erlang 文件:启动erlang是 系统会优先读取这个文件的内容,可以再erlang_home里 也可以在当前目录(如果有 ,优先读取当前目录的.erlang),他可以随意创建
内容可以如下:
io:format("running erlang ~p").
code:add_patha("."). code:add_patha("home/joe").
在这些启动文件中通常加入一些打印信息,否则可能不知道启动的哪些文件。
erlang的home获取方法 1> init:get_argument(home). {ok,[["C:\\Users\\zhiqiang"]]}
6.3运行程序的几种不同的方法
1、c(module).编译 然后运行
2、erlc hello.erl 编译
erl -noshell -s helllo start -s init stop
[root@web crypto]# erl -eval 'io:format("memory:~p~n",[erlang:memory(total)]).' -shell -s init stop Erlang R14B (erts-5.8.1) [source] [64-bit] [rq:1] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.8.1 (abort with ^G) 1> memory:76220800
-nosell 启动erlang但是关闭shell, 不会看到Erlang R14B (erts-5.8.1) [source] [64-bit] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]这行提示信息。
-s hello start 表示运行hello:start().
注:-compile(export_all)会到处所有函数,在便携使用它 完成编码是要去掉。
3、把程序当成escript
6.4、使用makefile进行自动编译
6.5、在erlang shell中的命令编辑
6.6、解决系统死锁
6.7 如何应对故障