Erlang cannot miss the feast (Classic)

Source: Internet
Author: User
Tags ftp client

As a programmer, we once heard a lot of "industry news" and "technological innovation", and once came into contact with many "Master sayings" and "authoritative recommendations ". These are correct or not!

Now, let's welcome the Erlang feast!

I. Experience

When searching for code in koders.com on December 31, November 2007, we found *. the source file in ERL format is amazed at the variety of development languages. At this time, I think Erlang is an ugly little guy and I am not very interested in it when I look at the name.

At the beginning of 2008, during the company's project development, I got to know ejabberd, an open-source Jabber server developed using Erlang. I began to fall into favor of its attractive features. It's time to take a closer look at what Erlang looks like!

In April 2008, I learned from the collection of various materials that I decided to systematically learn Erlang.

Today, after four months of hard work, I am already familiar with Erlang and am already using Erlang development projects. As a C ++ programmer, I do not dare to use words like "familiar" and "proficient". But for Erlang, I can say responsibly: Erlang is very clever and powerful!

2. confusions

In the face of a new transaction, our nature will be full of curiosity, but as a programmer, we are often full of resistance to the new language: Is this new thing worth learning? Will it make me abandon old love? Is it rich in documentation? Is it hard to understand? What is its prospect? · I believe that everyone has the same troubles as me.

However, please let me know! We are programmers and we are at the forefront of technological innovation. The user's product experience is produced by us! We can't hold on to it. We stick to it, that is, our company and even the whole industry are stuck! The slogan may be a bit loud, but I believe my friends may feel it.

3. What is Erlang?

Erlang is the first problem we need to face. We can make our decisions only when we know what it is. The importance of this issue determines whether many readers will continue reading it! Very nervous.

Erlang was initially developed by Ericsson to develop telecommunications-related products.

Erlang is a function programming language oriented to concurrency (concurrency oriented) and message oriented (Message Oriented.

Concurrency-oriented description Erlang supports large-scale concurrent applications. We can process thousands of concurrent applications without affecting each other. Message-oriented service is actually a concurrent service! We should be familiar with multithreading, locking and unlocking, and possible resource competition and deadlocks. In the Erlang world, we can gently erase these annoying words. In the Erlang world, each processing is an independent individual, and the interaction between them depends only on messages! Therefore, there will be no deadlocks and no painful programming experience.

An important term in Erlang: process, which is the "individual" we mentioned earlier ". It is neither a process in our operating system nor a thread. It is a super lightweight process that Erlang provides to us. To adapt to the features of large-scale concurrency, the process needs to be quickly created and destroyed. The only method for communication between processes is the message. We can send a message to a process whose name is PID. Process can also receive messages at any time. We have only one purpose: to make our system simpler and implement an efficient language with a simple approach.

Erlang is a functional programming language. I have no deep understanding of this. The most obvious feature is that Erlang is full of functions, which constitute the main body of our products, put these functions in one process to let them run, then it constitutes our vigorous product.

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

At the same time, it should be noted that Erlang has a built-in garbage collection mechanism (GC ).

Iv. Language Features of Erlang 1. Simple and compact

There are only eight basic data types in Erlang:

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

At the same time, two composite structures are provided: tuple and list, which are all data types of Erlang.

2. Pattern Matching

In Erlang functions, we can use pattern matching in some syntaxes. This is a very good feature. We can let the code decide how to execute 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 the price (fruit) function to return the specific price based on the content of the fruit variable. The advantage of doing so is to save the amount of our code and we don't need to use if... else... Or switch... Case is coming. It also facilitates code Extension: we only need to add a line to add a new fruit variety.

A very important part of learning Erlang is pattern matching, but do not confuse it. This matching has nothing to do with regular expressions.

3. assign values to variables at a time

This is an incredible feature. A variable can only be assigned a value at a time! Yes, once a variable in Erlang is bound to a value, it cannot be bound again. The advantage of this is that it facilitates debugging errors (the deeper reason is that Erlang is designed for concurrency. If the variable can be modified, so it involves the issue of resource locking and unlocking.) When an error occurs, a variable is always what it is. You don't have to worry about who has modified it, saving a lot of trouble. The only trouble is that when you need a variable of a letter, you have to name it again.

4. Rich libs

Erlang provides rich libs

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

Mnesia provides a distributed database system

Inets provides FTP client, HTTP client/server, and TFTP client/server

Crypto provides encryption and decryption functions based on OpenSSL implementations.

SSL implements encrypted socket communication based on OpenSSL

SSH protocol

XML parsing using xmerl

Simple Network Management Protocol (SNMP)

Observer is used to analyze and track distributed applications.

ODBC enables Erlang to connect to SQL-based databases

Orber implements the client-side request proxy service

OS _mon provides the OS monitoring function

Dialyzer provides a static code or program analysis tool

Edoc generates documents based on the source file

GS can provide some gui functions (based on Tcl/tk) for us)

...

Many other friends provide some open-source libraries, such as eunit, for unit testing.

5. flexible and diverse error handling

Erlang was initially developed for telecom products. This goal determined its strict requirements on error handling. Exception, catch, try… provided by general languages in Erlang... Catch and other syntaxes. At the same time, Erlang supports the link and monitor mechanisms. We can connect the processes to make them a whole. When a process fails or is released, other processes have the ability to learn about their launch. Monitor, as its name implies, can be used to monitor a process and determine whether it exits or fails. All of these Erlang products provide internal support, so we can develop robust products quickly.

6. Hot code replacement

Do you want to update your product continuously? Erlang can meet your needs. Erlang will automatically replace the old module at runtime. Everything is quiet.

7. Inherent Distribution

Erlang is designed for distributed application development. Many of its BIF (built-in functions and APIs) have distributed versions. We can use BIF to create a process on a remote machine, you 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, or use the cookie-based distributed architecture embedded in Erlang, for development. You can also mix the two. Distributed development is more convenient and fast. Erlang process operations and error processing all support distributed operations.

8. High concurrency

Because of its own process, rather than operating system processes and threads, we can create large-scale concurrent processing, while also simplifying our programming complexity. We can use dozens of lines of code to implement a concurrent TCP server, which is hard to think about in other languages!

9. multi-core support

Erlang enables your applications to support multiple processors. You do not need to develop different hardware systems. Using Erlang will maximize the performance of your machine.

10. cross-platform

Like java, Erlang supports cross-platform platforms (which currently supports 19 platforms, such as Linux, Mac, and Windows), without worrying about code porting.

We only need to understand some features of the platform and optimize it during 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. Open source will make Erlang better.

5. Interaction between Erlang and the outside world

Erlang can interact with other languages, such as C, C ++, and Java. Of course, some enthusiastic friends also provide interaction with other languages. If needed, you can also provide a library based on Erlang's data format to allow erang to interact with your beloved language.

Erlang supports distributed development. You can create a C node, which is like an Erlang node, provided that you comply with Erlang specifications.

Of course, the most common interaction is on the same node. For example, if we want to call a lib and call some functions provided by the system, there are two main methods at this time: Port and embedded execution.

Port is the most basic way for Erlang to interact with the outside world. The two sides of the interaction are encoded, decoded, and the information is transmitted in byte streams. (The specific implementation method of this channel varies depending on the operating system. For example, in a Unix environment, pipe is used for implementation, theoretically, any language that supports the corresponding port channel can interact with Erlang ). Erlang provides erl_interface and jinterface to facilitate C and Java programmers.

Using port, your code runs outside the Erlang platform, and its crash does not affect Erlang.

Embedded execution is loaded through the Erlang platform, so this is very dangerous. If your program crashes, there is no reason, Erlang will crash.

Vi. Erlang application scenarios

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

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

VII. Erlang Learning Process

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

Linux: Get the source code and compile it according to instructions; windows: Install it directly

2. Read programming Erlang carefully (the Chinese version has been published) and keep practicing the routines in the book.

3. When you encounter problems, do not retreat and stick to it to find a solution.

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

5. Start a small project with confidence

6. continuous communication with everyone to improve

Possible difficulties:

A) Are you not comfortable with the syntax?

Keep reading the code and continue writing it. I believe that you will like the Erlang syntax for one month.

B) are some data types unclear?

Read the information carefully or ask a friend, such as me.

C) What is the lack of Chinese materials?

There will be more and more documents in Erlang. In addition, it is easier to understand the relevant English documents of Erlang.

8. Open-source Erlang Projects

Ranking in no particular order

  • Couchdb provides HTTP interfaces based on documents and other unstructured data Databases
  • Disco map-Reduce framework, Erlang + Python
  • Ejabberd has outstanding performance and is widely used as an open-source Jabber server.
  • Mochiweb lightweight and efficient HTTP application framework
  • The rabbitmq intermediate server implements the amqp protocol.
  • Yaws efficient Web Server
  • Etorrent BitTorrent client
  • Scalaris Distributed Key-value Storage
IX. Problems Encountered

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

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

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

Search for answers in Google

10. 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 (http://www.erlang.se/doc/programming_rules.shtml)

11. Recommendation websites

Http://www.erlang.org

Http://erlang-china.org

Http://trapexit.org (domestic blockout, can use http://trapexit.org.nyud.net: 8080/or another Proxy login)

Http://toquick.com

Http://blog.socklabs.com/

Http://www.planeterlang.org/

 

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.