Confessions of a Erlang Programmer

Source: Internet
Author: User
Tags openssl snmp ftp client
Erlang can't miss the feast

(Trot into Erlang's world)

Author: Chenglitao (litaocheng@gmail.com)

As programmers, we have hearing a lot of "industry dynamics", "technological innovation", once contacted a lot of "master motto", "authoritative recommendation." Whether these are correct or not is a past.

Now, let's Meet the Erlang feast. First, Experience

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

In the early 2008, the company's project development, I was destined to know Ejabberd, a use of Erlang developed open source Jabber server. I began to fall for its seductive features. It's time to take a serious look at what Erlang looks like.

April 2008, through a variety of data collection, understanding, I decided to study Erlang system.

Today, with 4 months of hard work, I am already familiar with Erlang and are already using Erlang development projects. As a C + + programmer, I'm not afraid to use the word "familiar" or "proficient", but I can be very responsible for Erlang: Erlang is very clever and powerful. Second, confusion

In the face of a new business, our nature will be full of curiosity, but as programmers, many times for the new language we are full of conflict: This new thing is worth learning. Will it make me abandon my old love. Does it have a rich document? is not difficult to understand. What's the future of it? I believe everyone has the same distress as me.

But listen to me, please. We are programmers, we are at the forefront of technological innovation. The user's product, experience is generated through us. We cannot hang on, our laurels are the stagnation of our company and even the industry as a whole. The slogan may be a little loud, but seriously thinking, I believe that friends must have some feeling. third, what is Erlang?

What Erlang is is the first problem we have to face, and only when we know what it is, can we make our decision. The importance of this question is apparent, and it determines whether many readers will continue to look at it. Very nervous.

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

Erlang is a functional (functional) programming language that is oriented to concurrency (concurrency oriented), message oriented.

Concurrent description Erlang supports large-scale concurrent applications, and we can handle thousands of concurrency in applications without impacting each other. Message-oriented, in fact, is for concurrent services. We should all be familiar with multithreading, familiar with lock unlock operations, familiar with possible resource competition and deadlock. In Erlang's world, we can gently erase these distressing words. In Erlang's world, each process is a separate individual, and the interactions between them depend solely on the message. So there will be no deadlock, no 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 offers us. To accommodate large-scale concurrency, process needs to be quickly created and quickly destroyed. The only way to communicate between process is by message, we can send a message to a process by the name of the PID. Process can also receive messages at any time. We do this only for one purpose: to make our system simpler and to implement an efficient language in a simple way.

Erlang is a functional programming language, I do not have a very deep understanding of this, the most obvious feature is that Erlang is full of functions, functions constitute the main body of our products, put these functions into one of the process to let them run up, then the formation of our vibrant products.

Erlang supports bit operations on data and has a rich data persistence mechanism.

It also needs to be explained 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

It also offers 2 composite structures: Tuple,list, which is all the data types for Erlang. 2. Pattern matching

In Erlang's function, some syntax we can use pattern matching, which is a very good feature, we can let the code decide how to do it ourselves:

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

Price (Apple)-> 2.0;

Price (banana)-> 1.2.

We then call Price (Fruit), which returns the specific prices based on the contents of the Fruit variable. The advantage of this is to save our code, we do not have to if...else ... or switch...case to serve it. Also easy to expand the code: Add a new fruit variety, we only need to add a line on it.

One of the most important things to learn about Erlang is pattern matching, but don't confuse it, this match has nothing to do with regular expressions. 3. Variable single assignment

This is an incredible feature, and variables can only be assigned a single time. Yes, in Erlang. Once a value is bound, it cannot be bound again, and the advantage of this is that it facilitates debugging errors (the deeper reason is that Erlang is a concurrent design, and if the variable can be modified, it involves the lock unlock of the resource), and when an error occurs, A variable is what will always be, do not have to find a way to search 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. A rich Libs

A rich libs 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 encryption and decryption related functions, based on OpenSSL related implementation

SSL realizes encrypted socket communication, based on OpenSSL implementation

SSH implementation SSH protocol

Xmerl Implement XML correlation parsing

SNMP Implementation SNMP Protocol (Simple network Management Protocol)

Observer used to analyze and track distributed applications

ODBC enables Erlang to connect to sql-based databases

Orber implement CORBA object request Proxy Service

Os_mon provides monitoring capabilities for the operating system

Dialyzer provides a static code or program analysis tool

Edoc generate documents from source files

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

...

Many friends also offer open source lib, such as Eunit, for unit testing. 5. Flexible and diverse error handling

Erlang was originally developed for telecommunications products, and the goal was to determine its strict 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 to make them a whole, When a process fails, or when it is launched, the other process has the ability to learn about its launch. Monitor, as the name suggests, can be used to monitor a process to determine whether it exits or fails. All of these Erlang offers intrinsic support, and we quickly develop solid products that are not in the wild. 6. Code Heat Replacement

Does your product want uninterrupted updates? 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 process on a remote machine by BIF, sending messages to a process on a remote machine. In the development of distributed applications, we can communicate through sockets like C, C++,java, and can also be developed using Erlang's embedded distributed architecture based on cookies. Of course, you can mix the two. Distributed development is more convenient and fast. Erlang's process operations, error handling, and so on support distributed operations. 8. Ultra-strong concurrency

By adopting its own process and without operating system processes and threads, we can create large-scale concurrent processing while simplifying our programming complexity. We can implement a concurrent TCP server with dozens of lines of code, which I can't even think of in any other language. 9. Multi-Core Support

Erlang allows your application to support multiple processors, and you don't 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 of platforms such as linux,mac,windows, without headaches for code transplants.

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

Open source is a word I like very much, open source means that it is stronger, more open and more equal in the pursuit of equality. Open source will make Erlang better. v. Erlang's interaction with the outside world

Erlang can interact with other languages, such as C, C++,java. Of course, there are enthusiastic friends to provide interaction with other languages, if necessary, you can also according to Erlang's data format, provide a library, let Erang and your beloved language interaction.

Erlang supports distributed development, and you can create a C node, which is like a Erlang node, if you follow Erlang's specifications.

Of course, the most commonly used interaction is the same node, such as we want to call a certain lib, call some of the functions provided by the system, there are two main ways: port and embedded execution.

Port is Erlang's most basic way of interacting with the outside world, the two sides of the interaction through encoding, decoding, the message to the way of byte stream transmission. (The implementation of this channel, depending on the operating system and different, such as UNIX environment, the implementation of pipe, in theory, any support for the port 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 through the Erlang platform, so this is very dangerous, and if your program crashes, there's no reason Erlang will crash. Six, Erlang application scenario

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

Erlang can also be used as a rapid development language for prototyping. vii. The learning process of Erlang

1. Installation first from Erlang official website, download installation Erlang (http://www.erlang.org/download.html)

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

2. Read "Programming Erlang" (Chinese version of the book has been published), and continue to practice the book routines.

3. When problems are encountered, do not retreat, insist on finding a solution

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. Uninterrupted exchanges with you, and jointly improve

Difficulties that may be encountered:

A is not suitable for grammar.

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

(b) Some data types are not clear.

Look at the material carefully, or ask a friend, like me

(c) Lack of Chinese information.

Erlang Chinese information will be more and more, in addition, Erlang related English information is also easier to understand, or that sentence, don't be afraid of trouble eight, Erlang open source project

Ranking in a separate order

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.