Using Erlang - Open Source Erlang Programmingby Francesco Cesarini, Simon Thompson
This appendix begins by helping you to get started with Erlang. We then recommend a number of tools to help you to develop Erlang-based systems more effectively. Finally, we tell you where you can find out more about Erlang, particularly from the many web-based resources for Erlang.
This excerpt is from Erlang Programming. You'll learn how to write complex concurrent programs in this language, regardless of your programming background or experience.
This section tells you how to get started with Erlang: first, how you install it, and then how to run Erlang programs. We conclude by showing you the various commands in the Erlang shell that help you to be a power user of the shell, using the history mechanism and the line editing commands.
The Erlang distribution is available from the Erlang website, http://erlang.org/download.html or one of the many mirror sites. You can also download Erlang using BitTorrent, as well as find it bundled in many of the major Linux distributions. The sources are provided for compilation on Unix operating systems, including Linux and Mac OS X; for Windows, use the binary installer. When building from source, follow the instructions that come with the distribution.
In Unix, Linux, and Mac OS X, you can run the Erlang shell from
the command line by typing the
erl command, setting whatever options you
To open an Erlang file in Windows, double-click the file icon; this will ensure that the system is opened in the correct directory. There are two variants of the system on Windows, available under “Open With” when you right-click an Erlang source file:
Opens the file in an Erlang shell from the Command Prompt program
Opens an Erlang shell in a window supporting copy and paste operations more accessibly than the standard Command Prompt program
To run Erlang with options set—for example, the
–smp option required to run wxErlang—you can run these commands from
the Run window, or by typing them as commands to a command prompt. This
option allows you to change into the appropriate directory before
issuing the command, as in the following:
C:\Documents and Settings\Administrator>
"c:\Program Files\erl5.7\bin\erl.exe" -smp miniblog.erlEshell V5.7 (abort with ^G) 1>
You can also change these features by right-clicking the file icon and selecting Properties. In the Shortcut tab, you can change your target to include your start options and change the “Start in directory” to point to where your source code is.
In the Erlang shell on Unix and in Werl on Windows, there is a standard set of editing operations on the commands typed:
- Up and down arrows
Fetch the previous and next command line; this may be part of a command, since commands can span multiple lines, in general
- Ctrl-P and Ctrl-N
Have the same effect as the up and down arrows
- Left and right arrows
Move the cursor one character to the left and right
- Ctrl-B and Ctrl-F
Have the same effect as the left and right arrows
Takes the cursor to the start of the line
Takes the cursor to the end of the line
Deletes the character under the cursor
As you’ve seen in the body of the text, there is a set of commands in the Erlang shell. The most commonly used commands include the following:
The history of commands and their results are remembered:
Will repeat the nth previous command; for example,
e(−1)is the previous command
The return value of the nth previous command; for example,
v(−1)+v(−2)will return the sum of the previous two values
If you want to get started writing programs in a new language, the last thing you want to have to do is to learn a new editor, too. Luckily, many common editors and IDEs provide support for writing programs. In this section, we’ll discuss these editors and IDEs, as well as some of the other tools we find useful beyond those we’ve already described. Some tools come as a part of the Erlang distribution, and there’s a comprehensive description of those tools in the documentation that accompanies the distribution.
Erlang programs are contained in text files, and so they can be created within any text editor. However, a number of editors support Erlang-aware operations, and, taking a leaf from the Java and C++ communities, a growing number of fully fledged IDEs support Erlang:
According to a recent survey of Erlang users, the principal development tool for the dedicated Erlang programmer is Erlang mode for Emacs, documented in the tools reference manual from the Erlang online documentation. This gives syntax coloring for Erlang, as well as context-sensitive formatting that will help you to lay out your program so that you and others can read it. The mode will also check that your module names and filenames match, as well as providing skeletons for common OTP behaviors.
Distel, or Distributed Emacs Lisp, takes the Emacs support for Erlang to another level, as it allows Emacs to interact with running Erlang nodes, as we described for other programming languages in Chapter 16, Interfacing Erlang with Other Programming Languages. Distel provides completion of names of functions and modules; runs Erlang code from within Emacs; offers some limited refactoring support; and features an interactive debugger. Distel is a live Google Code project.
There is also an Erlang plug-in package for Vim, which supports indentation and syntax highlighting, as well as folding and partial omni-completion. This is available from the Vim website, http://www.vim.org/scripts/script.php?script_id=1584.
Eclipse is the tool of choice for many Java and C++ programmers, and Erlang is now supported in Eclipse through Erlide, an Erlang plug-in for Eclipse, available on Sourceforge.net. Erlide implements syntax highlighting and indentation, but also offers evaluation of Erlang expressions within the IDE and automatic compilation on file save. Its structure also defines the scope for Erlang projects, and gives debugging support. Erlide also provides access to the refactorings in Wrangler (discussed shortly).
Other IDEs and editor support include ErlyBird, an Erlang IDE based on NetBeans, and UltraEdit, which some Windows developers use to highlight syntax in Erlang programs.
In addition to the tools we described earlier, other tools we find helpful include the following:
Although EUnit provides the framework for unit testing, Common Test gives a framework for complete Erlang-based systems. Common Test is part of the Erlang standard distribution.
Traditional testing allows you to check the behavior of a system under particular inputs; an alternative approach, embodied in QuickCheck, asks the tester to state properties of the system and then tests the properties for randomly generated input values. QuickCheck is also able to test properties of concurrent systems using finite state machines to exercise their behavior, and to shrink any data that fails to satisfy the property to minimal counterexamples. QuickCheck is a product of Quviq AB.
Beyond simple testing, static analysis can check for dead code as well as type anomalies. Dialyzer comes as part of the Erlang standard distribution.
Refactoring for Erlang is supported by the Wrangler tool, embedded in both Emacs and Erlide, and available from the University of Kent in the United Kingdom. RefactorErl also supports some refactorings, as do Distel and the
syntax_toolsmodules that come with the Erlang standard distribution.
Although testing tools can check the behavior of a system under only a selection of inputs, model checking enables the user to check all possible behaviors of the system under test. McErlang, developed at the Universidad Politécnica de Madrid, is a model checker for Erlang written in Erlang. Another approach to model checking translates Erlang into μCRL and then model-checks the results.
The best place to start when you want to learn more about Erlang is the Erlang home page, http://www.erlang.org. Here, you can find out about upcoming events, books, courses, and jobs, as well as access the system documentation.
The system documentation—which you can access at http://www.erlang.org/doc/ and download to your computer when you download Erlang—can be a bit overwhelming at first, but it contains a lot of useful information:
Each Erlang module in the distribution is documented at http://www.erlang.org/doc/: click the Modules link in the top-lefthand corner of the home page.
To get other information about a topic or potential function, you can use the index generated from the documentation, which is accessible from the top-lefthand corner of the home page.
The tabs down the left side of the main page give links to documentation regarding the main Erlang applications and tools. Particularly useful are:
The installation guide (under the Erlang/OTP tab)
Getting Started, a mini tutorial (under Erlang Programming)
The Erlang reference manual (under Erlang Programming)
The FAQs at http://www.erlang.org/faq.html
You can find other Erlang information in the following locations:
The Erlang community site, Trapexit.org, at http://www.trapexit.org.
The website for this book, http://www.erlangprogramming.org, which also has links to all the sites mentioned here, as well as a lot more background information.
The Erlang mailing lists, accessible from http://erlang.org/faq.html and available in archived form at Nabble.com and elsewhere.
The many Erlang-focused blogs. Just search for “Erlang” on http://blogsearch.google.com; many of the blogs are aggregated at http://planet.trapexit.org and at Planet Erlang, http://www.planeterlang.org.
The two annual Erlang events: the Erlang Workshop, sponsored by ACM SIGPLAN and collocated with the International Conference on Functional Programming (http://www.erlang.org/workshop/), and the Erlang User Conference, which takes place in Stockholm each year (http://www.erlang.org/euc/).
The Erlang Factory, which runs commercial Erlang conferences and whose website, http://www.erlang-factory.com/, contains slides and videos from many of the talks given at the conferences.
 Available at http://www.protest-project.eu/publications/survey.pdf, this was undertaken as part of the ProTest project.