Programming Erlang读书笔记6: 编译和运行Erl程序

本文介绍了Erlang编程的基础知识,包括启动和停止Eshell、查看及添加代码路径的方法、运行Erlang程序的不同方式、使用Makefile构建Erlang程序、获取帮助的方式以及处理程序崩溃的方法。
启动和停止Eshell
[code]
$ erl

1> halt().
[/code]

查看/添加代码查找路径
[code]
code:get_path().
code:add_patha(Dir).
code:add_pathz(Dir).

erl -pa Dir1 -pa Dir2 ... -pz DirK1 -pz DirK2
[/code]

查看载入的module和查看出错的module
[code]
code:all_loaded().
code:clash().
[/code]

可以将code:add_patha()和code:add_pathz()扔到.erlang文件

运行Erl程序的几种方式:
hello.erl
[code]
-module(hello).
-export([start/0]).

start() ->
io:format("Hello world~n").

%%%%%%%%%%%%%
$ erl
1> c(hello).
2> hello:start().
%%%%%%%%%%%%%
$ erlc hello.erl
$ erl -noshell -s hello start -s init stop
[/code]

Quick Scripting
[code]
erl -eval 'io:format("Memory: ~p~n", [erlang:memory(total)]).' -noshell -s init stop
[/code]

hello.sh
[code]
#!/bin/sh
erl -noshell -pa /home/joe/code -s hello start -s init stop
[/code]

接受命令行参数
[code]
-module(main).
-export([main/1]).

fac(0) -> 1;
fac(N) -> N*fac(N-1).

main([A]) ->
I = list_to_integer(atom_to_list(A)),
F = fac(I),
io:format("factorial ~w = ~w~n", [I, F]),
init:stop().

%%%%%%%%%%%%
$ erlc main.erl
$ erl -noshell -s main main 25
factorial 25 = 15511210043330985984000000
[/code]

使用Makefile构建Erl程序
[code]
% Makefile.template
# leave these lines alone
.SUFFIXED: .erl .beam .yrl

.erl.beam:
erlc -W $<

.yrl.erl:
erlc -W $<

ERL = erl -boot start_clean

# Here's a list of the erlang modules you want compiling
# If the modules don't fit onto one line add a \ character
# to the end of the lien and continue on the next line

# Edit the lines below
MODS = module1 module2 \
module3 ... special1 ...\
...
moduleN

# The first target in any makefile is the default target.
# If you just type "make" then "make all" is assumed (because
# "all" is the first target in this makefile)

all: compile

compile: ${MODS:%=%.beam} subdirs

## special compilation requirements are added here

special1.beam: special1.erl
${ERL} -Dflag1 -WO special1.erl

## run an application from the makefile

application1: compile
${ERL} -pa Dir1 -s application1 start Arg1 Arg2

# the subdirs target compiles any code in
# sub-directories

subdirs:
cd dir1; make
cd dir2; make
...

# remove all the code

clean:
rm -rf *.beam erl_crash.dump
cd dir1; make clean
cd dir2; make clean
[/code]

Getting Help
[code]
$ erl -man lists
[/code]
不过男人不支持windows

如果Erlang crash掉了,它会生成一个erl_crash.dump文件,有一个基于Web的crash分析工具
[code]
1> webtool:start().
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值