`
hideto
  • 浏览: 2650424 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
启动和停止Eshell
$ erl

1> halt().


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

erl -pa Dir1 -pa Dir2 ... -pz DirK1 -pz DirK2


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


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

运行Erl程序的几种方式:
hello.erl
-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


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


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


接受命令行参数
-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


使用Makefile构建Erl程序
% 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


Getting Help
$ erl -man lists

不过男人不支持windows

如果Erlang crash掉了,它会生成一个erl_crash.dump文件,有一个基于Web的crash分析工具
1> webtool:start().
分享到:
评论
3 楼 337240552 2012-06-27  
表示不会整
2 楼 jahson 2009-01-15  
很实用的入门文章,刚开始学习,谢谢
1 楼 blackanger 2008-01-08  
来学习下Erlang,

PS : 发现个blog:http://hedito.iteye.com/

相关推荐

Global site tag (gtag.js) - Google Analytics