Modules & Compilationby Pigbrain

Modules

  • 모듈은 함수의 집합체이다
  • 대부분의 얼랭 프로그램은 하나 이상의 모듈로 구성된다
  • 모듈은 Java의 패키지, C의 헤더파일과 비슷한 의미이다

Calling a Function

  • 얼랭에서 함수 호출은 다음과 같은 형태를 갖는다

    1. %% module_name:function(argument1, argument2, ...)
    2. > lists:max([1, 2, 3]).

Defining Modules (mlists)

  • mlists.erl 파일을 생성한다
    • 파일 이름과 모듈 이름은 동일해야 한다
  • 모듈 속성(Attribute)를 정의한다
    • 속성은 얼랭 컴파일러에게 전달하고자하는 정보이다
    • 속성은 파일 윗 부분에 -attribute(attribute_value(s))와 같이 선언한다
    • -module(name)이라는 속성은 반드시 정의해야 한다
  • -module(mlists). 를 정의한다
  • -export([function1/arity1, function2/arity2, …]).
    • export는 모듈 외부에서 사용하고 하는 함수들을 정의하는 속성이다
    • 모듈의 모든 함수가 export 될 필요는 없다. 외부에서 사용하고자 하는것만 export한다
    • Java의 Interface와 동일하다
  • -compile([…]).
    • 컴파일러에게 전달하고자하느 명령어를 적으면 된다
    • 유용하게 사용하는 것으로 export_all이 있다
      • 모듈의 모든 함수를 export 시킨다

        1. -compile([export_all]).
  • =import(module_name, [function1/arity1, function2/arity2]).
    • 다른 모듈에 있는 함수를 module_name:function(…)이 아닌 function(…) 형태로 사용하기 위해 선언한다
    • import를 하면 사용하기에는 편리하지만 가독성이 떨어져 추천하지 않는다
  • 그 외 속성(Attribute)
    • 사용자 정의 속성을 정의할 수 있다

      1. -author("V. Trigonakis").
      2. -date({2011, 03, 11}).


Example

  1. -module(md).
  2. -export([same/1, double/1]).
  3. -author("V. Trigonakis").
  4. -date({2011, 03, 11}).
  5.  
  6. same(l) ->
  7. l.
  8. double(N) ->
  9. 2 * N.
  10.  
  11. not_exported() ->
  12. same(smthing),
  13. double(123).
  • 위 모듈은 3개의 함수를 가지고 2개의 함수만(same/1, double/1) export 되었다


Compiling Modules

  • Emulator
    • 얼랭 에뮬레이터에서 코드를 컴파일 하기 위해서는 BIF 중 c(module_name)를 사용한다

      1. 1> c(md).
      2. {ok, md}
    • 컴파일 과정에서 오류가 발생하지 않았다면 .beam 파일이 생성되었을 것이다

  • Erlang Compiler (erlc)
    • c(module_name) 외에 erlc를 통하여 beam을 생성하는 방법도 있다
      • Erlang Compiler

        1. $ erlc md.erl
        2. $ ls
        3. md.beam md.erl


Loading a Module

  • 컴파일된 모듈은 다음과 같은 형태로 로딩할 수 있다

    1. 1> l(md).
    2. {module, md}.


Getting the Module’s Attributes

  • 모든 컴파일된 모듈은 module_info/0, module_info/1 함수를 통하여 해당 모듈의 속성을 가져올 수 있다

    1. 1> erlang:module_loaded(md).
    2. false
    3. 2> l(md).
    4. {module,md}
    5. 3> erlang:module_loaded(md).
    6. true
    7. 4> md:module_info().
    8. [{exports,[{same,1},
    9. {double,1},
    10. {module_info,0},
    11. {module_info,1}]},
    12. {imports,[]},
    13. {attributes,[{vsn,[205824271517095806442935620583334286333]},
    14. {author,"Vasileios Trigonakis"},
    15. {date,[{2011,3,13}]}]},
    16. {compile,[{options,[{cwd,"~/Documents/playing/erlang/post_modules"},
    17. {outdir,"~/Documents/playing/erlang/post_modules"}]},
    18. {version,"4.6.4"},
    19. {time,{2011,3,14,7,54,12}},
    20. {source,"~/Documents/playing/erlang/post_modules/md.erl"}]}]
    21. 5> md:module_info(attributes).
    22. [{vsn,[205824271517095806442935620583334286333]},
    23. {author,"Vasileios Trigonakis"},
    24. {date,[{2011,3,13}]}]



원문

  • http://trigonakis.com/blog/2011/03/14/introduction-to-erlang-modules-compilation/
Published 02 January 2016