Appendix A. Using Erlang

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.

Getting Started with Erlang

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.

Installing the System

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.

Running the Erlang Shell

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 require.

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:

Erl

Opens the file in an Erlang shell from the Command Prompt program

Werl

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 SettingsAdministrator>cd Desktopprogrammingwxex
C:......wxex>"c:Program Fileserl5.7inerl.exe" -smp miniblog.erl
Eshell V5.7  (abort with ^G)
1> miniblog:start().

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

Ctrl-A

Takes the cursor to the start of the line

Ctrl-E

Takes the cursor to the end of the line

Ctrl-D

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:

c(File)

Compiles and loads the File, purging old versions of code

b()

Prints the current variable bindings

f()

“Forgets” all the current variable bindings

f(X)

“Forgets” the binding for the variable X

The history of commands and their results are remembered:

h()

Will print the history list (which has a default length of 20, but can be changed)

e(N)

Will repeat command number N

e(-N)

Will repeat the nth previous command; for example, e(−1) is the previous command

v(N)

The return value of command N

v(-N)

The return value of the nth previous command; for example, v(−1)+v(−2) will return the sum of the previous two values

Other shell commands, including those for dealing with record definitions in the shell, are described in detail in the documentation for the shell module.

Tools for Erlang

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.

Editors

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,[55] 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. 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.

Other Tools

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_tools modules 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.

Where to Learn More

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:



[55] Available at http://www.protest-project.eu/publications/survey.pdf, this was undertaken as part of the ProTest project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset