escript的高级特性

escript Erlang scripting support, 可以让erl模块转身变成unix script来使用,大大方便用户,具体的使用参看otp文档。我这里要演示的是些比较被忽视的高级特性:

首先crack erts/etc/common/escript.c:33 static int debug = 1; 让之显示调用参数。

root@nd-desktop:~# cat >factorial
[color=red]#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose[/color]
main([String]) ->
try
N = list_to_integer(String),
F = fac(N),
io:format("factorial ~w = ~w\n", [N,F])
catch
_:_ ->
usage()
end;
main(_) ->
usage().

usage() ->
io:format("usage: factorial integer\n"),
halt(1).

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

CTRL+D

root@nd-desktop:~# chmod +x factorial
root@nd-desktop:~# ./factorial 10
 [color=red]erl +B -boot start_clean -noshell -smp enable -sname factorial -mnesia debug verbose -run escript start -extra ./factorial 10[/color]
factorial 10 = 3628800

[b]特性1:[/b]

摘抄文档。。。
On the third line (or second line depending on the presence of the Emacs directive), it is possible to give arguments to the emulator, such as

%%! -smp enable -sname factorial -mnesia debug verbose

Such an argument line must start with %%! and the rest of the line will interpreted as arguments to the emulator.

我们可以看到 这些选项被传递给了 erl
[b]
特性2: [/b]
-mode(compile).

这个选项是在escript.erl这个模块处理的。默认情况下 escript是被解释执行的,如果你的脚本很复杂,那么效率估计会是瓶颈。这种情况下, 你可以通过这个选项来让escript来先编译你的模块成opcode, 在vm里面运行。

[b]特性3:[/b]
-d 选项 用来调试script的
-c 编译执行
-i 解释执行
-s 只检查不执行
root@nd-desktop:~# escript -d ./factorial 10
我们就可以看到 调试界面如下图
[img]http://dl.iteye.com/upload/attachment/172159/1bac856a-ba5f-301c-8512-8c4d869f037c.png[/img]

[b]特性4:[/b]
可以把一个beam文件作为script

root@nd-desktop:/usr/src# cat hello.erl
-module(hello).
-export([start/0,main/1]).

main(_)->
start().

start()->
io:format("hello world~n",[]).

root@nd-desktop:/usr/src# erlc hello.erl

root@nd-desktop:/usr/src# cat >hello
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
CTRL+D

root@nd-desktop:/usr/src# cat hello.beam >>hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world


[b]特性5:[/b]
可以把一个zip文件作为script

root@nd-desktop:/usr/src# cat hello.erl
-module(hello).
-export([start/0,main/1]).

main(_)->
start().


start()->
io:format("hello world, fac(10)=~w ~n",[fac:fac(10)]).

root@nd-desktop:/usr/src# cat fac.erl
-module(fac).
-export([fac/1]).

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

root@nd-desktop:/usr/src# erlc *.erl
root@nd-desktop:/usr/src# erl
Erlang R13B03 (erts-5.7.4) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4 (abort with ^G)
1> zip:zip("hello.zip", ["hello.beam", "fac.beam"]).
{ok,"hello.zip"}
2>

root@nd-desktop:/usr/src# cat >hello
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose -escript main hello
CTRL+D

root@nd-desktop:/usr/src# cat hello.zip >>hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world, fac(10)=3628800

[b]特性6:[/b]
在独立的包里面 把escript伪装成我们的应用程序

root@nd-desktop:/usr/src# cat >hello.escript
-module(hello).
-export([start/0,main/1]).

main(_)->
start().

start()->
io:format("hello world~n",[]).

CTRL+D
root@nd-desktop:/usr/src# cp `which escript` hello
root@nd-desktop:/usr/src# ./hello
hello world

规则是 escript 改名成xxxx 执行xxxx的时候 实际上要读取的脚本是 xxxx.escript

综述: escript是很强大的 未来的erlang standalone全靠它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值