<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; escript</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/escript/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>escript的高级特性</title>
		<link>http://blog.yufeng.info/archives/153</link>
		<comments>http://blog.yufeng.info/archives/153#comments</comments>
		<pubDate>Tue, 24 Nov 2009 21:45:55 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[escript]]></category>
		<category><![CDATA[mode]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=153</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: escript的高级特性 escript Erlang scripting support, 可以让erl模块转身变成unix script来使用，大大方便用户，具体的使用参看otp文档。我这里要演示的是些比较被忽视的高级特性: 首先crack erts/etc/common/escript.c:33 static int debug = 1; 让之显示调用参数。 CTRL+D [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 特性1： 摘抄文档。。。 On the third line (or second line depending on the presence of [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/153">escript的高级特性</a></p>
</div>
<p>escript Erlang scripting support, 可以让erl模块转身变成unix script来使用，大大方便用户，具体的使用参看otp文档。我这里要演示的是些比较被忽视的高级特性:</p>
<p>首先crack erts/etc/common/escript.c:33  static int debug = 1; 让之显示调用参数。</p>
<pre class="brush: bash; title: ; notranslate">root@nd-desktop:~# cat &amp;gt;factorial</pre>
<pre class="brush: erlang; title: ; notranslate">
[color=red]#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose[/color]
main([String]) -&gt;
try
N = list_to_integer(String),
F = fac(N),
io:format(&quot;factorial ~w = ~w\n&quot;, [N,F])
catch
_:_ -&gt;
usage()
end;
main(_) -&gt;
usage().

usage() -&gt;
io:format(&quot;usage: factorial integer\n&quot;),
halt(1).

fac(0) -&gt; 1;
fac(N) -&gt; N * fac(N-1).
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:~# chmod +x factorial
root@nd-desktop:~#　./factorial  10
</pre>
<p>[color=red]erl +B -boot start_clean -noshell -smp enable -sname factorial -mnesia debug verbose -run escript start -extra ./factorial 10[/color]<br />
factorial 10 = 3628800</p>
<p><strong>特性1：</strong></p>
<p>摘抄文档。。。<br />
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</p>
<p>%%! -smp enable -sname factorial -mnesia debug verbose</p>
<p>Such an argument line must start with %%! and the rest of the line will interpreted as arguments to the emulator.</p>
<p>我们可以看到　这些选项被传递给了 erl</p>
<p><strong>特性2： </strong><br />
-mode(compile).</p>
<p>这个选项是在escript.erl这个模块处理的。默认情况下 escript是被解释执行的，如果你的脚本很复杂，那么效率估计会是瓶颈。这种情况下， 你可以通过这个选项来让escript来先编译你的模块成opcode, 在vm里面运行。</p>
<p><strong>特性3：</strong><br />
-d 选项 用来调试script的<br />
-c  编译执行<br />
-i  解释执行<br />
-s  只检查不执行<br />
root@nd-desktop:~#　escript -d ./factorial  10<br />
我们就可以看到 调试界面如下图<br />
<a href="http://blog.yufeng.info/wp-content/uploads/2009/11/escript_debug.PNG"><img class="alignnone size-medium wp-image-154" title="escript_debug" src="http://blog.yufeng.info/wp-content/uploads/2009/11/escript_debug-275x300.PNG" alt="escript_debug" width="275" height="300" /></a></p>
<p><strong>特性4：</strong><br />
可以把一个beam文件作为script</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(hello).
-export([start/0,main/1]).

main(_)-&gt;
start().

start()-&gt;
io:format(&quot;hello world~n&quot;,[]).
</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# erlc hello.erl
root@nd-desktop:/usr/src# cat &amp;gt;hello
</pre>
<pre class="brush: erlang; title: ; notranslate">
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.beam &amp;gt;&amp;gt;hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world
</pre>
<p><strong>特性5:</strong><br />
可以把一个zip文件作为script</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(hello).
-export([start/0,main/1]).

main(_)-&gt;
start().

start()-&gt;
io:format(&quot;hello world, fac(10)=~w ~n&quot;,[fac:fac(10)]).</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat fac.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(fac).
-export([fac/1]).

fac(0) -&gt;
1;
fac(N) -&gt; N * fac(N-1).</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# erlc *.erl
root@nd-desktop:/usr/src# erl
Erlang R13B03 (erts-5.7.4)
[smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1&amp;gt; zip:zip(&quot;hello.zip&quot;, [&quot;hello.beam&quot;, &quot;fac.beam&quot;]).
{ok,&quot;hello.zip&quot;}
2&amp;gt;
</pre>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat &amp;gt;hello
</pre>
<pre class="brush: erlang; title: ; notranslate">
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose -escript main hello
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat hello.zip &amp;gt;&amp;gt;hello
root@nd-desktop:/usr/src# chmod +x hello
root@nd-desktop:/usr/src# ./hello
hello world, fac(10)=3628800
</pre>
<p><strong>特性6：</strong><br />
在独立的包里面 把escript伪装成我们的应用程序</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src# cat &amp;gt;hello.escript
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(hello).
-export([start/0,main/1]).

main(_)-&gt;
start().

start()-&gt;
io:format(&quot;hello world~n&quot;,[]).
</pre>
<p>CTRL+D</p>
<pre class="brush: bash; title: ; notranslate">
root@nd-desktop:/usr/src#  cp `which escript` hello
root@nd-desktop:/usr/src# ./hello
hello world
</pre>
<p>规则是 escript 改名成xxxx 执行xxxx的时候 实际上要读取的脚本是 xxxx.escript</p>
<p><strong>综述： escript是很强大的 未来的erlang standalone全靠它。</strong></p>
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/153/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

