Chapter 12. Next Steps Through Erlang

Hopefully you now feel comfortable writing basic Erlang programs, and understand roughly how modules and processes build into programs. You should be ready to experiment with writing Erlang code, but more importantly, you should be ready to explore other resources for mastering Erlang and its many powerful libraries. There’s a lot to explore!

Moving Beyond the Shell

The Erlang shell is a great place to test code, and to poke and prod Erlang code. You’ll likely spend a lot more time in the shell if you keep using Erlang, but the way you use it may change.

You can compile and run Erlang code outside of the shell, and this makes it much easier to integrate Erlang work with tools you typically use to manage code and related resources. Erlang’s make module is a common place to start, letting you create Emakefile files that provide instruction to the erl -make command.

If you want to automate your Erlang builds further, there are many ways to integrate Erlang with the classic Make toolset. For a more thoroughly Erlang-centric build experience, you may want to explore rebar, at https://github.com/rebar/rebar. You can even mix Make and rebar to apply the strengths of each.

If you want to use Erlang from an IDE, you may want to explore http://erlide.org/, a set of tools for working with Erlang in Eclipse. Emacs users will want to explore the Erlang mode.

Distributed Computing

Almost everything you’ve learned in this book points toward a computing model that makes it easy to distribute programs across a network of Erlang nodes. Setting up a set of nodes isn’t that difficult. It may even be too easy in some ways to let security-obsessed administrators sleep easily.

Before you set up large sets of nodes, you’ll want to know much more about how Erlang schedules code to run, how messages get passed among nodes, and how to administer Erlang nodes remotely. All of that is out there, providing the foundation on which tools like OTP and Mnesia can build.

Processing Binary Data

Erlang includes a binary data type, binary operators, and a variety of libraries for processing binary data. If you need to build network protocols that handle bits on the wire, or ASN.1 data, for example, Erlang offers powerful tools for getting information into and out of binary form. If you see numbers or strings enclosed in << and >>, you’ve encountered Erlang’s tools for handling binary data. They allow you to specify, pattern match, and process binary data structures.

Input and Output

Chapter 4 and Chapter 5 introduced you to the io:format functions in the context of presenting information in the shell. The io module offers much more, however, for reading and writing data, and the file, filename, filelib, and io_lib modules give you the tools you need to get into and out of files. If networking is more your style, you’ll want to explore the gen_tcp, gen_udp, and inet modules.

Testing, Analyzing, and Refactoring

Functional programming approaches should, once you get used to them, make it easier to create clean code. However, it is always possible to tie yourself in knots, especially as you move toward solving more complex problems than the ones presented in this book.

Unit testing is one approach to making sure that your code keeps working as you move forward. Focused on small components of your programs that should be able to reliably return a set of correct outputs from a given set of inputs, unit testing can both help to tell you when you’ve made your code work and warn you when it breaks. Erlang includes the EUnit framework for unit testing and the Common Test framework for system testing.

Erlang also includes Dialyzer, the Discrepancy Analyzer for Erlang, which can help you catch basic errors of sending wrongly typed data, code that never gets called, and similar issues that compilers for statically typed languages are usually good at catching. Erlang also includes profilers and coverage tools; you should explore the eprof, fprof, and cover modules, and the cprof tool.

If you’re excited about refactoring code—once you have more of it—you may want to explore Wrangler, at http://www.cs.kent.ac.uk/projects/forse/wrangler/doc/overview-summary.html. It allows you to explore your code and automate a wide variety of common program modifications.

Networking and the Web

Erlang is a natural match for a world of web programming in which the number of users is constantly increasing, data needs to move smoothly among nodes, and customers are starting to expect critical web applications to work as reliably as the telephone system. Most web applications are large pipelines of data, well-suited for Erlang’s strengths.

Several Erlang frameworks let you build web applications. Yaws (http://yaws.hyber.org/), a web server itself written in Erlang, offers an environment that should be fairly familiar to anyone building web applications. Yaws lets you mix Erlang code with HTML (and other) templates, making it fairly easy to get a website or application together on an Erlang foundation. If Yaws doesn’t look like it’s for you, you may also want to explore Mochiweb (https://github.com/mochi/mochiweb) or Cowboy (https://github.com/extend/cowboy). For a more comprehensive framework that can run with any of these, take a look at Nitrogen (http://nitrogenproject.com/).

If you have fallen for the siren song of REST-based service development, you may also want to explore Webmachine (http://wiki.basho.com/Webmachine.html), a toolkit for HTTP processing that brings you very close to the core of the Web’s foundation protocol. Even if you don’t end up using it, exploring its flow diagram at http://wiki.basho.com/Webmachine-Diagram.html will teach you a lot about what’s involved in processing a web request.

Data Storage

You already have ETS, DETS, and Mnesia. What else might you need?

Many people are using Erlang without knowing they are using it, as they interact with the popular NoSQL databases CouchDB (http://couchdb.apache.org/) and Riak (http://basho.com/products/riak-overview/). Their Erlang underpinnings make them easy to distribute and manage, and they’ve both reached large and growing audiences. For a great brief intro to them (and five other database options), you should explore Seven Databases in Seven Weeks (Pragmatic Programmers). It won’t teach you much about using them with Erlang, but it will give you a solid foundation that will help you explore their Erlang interfaces once their broad approaches make sense to you.

Many other databases have Erlang interfaces, and there is support for the classic ODBC connections.

Extending Erlang

If you need to wring out every drop of performance on a complex task, or want to avoid rewriting a library written in a language other than Erlang, you’ll want to explore Erlang’s tools for connecting with other programming languages. Erlang Programming explores Java, C, and Ruby connections, but also notes approaches you can use to connect with .NET languages, Python, Perl, PHP, Haskell, Scheme, and Emacs Lisp. You’ll want to examine native implemented functions (NIFs) and drivers.

Languages Built on Erlang

Erlang may put the fun in functional programming, but its structures may feel brittle if you’re used to the focus on flexibility that many other languages provide. Elixir (http://elixir-lang.org/) combines the Erlang Runtime System with a very different syntax more focused on polymorphism, meta-programming, and associative data structures. (One of Erlang’s creators, Joe Armstrong, is also working in similar directions with his Erl2 work, which you can find at https://github.com/joearms/erl2.)

Erlang’s runtime model and tools are powerful and unique, and there may be other great ideas coming that will let you apply them to work that might not seem on the surface to be written in Erlang.

Community

As you learn more about Erlang, you’ll find a community happy to help you at every level. The erlang-questions mailing list is at its heart, and welcomes beginners. During the writing of this book, I found its archives incredibly valuable. You can find subscription and archive information at http://erlang.org/mailman/listinfo/erlang-questions, and you will probably run into its archives regularly if you do searches on Erlang. If you prefer live chat to email, there is also an #erlang IRC channel on freenode (http://freenode.net).

If you prefer “real” live chat to the Internet, Erlang has a thriving conference circuit. For completely Erlang-focused venues, check out Erlang Factory (http://www.erlang-factory.com/), which produces a number of shows around the world, including the Erlang User Conference. The Association for Computing Machinery (ACM)’s Special Interest Group on Programming Languages (SIGPLAN) also holds an Erlang Workshop, about which you can learn more at http://www.erlang.org/workshop/.

There are also a lot of more informal “Erlounges” in a wide variety of locations, and many larger conferences, like O’Reilly’s own Open Source Conference (OSCON), include Erlang sessions and tutorials.

If you want to explore Erlang code, there’s lots of it on github; you can look around at the most active projects by visiting https://github.com/languages/Erlang.

Sharing the Gospel of Erlang

It may seem easy to argue for Erlang. The broad shift from single computers to networked and distributed systems of multiprocessor-based computing gives the Erlang environment a tremendous advantage over practically every other environment out there. More and more of the computing world is starting to face exactly the challenges that Erlang was built to address. Veterans of those challenges may find themselves breathing a sigh of relief as they can stop pondering toolsets that tried too hard to carry single-system approaches into a multi-system world.

At the same time, though, I’d encourage you to consider a bit of wisdom from Joe Armstrong: “New technologies have their best chance a) immediately after a disaster or b) at the start of a new project.” (http://erlang.org/pipermail/erlang-questions/2012-October/069626.html)

While it is possible you’re reading this because a project you’re working on has had a disaster (or you suspect it will have one soon), it’s easiest to apply Erlang to new projects, preferably projects where the inevitable beginner’s mistakes won’t create new disasters.

Find projects that look like fun to you, and that you can share within your organization or with the world. There’s no better way to show off the power of a programming language and environment than to build great things with it!

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

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