Erlang – Web Programming ”; Previous Next In Erlang, the inets library is available to build web servers in Erlang. Let’s look at some of the functions available in Erlang for web programming. One can implement the HTTP server, also referred to as httpd to handle HTTP requests. The server implements numerous features, such as − Secure Sockets Layer (SSL) Erlang Scripting Interface (ESI) Common Gateway Interface (CGI) User Authentication (using Mnesia, Dets or plain text database) Common Logfile Format (with or without disk_log(3) support) URL Aliasing Action Mappings Directory Listings The first job is to start the web library via the command. inets:start() The next step is to implement the start function of the inets library so that the web server can be implemented. Following is an example of creating a web server process in Erlang. For example -module(helloworld). -export([start/0]). start() -> inets:start(), Pid = inets:start(httpd, [{port, 8081}, {server_name,”httpd_test”}, {server_root,”D://tmp”},{document_root,”D://tmp/htdocs”}, {bind_address, “localhost”}]), io:fwrite(“~p”,[Pid]). The following points need to be noted about the above program. The port number needs to be unique and not used by any other program. The httpd service would be started on this port no. The server_root and document_root are mandatory parameters. Output Following is the output of the above program. {ok,<0.42.0>} To implement a Hello world web server in Erlang, perform the following steps − Step 1 − Implement the following code − -module(helloworld). -export([start/0,service/3]). start() -> inets:start(httpd, [ {modules, [ mod_alias, mod_auth, mod_esi, mod_actions, mod_cgi, mod_dir, mod_get, mod_head, mod_log, mod_disk_log ]}, {port,8081}, {server_name,”helloworld”}, {server_root,”D://tmp”}, {document_root,”D://tmp/htdocs”}, {erl_script_alias, {“/erl”, [helloworld]}}, {error_log, “error.log”}, {security_log, “security.log”}, {transfer_log, “transfer.log”}, {mime_types,[ {“html”,”text/html”}, {“css”,”text/css”}, {“js”,”application/x-javascript”} ]} ]). service(SessionID, _Env, _Input) -> mod_esi:deliver(SessionID, [ “Content-Type: text/htmlrnrn”, “<html><body>Hello, World!</body></html>” ]). Step 2 − Run the code as follows. Compile the above file and then run the following commands in erl. c(helloworld). You will get the following output. {ok,helloworld} The next command is − inets:start(). You will get the following output. ok The next command is − helloworld:start(). You will get the following output. {ok,<0.50.0>} Step 3 − You can now access the url – http://localhost:8081/erl/hello_world:service. Print Page Previous Next Advertisements ”;
Category: erlang
Erlang – Operators
Erlang – Operators ”; Previous Next An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Erlang has the following type of operators − Arithmetic operators Relational operators Logical operators Bitwise operators Arithmetic Operators Erlang language supports the normal Arithmetic operators as any the language. Following are the Arithmetic operators available in Erlang. Show Examples Operator Description Example + Addition of two operands 1 + 2 will give 3 − Subtracts second operand from the first 1 – 2 will give -1 * Multiplication of both operands 2 * 2 will give 4 / Division of numerator by denominator 2 / 2 will give 1 rem Remainder of dividing the first number by the second 3 rem 2 will give 1 div The div component will perform the division and return the integer component. 3 div 2 will give 1 Relational Operators The Relational Operators allow the comparison of objects. Following are the relational operators available in Erlang. Show Examples Operator Description Example == Tests the equality between two objects 2 = 2 will give true /= Tests the difference between two objects 3 /= 2 will give true < Checks to see if the left object is less than the right operand. 2 < 3 will give true =< Checks to see if the left object is less than or equal to the right operand. 2 =<3 will give true > Checks to see if the left object is greater than the right operand. 3 > 2 will give true >= Checks to see if the left object is greater than or equal to the right operand. 3 >= 2 will give true Logical Operators These Logical Operators are used to evaluate Boolean expressions. Following are the logical operators available in Erlang. Show Examples Operator Description Example or This is the logical “or” operator true or true will give true and This is the logical “and” operator True and false will give false not This is the logical “not” operator not false will give true xor This is the logical exclusive “xor” operator True xor false will give true Bitwise Operators Erlang provides four bitwise operators. Following are the bitwise operators available in Erlang. Show Examples Sr.No. Operator & Description 1 band This is the bitwise “and” operator 2 bor This is the bitwise “or” operator 3 bxor This is the bitwise “xor” or Exclusive or operator 4 bnot This is the bitwise negation operator Following is the truth table showcasing these operators − p q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1 Operator Precedence The following table shows the Operator Precedence for the Erlang operators in order of descending priority together with their associativity. Operator precedence and associativity are used to determine the evaluation order in un-parenthesized expressions. Operators Associativity : # bnot,not /,*,div,rem,band,and Left associative +,-,bor,bxor,or,xor Left associative ==,/=,=<,<,>=,> Print Page Previous Next Advertisements ”;
Erlang – Home
Erlang Tutorial PDF Version Quick Guide Resources Job Search Discussion Erlang is a general purpose or you might say a functional programming language and runtime environment. It was built in such a way that it had inherent support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems. But it has now slowly made its foray into diverse sectors like ecommerce, computer telephony and banking sectors as well. Audience This tutorial has been prepared for professionals aspiring to make a career in the field of telecom, banking, instant messaging, e-commerce and computer telephony as well. This tutorial will give you enough understanding on this programming language and also help you in building scalable soft real time systems that will have requirements on higher availability. Prerequisites Before proceeding with this tutorial, you must have some basic knowledge on programming in the following languages such as C or C++, Java, Python, Ruby. Furthermore, it might also be helpful, to have some working knowledge on functional programming languages like Clojure, Haskell, Scala or OCaml for advanced programming on Erlang. Print Page Previous Next Advertisements ”;
Erlang – Strings
Erlang – Strings ”; Previous Next A String literal is constructed in Erlang by enclosing the string text in quotations. Strings in Erlang need to be constructed using the double quotation marks such as “Hello World”. Following is an example of the usage of strings in Erlang − Example Live Demo -module(helloworld). -export([start/0]). start() -> Str1 = “This is a string”, io:fwrite(“~p~n”,[Str1]). The above example creates a string variable called Str1. The string “This is a string” is assigned to the variable and displayed accordingly. The output of the above program will be − Output “This is a string” Next, we will discuss the various operations available for Strings. Note that for string operations, you need to include the string library as well. Sr.No String Methods & Description 1 len The method returns the length of a particular string. 2 equal The method returns a Boolean value on whether one string is equal to another. 3 concat The method concats 2 strings and returns the concatenated string. 4 chr The method returns the index position of a character in a string. 5 str The method returns the index position of a sub string in a string. 6 substr The method returns the sub string from the original string based on the starting position and number of characters from the starting position. 7 left The method returns the sub string from the original string based on the starting position and number of characters from the starting position. left with trailing character The method returns the sub string from the left of the string based on the number of characters. But with the option to include a trailing character if the number is greater than the length of the string. Syntax left(str1,number,$character) Parameters str1 − This is the string from which the sub string needs to be extracted. Number − This is the number of characters which need to be present in the substring. $Character − The character to include as the trailing character. Return Value Returns the sub string from the original string based on the left hand side of the string and the number. For example Live Demo -module(helloworld). -import(string,[left/3]). -export([start/0]). start() -> Str1 = “hello”, Str2 = left(Str1,10,$.), io:fwrite(“~p~n”,[Str2]). Output When we run the above program, we will get the following result. “hello…..” right The method returns the sub string from the right of the string based on the number of characters. Syntax right(str1,number) Parameters str1 − This is the string from which the sub string needs to be extracted. Number − This is the number of characters which need to be present in the substring. Return Value Returns the substring from the original string based on the right hand side of the string and the number. For example Live Demo -module(helloworld). -import(string,[right/2]). -export([start/0]). start() -> Str1 = “hello World”, Str2 = right(Str1,2), io:fwrite(“~p~n”,[Str2]). Output When we run the above program, we will get the following result. “ld” right with trailing character The method returns the substring from the right of the string based on the number of characters. But with the option to include a trailing character if the number is greater than the length of the string. Syntax right(str1,number,$character) Parameters str1 − This is the string from which the sub string needs to be extracted. Number − This is the number of characters which need to be present in the substring. $Character − The character to include as the trailing character. Return Value Returns the sub string from the original string based on the right hand side of the string and the number. For example Live Demo -module(helloworld). -import(string,[right/3]). -export([start/0]). start() -> Str1 = “hello”, Str2 = right(Str1,10,$.), io:fwrite(“~p~n”,[Str2]). Output When we run the above program, we will get the following result. “…..hello” to_lower The method returns the string in lower case. Syntax to_lower(str1) Parameters str1 − This is the string from which needs to be converted to lower case. Return Value Returns the string in lower case. For example Live Demo -module(helloworld). -import(string,[to_lower/1]). -export([start/0]). start() -> Str1 = “HELLO WORLD”, Str2 = to_lower(Str1), io:fwrite(“~p~n”,[Str2]). Output When we run the above program, we will get the following result. “hello world” to_upper The method returns the string in upper case. Syntax to_upper(str1) Parameters str1 − This is the string from which needs to be converted to upper case. Return Value − Returns the string in upper case. For example Live Demo -module(helloworld). -import(string,[to_upper/1]). -export([start/0]). start() -> Str1 = “hello world”, Str2 = to_upper(Str1), io:fwrite(“~p~n”,[Str2]). Output When we run the above program, we will get the following result. “HELLO WORLD” sub_string Returns a substring of String, starting at the position Start to the end of the string, or to and including the Stop position. Syntax sub_string(str1,start,stop) Parameters str1 − This is the string from which the sub string needs to be returned. start − This is the start position of the sub string stop − This is the stop position of the sub string Return Value Returns a substring of String, starting at the position Start to the end of the string, or to and including the Stop position. For example Live Demo -module(helloworld). -import(string,[sub_string/3]). -export([start/0]). start() -> Str1 = “hello world”, Str2 = sub_string(Str1,1,5), io:fwrite(“~p~n”,[Str2]). Output When we run the above program, we will get the following result. “hello” Print Page Previous Next Advertisements ”;
Erlang – Emails
Erlang – Email ”; Previous Next To send an email using Erlang, you need to use a package available from github for the same. The github link is − https://github.com/Vagabond/gen_smtp This link contains an smtp utility which can be used for sending email from an Erlang application. Follow the steps to have the ability to send an email from Erlang Step 1 − Download the erl files from the github site. The files should be downloaded to the directory where your helloworld.erl application resides. Step 2 − Compile all the smtp related files shown in the following list using the erlc command. The following files need to be compiled. smtp_util gen_smtp_client gen_smtp_server gen_smtp_server_session binstr gen_smtp_application socket Step 3 − The following code can be written to send an email using smtp. Example -module(helloworld). -export([start/0]). start() -> gen_smtp_client:send({“[email protected]”, [“[email protected]”], “Subject: testing”}, [{relay, “smtp.gmail.com”}, {ssl, true}, {username, “[email protected]”}, {password, “senderpassword”}]). The following things need to be noted about the above program The above smtp function is being used along with the smtp server available from google. Since we wanted to send using a secure smtp, we specify the ssl parameter as true. You need to specify the relay as smtp.gmail.com. You need to mention a user name and password which has access to send the email. Once you configure all the above settings and execute the program, the receiver will successfully receive an email. Print Page Previous Next Advertisements ”;
Erlang – Loops
Erlang – Loops ”; Previous Next Erlang is a functional programming language and what needs to be remembered about all functional programming languages is that they don’t offer any constructs for loops. Instead, functional programming depends on a concept called recursion. while Statement Implementation Since there is no direct while statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a while statement implementation. We will try to follow the same implementation of the while loop as is followed in other programming languages. Following is the general flow which will be followed. Let’s look at an example of how we can use recursion to implement the while loop in Erlang. Example Live Demo -module(helloworld). -export([while/1,while/2, start/0]). while(L) -> while(L,0). while([], Acc) -> Acc; while([_|T], Acc) -> io:fwrite(“~w~n”,[Acc]), while(T,Acc+1). start() -> X = [1,2,3,4], while(X). The following key points need to be noted about the above program − Define a recursive function called while which would simulate the implementation of our while loop. Input a list of values defined in the variable X to our while function as an example. The while function takes each list value and stores the intermediate value in the variable ‘Acc’. The while loop is then called recursively for each value in the list. The output of the above code will be − Output 0 1 2 3 for Statement Since there is no direct for statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a for statement implementation. We will try to follow the same implementation of the for loop as is followed in other programming languages. Following is the general flow which should be adhered to. Let’s look at an example of how we can use recursion to implement the for loop in Erlang. Example Live Demo -module(helloworld). -export([for/2,start/0]). for(0,_) -> []; for(N,Term) when N > 0 -> io:fwrite(“Hello~n”), [Term|for(N-1,Term)]. start() -> for(5,1). The following key points need to be noted about the above program − We are defining a recursive function which would simulate the implementation of our for loop. We are using a guard within the ‘for’ function to ensure that the value of N or the limit is a positive value. We recursively call the for function, by reducing the value of N at each recursion. The output of the above code will be − Output Hello Hello Hello Hello Hello Print Page Previous Next Advertisements ”;
Erlang – Environment
Erlang – Environment ”; Previous Next Now before you can start working on Erlang, you need to ensure that you have a fully functional version of Erlang running on your system. This section will look into the installation of Erlang and its subsequent configuration on a windows machine to get started with Erlang. Ensure that the following system requirements are met before proceeding with the installation. System Requirements Memory 2 GB RAM (recommended) Disk Space No minimum requirement. Preferably to have enough storage to store the applications which will be created using Erlang. Operating System Version Erlang can be installed on Windows, Ubuntu/Debian, Mac OS X. Downloading Erlang To download Erlang, one must go to the following url − www.erlang.org/downloads. This page has a variety of downloads and also the steps required to download and install the language on Linux and Mac platforms. Click on the ‘OTP 18.3 Windows 32-bit Binary File’ to begin the download of the Erlang Windows Installation file. Erlang Installation The following steps detail how Erlang can be installed on Windows − Step 1 − Launch the Installer downloaded in the earlier section. After the installer starts, click Run. Step 2 − Click Next on the following screen to accept the default components, which will be installed. Step 3 − Accept the default installation path and click Next. Step 4 − Accept the default Start Menu item, which will be created and click Next. Step 5 − After the installation is complete, click Close to complete the installation. Erlang Configuration After the installation is complete the following configuration needs to be carried out to ensure that Erlang starts working on the system. OS Output Windows Append the String; C:Program Files(x86)erl7.2.1bin OR C:Program Fileserl7.2.1bin to the end of the system variable PATH. If you now open the command prompt and type erl, you should be able to get the erl command prompt. Congratulations, you now have erl successfully configured on your laptop. Installation of Plugin-ins on Popular IDE’s Erlang as a programming language is also available in popular IDE’s such as Eclipse and IntelliJ. Let’s look at how we can get the required plugin’s in these IDE’s so that you have more choices in working with Erlang. Installation in Eclipse Step 1 − Open Eclipse and click the Menu item, Help → Install New Software. Step 2 − Enter the Work with link as https://download.erlide.org/update Then click Add. Step 3 − You will then be prompted to enter a Name for the plugin, enter the name as Erlide. Click Ok. Step 4 − Eclipse will then scan the link provided and get the required plugins. Check the plugins and click Next. Step 5 − In the next dialog box, Eclipse will show all the components which will be installed. Click Next. Step 6 − In the next dialog box, Eclipse will just ask to review the components being installed. Click Next. Step 7 − In the next dialog box, you just need to accept the license agreement. Finally, click the Finish button. The installation will then begin, and once completed, it will prompt you to restart Eclipse. Once Eclipse is restarted, when you create a project, you will be able to see Erlang as an option as well. Installation in IntelliJ Please follow the subsequent steps to install IntelliJ in your computer. Step 1 − Open IntelliJ and click Configure → Plugins. Step 2 − Type Erlang in the search box. You will get Erlang plugin on the right hand side of the screen. Click the Install button. Step 3 − After the Erlang plugin is installed, you will be prompted to restart the IDE. When you restart the IDE and try to create a new project, you will see the option to create an Erlang project. Print Page Previous Next Advertisements ”;
Erlang – Data Types
Erlang – Data Types ”; Previous Next In any programming language, you need to use several variables to store various types of information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory to store the value associated with that variable. You may like to store information of various data types like string, character, wide character, integer, floating point, Boolean, etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Built-in Data Types Erlang offers a wide variety of built-in data types. Following is a list of data types which are defined in Erlang − Number − In Erlang, there are 2 types of numeric literals which are integers and floats. Atom − An atom is a literal, a constant with name. An atom is to be enclosed in single quotes (”) if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @. Boolean − Boolean data types in Erlang are the two reserved atoms: true and false. Bit String − A bit string is used to store an area of un-typed memory. Tuple − A tuple is a compound data type with a fixed number of terms. Each Term in the tuple is called as an element. The number of elements is said to be the size of the tuple. Map − A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map. List − A list is a compound data type with a variable number of terms. Each term in the list is called an element. The number of elements is said to be the length of the list. Note − You will be surprised to see that you cannot see the String type anywhere in the list above. That’s because there is no string data type exclusively defined in Erlang. But we will see how we can work with strings in a subsequent chapter. Following are the examples of how each data type can be used. Again each data type will be discussed in detail in the ensuing chapters. This is just to get you acquainted with a brief description of the above-mentioned data types. Number An example of how the number data type can be used is shown in the following program. This program shows the addition of 2 Integers. Example Live Demo -module(helloworld). -export([start/0]). start() -> io:fwrite(“~w”,[1+1]). The output of the above program will be − Output 2 Atom Atoms should begin with a lowercase letter and can contain lowercase and uppercase characters, digits, underscore (_) and the “at” sign (@). We can also enclose an atom in single quotes. An example of how the atom data type can be used is shown in the following program. In this program, we are creating an atom which is called atom1. Example Live Demo -module(helloworld). -export([start/0]). start() -> io:fwrite(atom1). The output of the above program will be − Output atom1 Boolean An example of how the Boolean data type can be used is shown in the following program. This example does a comparison between 2 integers and prints the resultant Boolean to the console. Example Live Demo -module(helloworld). -export([start/0]). start() -> io:fwrite(2 =< 3). The output of the above program will be − Output true Bit String An example of how the Bit String data type can be used is shown in the following program. This program defines a Bit String consisting of 2 bits. The binary_to_list is an inbuilt function defined in Erlang which can be used to convert a Bit String to a list. Example Live Demo -module(helloworld). -export([start/0]). start() -> Bin1 = <<10,20>>, X = binary_to_list(Bin1), io:fwrite(“~w”,[X]). The output of the above program will be − Output [10,20] Tuple An example of how the Tuple data type can be used is shown in the following program. Here we are defining a Tuple P which has 3 terms. The tuple_size is an inbuilt function defined in Erlang, which can be used to determine the size of the tuple. Example Live Demo -module(helloworld). -export([start/0]). start() -> P = {john,24,{june,25}} , io:fwrite(“~w”,[tuple_size(P)]). The output of the above program will be − Output 3 Map An example of how the Map data type can be used is shown in the following program. Here we are defining a Map M1 which has 2 mappings. The map_size is an inbuilt function defined in Erlang, which can be used to determine the size of the map. Example Live Demo -module(helloworld). -export([start/0]). start() -> M1 = #{name=>john,age=>25}, io:fwrite(“~w”,[map_size(M1)]). The output of the above program will be − Output 2 List An example of how the List data type can be used is shown in the following program. Here we are defining a List L which has 3 items. The length is an inbuilt function defined in Erlang, which can be used to determine the size of the list. Example Live Demo -module(helloworld). -export([start/0]). start() -> L = [10,20,30] , io:fwrite(“~w”,[length(L)]). The output of the above program will be − Output 3 Print Page Previous Next Advertisements ”;
Erlang – Basic Syntax
Erlang – Basic Syntax ”; Previous Next In order to understand the basic syntax of Erlang, let’s first look at a simple Hello World program. Example Live Demo % hello world program -module(helloworld). -export([start/0]). start() -> io:fwrite(“Hello, world!n”). The following things need to be noted about the above program − The % sign is used to add comments to the program. The module statement is like adding a namespace as in any programming language. So over here, we are mentioning that this code will part of a module called helloworld. The export function is used so that any function defined within the program can be used. We are defining a function called start and in order to use the start function, we have to use the export statement. The /0 means that our function ‘start’ accepts 0 parameters. We finally define our start function. Here we use another module called io which has all the required Input Output functions in Erlang. We used the fwrite function to output “Hello World” to the console. The output of the above program will be − Output Hello, world! General Form of a Statement In Erlang, you have seen that there are different symbols used in Erlang language. Let’s go through what we have seen from a simple Hello World program − The hyphen symbol (–) is generally used along with the module, import and export statement. The hyphen symbol is used to give meaning to each statement accordingly. So examples from the Hello world program are shown in the following program − -module(helloworld). -export([start/0]). Each statement is delimited with the dot (.) symbol. Each statement in Erlang needs to end with this delimiter. An example from the Hello world program is as shown in the following program − io:fwrite(“Hello, world!n”). The slash (/) symbol is used along with the function to define the number of parameters which is accepted by the function. -export([start/0]). Modules In Erlang, all the code is divided into modules. A module consists of a sequence of attributes and function declarations. It is just like a concept of a namespace in other programming languages which is used to logically separate different units of code. Defining a module A module is defined with the module identifier. The general syntax and example is as follows. Syntax -module(ModuleName) The ModuleName needs to be same as the file name minus the extension .erl. Otherwise code loading will not work as intended. Example -module(helloworld) These Modules will be covered in detail in the ensuing chapters, this was just to get you at a basic understanding of how a module should be defined. Import Statement in Erlang In Erlang, if one wants to use the functionality of an existing Erlang module, one can use the import statement. The general form of the import statement is depicted in the following program − Example -import (modulename, [functionname/parameter]). Where, Modulename − This is the name of the module which needs to be imported. functionname/parameter − The function in the module which needs to be imported. Let’s change the way we write our hello world program to use an import statement. The example would be as shown in the following program. Example % hello world program -module(helloworld). -import(io,[fwrite/1]). -export([start/0]). start() -> fwrite(“Hello, world!n”). In the above code, we are using the import keyword to import the library ‘io’ and specifically the fwrite function. So now whenever we invoke the fwrite function, we don’t have to mention the io module name everywhere. Keywords in Erlang A Keyword is a reserved word in Erlang which should not be used for any different purposes other than the purpose which it has been intended for. Following are the list of keywords in Erlang. after and andalso band begin bnot bor bsl bsr bxor case catch cond div end fun if let not of or orelse receive rem try when xor Comments in Erlang Comments are used to document your code. Single line comments are identified by using the % symbol at any position in the line. Following is an example for the same − Example % hello world program -module(helloworld). % import function used to import the io module -import(io,[fwrite/1]). % export function used to ensure the start function can be accessed. -export([start/0]). start() -> fwrite(“Hello, world!n”). Print Page Previous Next Advertisements ”;
Erlang – Modules
Erlang – Modules ”; Previous Next Modules are a bunch of functions regrouped in a single file, under a single name. Additionally, all functions in Erlang must be defined in modules. Most of the basic functionality like arithmetic, logic and Boolean operators are already available because the default modules are loaded when a program is run. Every other function defined in a module you will ever use needs to be called with the form Module:Function (Arguments). Defining a Module With a module, you can declare two kinds of things: functions and attributes. Attributes are metadata describing the module itself such as its name, the functions that should be visible to the outside world, the author of the code, and so on. This kind of metadata is useful because it gives hints to the compiler on how it should do its job, and also because it lets people retrieve useful information from compiled code without having to consult the source. The syntax of a function declaration is as follows − Syntax -module(modulename) Where, modulename is the name of the module. This has to be the first line of the code in the module. The following program shows an example of a module called helloworld. Example Live Demo -module(helloworld). -export([start/0]). start() -> io:fwrite(“Hello World”). The output of the above program is − Output Hello World Module Attributes A module attribute defines a certain property of a module. A module attribute consists of a tag and a value. The general syntax of an attribute is − Syntax -Tag(Value) An example of how the attribute can be used is shown in the following program − Example Live Demo -module(helloworld). -author(“TutorialPoint”). -version(“1.0”). -export([start/0]). start() -> io:fwrite(“Hello World”). The above program defines 2 custom attributes called author and version which contains the program author and program version number respectively. The output of the above program is − Output Hello World Pre-built Attributes Erlang has some pre-built attributes which can be attached to modules. Let’s take a look at them. Export The exports attribute will take a list of functions and arity to export for consumption by other modules. It will define the module interface. We have already seen this in all of our previous examples. Syntax export([FunctionName1/FunctionArity1,.,FunctionNameN/FunctionArityN]) Where, FunctionName − This is the name of the function in the program. FunctionArity − This is the number of parameters associated with the function. Example Live Demo -module(helloworld). -author(“TutorialPoint”). -version(“1.0”). -export([start/0]). start() -> io:fwrite(“Hello World”). The output of the above program will be − Output Hello World Import The import attribute is used to import functions from another module to use it as local. Syntax -import (modulename , [functionname/parameter]). Where, Modulename − This is the name of the module which needs to be imported. functionname/parameter − the function in the module which needs to be imported. Example Live Demo -module(helloworld). -import(io,[fwrite/1]). -export([start/0]). start() -> fwrite(“Hello, world!n”). In the above code, we are using the import keyword to import the library ‘io’ and specifically the fwrite function. So, now whenever we invoke the fwrite function, we don’t have to mention the io module name everywhere. The output of the above program will be − Output Hello, world! Print Page Previous Next Advertisements ”;