December 16th Wednesday 2009

今天从Mr. yufeng的blog上学了几招。先抄袭一下。^_^别拍我,我也为了查找方便嘛!

 

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

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

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

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
 erl +B -boot start_clean -noshell -smp enable -sname factorial -mnesia debug verbose -run escript start -extra ./factorial 10
factorial 10 = 3628800

特性1:

摘抄文档。。。
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

特性2:

-mode(compile).

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

特性3:
-d 选项 用来调试script的
-c  编译执行
-i  解释执行
-s  只检查不执行
root@nd-desktop:~# escript -d ./factorial  10
我们就可以看到 调试界面如下图 
 

 

(这是我在自己的vm上实验的截图,那个GUI的debug画面是自己跳出来的,cool!)

特性4:
可以把一个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


特性5:
可以把一个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

特性6:
在独立的包里面 把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全靠它。

特性5,没有成功哇!下面截图。不知道,别人会不会遇上同样现象。

我现在调查原因。

 

 

 

 

另外,今天修改了nginx用来构建makefile的脚本。nginx的http, mail与其他我不感兴的代码,都被我去掉了。nginx的核心就是个用来开发高性能服务器的框架。http服务也好,mail服务也好都是有上面做的开发。但是这个是面向短连接的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值