ErLangはメモ3-moduleを勉強します

5999 ワード


10. modules 
いよいよモジュールに入ります....
モジュールとは、いくつかのコマンドセットを1つのモジュールに整理することです.実は前にmodulesにも触れました例えばhead,tail
 
1.コアモジュールの使用文法:Module:Function(Arguments).
 
1> erlang:element(2,{2,3,4}).     // 
3
2> element(2,{a,b,c}).
b
3> lists:seq(1,5).
[1,2,3,4,5]
4> seq(1,5).
** exception error: undefined shell command seq/2
 
 
一部の関数では、elementなどのmoduleを作成する必要はありませんが、Moduleを作成する必要があります.例えばseq.このjavaのlangパケット下のAPIとはパケット名を導入する必要はなく、他のパケット下の関数を導入する必要がある.
理論的には、moduleは一般的にいくつかの共通特性関数の集合である.
 
2.モジュールの説明
一般的にmoduleには、プロパティと関数の2つのセクションがあります.
プロパティ:このmoduleの説明で、説明機能が含まれています.メタデータです.
関数:外部に露出する機能で、外部にアクセスできます.
 
3.自分の最初のモジュールを書く
 
まずvim first.Erl vim編集環境へ
これはmoduleだと宣言して、次の行のコードをノックします.
 
3.1 firstという名前のmoduleを定義します.方式は基本的にjavaと同じです.
 
-module(first).             // , . , erlang . 。
 
 
3.2定義関数
 
add(A,B) -> A + B.         // add。  A,B 。 ! , atom
 
 
3.3関数は定義して、どのように対外宣言して私達が1つの関数を定義したと言います
 
-export([add/2]).          //export add: , 2 。 , , java 
 
 
完成したmoduleが完成しました!完全なコードは次のとおりです.
 
-module(first).

-export([add/2]).

add(A,B) -> A + B.
 
 
3.4次はmoduleのコンパイルです!
コマンドラインの下でerlc flag fileを使用できます.具体的なflagは後で表にします.erlc first.Erlでいいです.
erlangにおけるshell環境であればc(first)を用いる.すぐ
コンパイルするとfirstが生成されます.beamファイルはjavaのclass、共erlangの仮想マシン解析に似ています
 
3.5運転効果を見る
 
1> cd('/home/inter12/work/erl').    // module 
/opt/home/inter12/work/erl
ok
2> first:add(1,2).                //  module:fun(arg,..)
3
 
 
4.複雑なmodule
 
-module(first).
-export([add/2,hello/0,greet_and_add_two/1]).    // ,  -export([Function1/Arity, Function2/Arity, ..., FunctionN/Arity]).

add(A,B) ->
 A + B.


%%show greeeting 
%%io format is the standard output text     
hello() ->
 io:format("hello world~n").             //io  module,format 


greet_and_add_two(X) ->                 //  , !
  hello(),
  add(X,2).
 
 
注意点:greet_and_add_two関数では他の2つの関数が呼び出され、importは行われません.ioは自身が提供する関数であり、addはこのmoduleで宣言されているため、他の関数を呼び出す必要がある場合はインポートします.構文は次のとおりです.
-import(Module, [Function1/Arity, ..., FunctionN/Arity]).
 
実行結果を表示:
 
1> first:add(1,2).
3
2> first:hello().
hello world
ok                        // OK , erlang , ok 。 !
3> first:greet_and_add_two(3).
hello world
5
4> first:greet_and_add_two(3,4).   // , !
** exception error: undefined function useless:greet_and_add_two/2
 
 
 
前にerlc flag fileについて言及しました.具体的にはどのようなflagが使われているのか、以下に示します.
 
-debug_info
    Erlang tools such as debuggers, code coverage and static analysis tools will use the debug information of a module in order to do their work.
-{outdir,Dir}
    By default, the Erlang compiler will create the 'beam' files in the current directory. This will let you choose where to put the compiled file.
-export_all
    Will ignore the -export module attribute and will instead export all functions defined. This is mainly useful when testing and developing new code, but should not be used in production.
-{d,Macro} or {d,Macro,Value}
    Defines a macro to be used in the module, where Macro is an atom. This is more frequently used when dealing when unit-testing, ensuring that a module will only have its testing functions created and exported when they are explicitly wanted. By default, Value is 'true' if it's not defined as the third element of the tuple. 
 
 
具体的には以下のように使います.
 
1> c(first,[debug_info,export_all]).
2>compile:file(useless, [debug_info, export_all]).
 
 
5.moduleの詳細について
 
ソースコードにコンパイルした後、moduleのプロパティ情報を含むmoduleの情報を表示する方法を知っています.では、次のように命令することができます.
 
3> first:module_info().
[{exports,[{add,2},
           {hello,0},
           {greet_and_add_two,1},
           {module_info,0},
           {module_info,1}]},
 {imports,[]},
 {attributes,[{vsn,[170044895626526885037789809337737753350]}]},
 {compile,[{options,[debug_info,export_all]},
           {version,"4.6.4"},
           {time,{2011,8,10,7,9,24}},
           {source,"/opt/home/inter12/work/erl/useless.erl"}]}]
 
 
具体的には説明しません!分かりやすいはず!具体的な情報を知りたい場合は、次のコマンドを使用します.
 
4> useless:module_info(exports).
[{add,2},
 {hello,0},
 {greet_and_add_two,1},
 {module_info,0},
 {module_info,1}]
 
 
moduleに作者の情報を入れたい場合は、
-author("An Erlang Champ")
-vsnはerlangコンパイラが自動的に生成する識別の主な役割は、プログラムの実行時にホットデプロイを行うことです.具体的にどのようにするかはまだ理解する必要があります!
次はaythorを追加したコードです
 
-module(useless).
-export([add/2,hello/0,greet_and_add_two/1]).
-author('zhaoming.xuezm').

add(A,B) ->
 A + B.
 

%%show greeeting 
%%io format is the standard output text 
hello() ->
 io:format("hello world~n").


greet_and_add_two(X) ->
  hello(),
  add(X,2).
 
 
コンパイル後に実行
 
3> useless:module_info(attributes).
[{vsn,[170044895626526885037789809337737753350]},
 {author,['zhaoming.xuezm']}]
module  A   B , B     A  。 , !