Confessions of an Erlang programmer

Source: Internet
Author: User
Tags snmp ftp client

The feast that Erlang cannot miss

(Trot into the world of Erlang)

Chenglitao ([email protected])

As programmers, we have upon hearing a lot of "industry dynamics", "technological innovation", has been exposed to a lot of "master Proverbs", "authoritative recommendations." These are right or not, have become the past!

Now, let's Meet the Erlang feast!

First, experience

November 2007 in koders.com Search code, found the *.erl format of the source files, exclamation the development of the language of the pattern, at this time, I think Erlang is an ugly little guy, look at the name is not how much interest to it.

At the beginning of 2008, in the company's project development, I was destined to know Ejabberd, an open source Jabber server developed with Erlang. I began to pour out the seductive features. It's time to take a serious look at what Erlang is like!

In April 2008, through the collection of various materials, I decided to learn the system of Erlang.

Today, with 4 months of serious study, I am already familiar with Erlang, and I am already using Erlang to develop projects. As a C + + programmer, I don't dare to use words like "familiar", "proficient", but I can be very responsible for Erlang: Erlang is very clever, very powerful!

Second, confusion

In the face of a new business, we are full of curiosity, but as programmers, many times for the new language we are full of resistance: This new thing is worth learning? Will it make me abandon my old love? Is it rich in documentation? Isn't it hard to understand? What's the future of it? I believe we have the same distress.

But please listen to me! We are programmers and we are at the forefront of technological innovation. The user's product, the experience is through us to produce! We can not hang on, our laurels, is our company, and even the entire industry is stagnant! Slogan may be some loud, but serious thinking, I believe that friends must have some sentiment.

Third, what is Erlang?

Erlang is the first problem we have to face, and only when we know what it is, can we make our decision. It can be seen that the importance of this problem, it determines whether many readers will continue to watch! Very nervous.

Erlang was originally created by Ericsson to develop telecommunications-related products.

Erlang is a functional (functional) programming language that is oriented toward concurrency (Concurrency oriented) and messages (message oriented).

The concurrency-oriented approach allows Erlang to support large-scale concurrent applications, and we can handle thousands of concurrency in our applications without affecting each other. Message-oriented, in fact, for the concurrency service! We should all be familiar with multi-threading, familiar with the locking unlock operation, familiar with possible resource competition and deadlock. In Erlang's world, we can gently erase these distressing words. Erlang's world, each processing is a separate individual, their interaction is only by the message! So there is no deadlock and there is no such thing as a painful programming experience.

A very important noun in Erlang: Process, the "individual" we mentioned earlier. It is not a process in our operating system, nor is it a thread. It is the super lightweight process that Erlang provides to us. To accommodate large-scale concurrency, the process needs to be quickly created and destroyed quickly. The only way to communicate between the process is by the message, and we can send a message to it just by knowing the name of a process, the PID. Process can also receive messages at any time. We do this for one purpose: to make our system simpler and to implement an efficient language with a simple approach.

Erlang is a functional programming language, which I have no deep understanding of, the most obvious feature is that Erlang is full of functions, functions constitute the main body of our products, put these functions into a process, let them run, then the formation of our vibrant products.

Erlang supports bit manipulation of data and has a rich data persistence mechanism.

It is also necessary to note that the Erlang built-in garbage collection mechanism (GC).

Iv. language characteristics of Erlang 1. Simple and Compact

There are only 8 basic data types in Erlang:

Integer, float, atom, reference, fun, port, PID, bitstring

There are also 2 composite structures available: Tuple,list, which is all the data types of Erlang.

2. Pattern matching

In the function of Erlang, we can use pattern matching in some syntax, which is a very good feature, and we can let the code decide for itself how to do it:

For example, we define a function that tells us the price of a certain fruit:

Price (Apple), 2.0;

Price (banana), 1.2.

We then call Price (Fruit), which returns specific prices based on the contents of the Fruit variable. The advantage of this is to save our code, we don't have to if...else ... or Switch...case to serve. Also facilitates the extension of the code: Add a new fruit variety, we only need to add a row on it.

One of the most important things about learning Erlang is pattern matching, but don't confuse the match with regular expressions.

3. Variable one-time assignment

This is a bizarre feature, the variable can only be assigned a single time! Yes, once a variable is bound to a value in Erlang, it is not possible to bind it again, and the benefit is that it is easy to debug (the deeper reason is that Erlang is designed for concurrency, and if the variables can be modified, it involves issues such as locking unlocking of resources), when an error occurs, A variable is what is always what, without the search for clues who modified it, save a lot of things. The only trouble is that when you need a variable for a letter, you have to think of a name for it again.

4. Rich Libs

The rich Libs available in Erlang

Stdlib contains a large number of data structures such as lists,array,dict,gb_sets,gb_trees,ets,dets, etc.

Mnesia provides a distributed database system

Inets provides FTP client,http client/server,tftp client/server

Crypto provides cryptographic decryption related functions, based on OpenSSL-related implementations

SSL enables encrypted socket communication, based on the OpenSSL implementation

SSH implementation SSH protocol

Xmerl implementing XML-related parsing

SNMP Implementation SNMP Protocol (simple Network Management Protocol)

Observer for analyzing and tracking distributed applications

ODBC enables Erlang to connect to SQL-based databases

Orber implement CORBA object request Proxy Service

Os_mon provides monitoring functions for the operating system

Dialyzer provides a static code or program analysis tool

Edoc generating documents based on source files

GS can provide us with some GUI functions (based on TCL/TK)

...

There are also many friends who provide open source lib, such as Eunit, for unit testing.

5. Flexible and diverse error handling

Erlang was originally developed for telecom products, and the purpose of this was to determine its stringent requirements for error handling. Erlang provides syntax such as Exception,catch,try...catch in the general language, while Erlang supports link and monitor two mechanisms, we can connect the process together and let them form a whole, When a process goes wrong, or when it is launched, other process has the ability to know its launch. The monitor, as its name implies, can be used to monitor a process to determine whether it exits or goes wrong. All of these Erlang provide built-in support, we quickly develop a strong product, not a luxury.

6. Code Hot Swap

Do you want to update the product continuously? Erlang can meet your needs, and Erlang will automatically replace the old modules at run time. Everything was quiet.

7. Natural distributed

Erlang is a natural fit for distributed application development, and many of its BIF (built-in functions, phase APIs) have distributed versions, and we can create a process on a remote machine through BIF, which can send messages to a process on a remote machine. In the development of distributed applications, we can communicate through sockets like C, C++,java and other languages, and can also be developed using the embedded cookie-based distributed architecture in Erlang. Of course, you can mix the two. Distributed development is more convenient and fast. Erlang's process operations, error handling, and so on are all supported for distributed operations.

8. Ultra-strong concurrency

By taking its own process and not using the operating system's processes and threads, we can create large-scale concurrency processing while simplifying our programming complexity. We can implement a concurrent TCP server with dozens of lines of code, which in other languages would not dare to think!

9. Multi-Core support

Erlang enables your application to support multiple processors, and you do not need to do different development for different hardware systems. Using Erlang will maximize the performance of your machine.

10. Cross-platform

Like Java, Erlang supports cross-platform (which currently supports 19 platforms such as linux,mac,windows) without headaches for porting code.

We just need to understand some of the features of the platform and optimize the runtime.

11. Open Source

Open source is one of my favorite words, open source means that it is stronger, more open, and more equal to the pursuit of equality. Open source will make Erlang better.

V. Interaction between Erlang and the outside world

Erlang can interact with other languages, such as C, C++,java. Of course there are enthusiastic friends who provide interaction with other languages, and if needed you can also provide a library for Erang to interact with your beloved language, depending on the data format of Erlang.

Erlang supports distributed development, and you can create a C node, like an Erlang node, if you follow the specifications of Erlang.

Of course, the most common interaction is the same node, for example, we want to invoke a Lib, some system-provided functions, this time there are two main ways: port and embedded execution.

Port is the most basic method of Erlang interaction with the outside world, the interaction between the two sides by encoding, decoding, the information is transmitted in the way of byte stream. (The implementation of this channel, depending on the operating system and different, such as the UNIX environment, using pipe implementation, theoretically any support corresponding to the port channel implementation of the language can be interactive with Erlang). Erlang provides erl_interface and jinterface for the convenience of C and Java programmers.

With port, your code runs outside of Erlang's platform, and its crash does not affect Erlang.

Embedded execution, loaded via the Erlang platform, is therefore very dangerous, and if your program crashes, Erlang will crash for no reason.

VI. Application Scenarios for Erlang

Distributed products, Web servers, clients, and other application environments.

Erlang can also be used as a rapid development language for prototype development.

Vii. learning process of Erlang

1. Install first from Erlang official website, download and install Erlang (http://www.erlang.org/download.html)

Linux: Get source code, compile according to instructions; Windows: Direct installation

2. Read "Programming Erlang" (the Chinese version of the book has been published) and keep practicing the routine in the book.

3. Do not retreat in the face of problems, persist in finding solutions

4. When you are familiar with the language, browse some good open source projects

5. When you are confident, start doing a small project

6. Continuously communicate with you to improve together

Difficulties that may be encountered:

A) not suitable for grammar?

Keep looking, code continues to write, I believe 1 months, you will love the syntax of Erlang

b) Some data types are unclear?

Read the information carefully, or ask a friend, like me

c) Lack of Chinese information?

There will be more and more information on Erlang, in addition, the relevant English material of Erlang is easier to understand, or that sentence, don't be afraid of trouble

Viii. Open Source project of Erlang

In no particular order of precedence

    • COUCHDB database based on unstructured data, such as documents, provides HTTP interfaces
    • Disco Map-reduce frame, Erlang + Python
    • Ejabberd performance, using a wide range of Jabber open source servers
    • Mochiweb Lightweight, efficient HTTP application framework
    • RABBITMQ Intermediate Server to implement AMQP protocol
    • Yaws Efficient Web Server
    • Etorrent BitTorrent Client
    • Scalaris Distributed Key-value Storage
Nine, encounter problems

See the official Erlang document http://www.erlang.org/doc/

Subscribe to Erlang's maillist (http://www.erlang.org/mailman/listinfo/erlang-questions) to ask questions

Search in the Erlang maillist archive provided by Nabble (http://www.nabble.com/Erlang-f14095.html)

Search for answers in Google

Ten, recommended Reading

Erlang Design Principles (http://www.erlang.org/doc/design_principles/part_frame.html)

Erlang Efficiency Guide (http://www.erlang.org/doc/efficiency_guide/part_frame.html)

Erlang programming Rules (http://www.erlang.se/doc/programming_rules.shtml)

XI. Recommended Sites

http://www.erlang.org

http://erlang-china.org

Http://trapexit.org (domestic blockade, can be logged in with http://trapexit.org.nyud.net:8080/or other agents)

Http://toquick.com

http://blog.socklabs.com/

http://www.planeterlang.org/

Confessions of an Erlang programmer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.