{"id":399,"date":"2011-03-14T22:13:47","date_gmt":"2011-03-14T21:13:47","guid":{"rendered":"http:\/\/trigonakis.com\/blog\/?p=399"},"modified":"2011-03-14T22:43:30","modified_gmt":"2011-03-14T21:43:30","slug":"introduction-to-erlang-modules-compilation","status":"publish","type":"post","link":"http:\/\/trigonakis.com\/blog\/2011\/03\/14\/introduction-to-erlang-modules-compilation\/","title":{"rendered":"Introduction to Erlang : Modules &#038; Compilation"},"content":{"rendered":"<div class=\"seriesmeta\">This entry is part 6 of 16 in the series <a href=\"http:\/\/trigonakis.com\/blog\/series\/introduction-to-erlang\/\" class=\"series-57\" title=\"Introduction to Erlang\">Introduction to Erlang<\/a><\/div><h3>Modules<\/h3>\n<p>A <i>module<\/i> is a container for functions; it provided the contained functions with a common namespace. Modules are used to organize functions in Erlang. Usually, a program in Erlang spans over more than one modules. You can imagine a module as a package in Java, or a header file in C.<\/p>\n<h3>Calling a Function<\/h3>\n<p>The calling convention in Erlang is <code>module:function(argument1, argument2, ...)<\/code>. For example:<\/p>\n<pre lang=\"erlang\">\r\n1> lists:max([1,3,2]).\r\n3\r\n<\/pre>\n<h3>Defining Modules<\/h3>\n<p>Lets say we want to create a module that will contain our own implementation of list functions and name it <code>mlists<\/code>.<br \/>\n<!--more--><\/p>\n<p>First, we create a file named <code>mlists.erl<\/code> that will contain the module. <strong>The container file and the module names have to be the same<\/strong>.<\/p>\n<h4>Calling a Module Function within the Module<\/h4>\n<p>A function defined within the module file can be called either as <code>module_name:function(arguments)<\/code>, or <code>function(arguments)<\/code>, so the module name can be skipped.<\/p>\n<h4>Module Directives<\/h4>\n<p>Then we need to define the module&#8217;s <i>attributes<\/i>. An <i>attribute<\/i> is information that we provide to the Erlang compiler. It is placed as a directive (usually) in the top of the file and has the &#8220;<code>-attribute_name(attribue_value(s)).<\/code>&#8221; format. The one attribute that we have to define is the one providing the module name. <\/p>\n<h5>-module(&#8230;).<\/h5>\n<p>Defines the name of the module. For our example, in the top of <code>mlists.erl<\/code> place:<\/p>\n<pre lang=\"erlang\">\r\n-module(mlists).\r\n<\/pre>\n<p>I repeat: the module name should be the same as the filename containing the module.<\/p>\n<h5>-export([&#8230;])<\/h5>\n<p>More attributes are available to be passed to the compiler. A &#8220;necessary&#8221; one is the:<\/p>\n<pre lang=\"erlang\">\r\n-export([function1\/arity1, function2\/arity2, ...]).\r\n<\/pre>\n<p>that is used to define which functions the module exports, where &#8220;exports&#8221; means that they will be available to be called outside the module. Think of it as an <code>Interface<\/code> in Java.<\/p>\n<p>All the functions that are not exported by the module are only visible within the file, similar with the <code>private<\/code> functions in Java and the <code>static<\/code> ones in C.<\/p>\n<h5>-compile([&#8230;])<\/h5>\n<p>This one can be used to pass compilation instructions to the compiler. A usefull one is<\/p>\n<pre lang=\"erlang\">\r\n-compile([export_all]).\r\n<\/pre>\n<p>which automatically exports all the module&#8217;s functions. It is very convenient while developing and debugging, but you should not use it on production code, since the exported functions is the interface of your module, so you do not want to provide functions that are intended only for use within the module.<\/p>\n<h5>-import(module_name, [function1\/arity1, function2\/arity2, &#8230;)<\/h5>\n<p>You can use this directive in order to import the selected exported functions of a module in the namespace of another one. That means that if you do so, you will be able to call the functions without the module prefix. Although in some cases it could be convenient, it is not recommended to use this directive, because it decreases the code&#8217;s readability.<\/p>\n<h5>Other attributes<\/h5>\n<p>You can even define your own attributes. For example:<\/p>\n<pre lang=\"erlang\">\r\n-author(\"V. Trigonakis\").\r\n-date({2011, 03, 11}).\r\n<\/pre>\n<h4>Example<\/h4>\n<pre lang=\"erlang\">\r\n-module(md).\r\n-export([same\/1, double\/1]).\r\n-author(\"Vasileios Trigonakis\").\r\n-date({2011, 03, 13}).\r\n\r\nsame(I) ->\r\n    I.\r\n\r\ndouble(N) ->\r\n    2 * N.\r\n\r\nnot_exported() ->\r\n    same(smthing),\r\n    double(123).\r\n<\/pre>\n<p>As you can see, this module has 3 functions and exports the 2 of them (<code>same\/1, double\/1<\/code>).<\/p>\n<h3>Compiling Modules<\/h3>\n<h4>Emulator<\/h4>\n<p>Start an Erlang emulator on the folder that contains your source files. In order to compile a <code>.erl<\/code> file you use the <code>c(module_name)<\/code> Bult-in Function (<strong>BIF<\/strong>):<\/p>\n<pre lang=\"erlang\">\r\n1> c(md).\r\n.\/md.erl:12: Warning: function not_exported\/0 is unused\r\n{ok,md}\r\n<\/pre>\n<p>If no error occurs, the compiler generates the compiled <code>.beam<\/code> file. In most of the cases, an Erlang warning indicates that something is not proper. For example, the warning for the md module can help us find that the <code>not_exported\/0<\/code> function is neither exported, nor used within the file.<\/p>\n<pre lang=\"erlang\">\r\n2> ls().\r\nmd.beam     md.erl      md.erl~     \r\nok\r\n<\/pre>\n<p>You can also provide a path to the file to be compiled, or even move around folders using the <code>cd(\"path\")<\/code> function.<\/p>\n<pre lang=\"erlang\">\r\n3> cd(\"..\").\r\n~\/Documents\/playing\/erlang\r\nok\r\n4> c(\"post_modules\/md\").\r\npost_modules\/md.erl:12: Warning: function not_exported\/0 is unused\r\n{ok,md}\r\n<\/pre>\n<p>Three other usefull utilities (functions) are the <code>ls()<\/code>, <code>pwd()<\/code>, and <code>os:cmd(\"command\")<\/code> functions.<\/p>\n<pre lang=\"erlang\">\r\n1> pwd().\r\n\/var\r\nok\r\n2> ls().\r\nbackups     cache       crash       games       lib         local       \r\nlock        log         mail        opt         run         spool       \r\ntmp         \r\nok\r\n3> os:cmd(\"echo echoing...\").\r\n\"echoing...\\n\"\r\n<\/pre>\n<p>The last one executes the given command on a shell and returns the result.<\/p>\n<h4>Erlang Compiler (erlc)<\/h4>\n<p>You can also use the <strong>Erl<\/strong>ang <strong>C<\/strong>ompiler to compile a module to a beam:<\/p>\n<pre lang=\"bash\">\r\n$ ls\r\nmd.erl  md.erl~\r\n$ erlc md.erl\r\n.\/md.erl:12: Warning: function not_exported\/0 is unused\r\n$ ls\r\nmd.beam  md.erl  md.erl~\r\n<\/pre>\n<h4>Loading a Module<\/h4>\n<p>You can load an already compiled module<\/p>\n<pre lang=\"erlang\">\r\n1> l(md).     \r\n{module,md}\r\n<\/pre>\n<h4>Getting the Module&#8217;s Attributes<\/h4>\n<p>Every compiled module exports the <code>module_info\/0<\/code> and <code>module_info\/1<\/code> functions that can be used to fetch the attributes of a module.<\/p>\n<pre lang=\"erlang\">\r\n1> erlang:module_loaded(md).\r\nfalse\r\n2> l(md).\r\n{module,md}\r\n3> erlang:module_loaded(md).\r\ntrue\r\n4> md:module_info().\r\n[{exports,[{same,1},\r\n           {double,1},\r\n           {module_info,0},\r\n           {module_info,1}]},\r\n {imports,[]},\r\n {attributes,[{vsn,[205824271517095806442935620583334286333]},\r\n              {author,\"Vasileios Trigonakis\"},\r\n              {date,[{2011,3,13}]}]},\r\n {compile,[{options,[{cwd,\"~\/Documents\/playing\/erlang\/post_modules\"},\r\n                     {outdir,\"~\/Documents\/playing\/erlang\/post_modules\"}]},\r\n           {version,\"4.6.4\"},\r\n           {time,{2011,3,14,7,54,12}},\r\n           {source,\"~\/Documents\/playing\/erlang\/post_modules\/md.erl\"}]}]\r\n5> md:module_info(attributes).  \r\n[{vsn,[205824271517095806442935620583334286333]},\r\n {author,\"Vasileios Trigonakis\"},\r\n {date,[{2011,3,13}]}]\r\n<\/pre>\n<h3>Next<\/h3>\n<p>I promise, from now on the posts will be far more interesting. Next one, or two posts will be about <storng>defining functions in Erlang. You can imagine how important functions are for a functional programming language.<\/storng><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This entry is part 6 of 16 in the series <a href=\"http:\/\/trigonakis.com\/blog\/series\/introduction-to-erlang\/\" class=\"series-57\" title=\"Introduction to Erlang\">Introduction to Erlang<\/a><\/div><p>Modules A module is a container for functions; it provided the contained functions with a common namespace. Modules are used to organize functions in Erlang. Usually, a program in Erlang spans over more than one modules. You can imagine a module as a package in Java, or a header file in C. Calling a Function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[40,51,28],"tags":[56,26,55,42],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ouW6-6r","_links":{"self":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/399"}],"collection":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":4,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":414,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/posts\/399\/revisions\/414"}],"wp:attachment":[{"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/trigonakis.com\/blog\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}