[Erl_question11] Erlang traps 11-20

Source: Internet
Author: User

11.Every time you reinstall the system, Erlang will be re-installed. Ubuntu installs sh to kill everything.

Https://gist.github.com/zhongwencool/11174620

12.Tips for hiding Erlang shell:

F (). % release all bound variables F (VAL ). % release the Val variable V (line ). % re-execute the line function once V (-1 ). %Execute the function of the previous row again.Rr (Module). %Load the record in the module into the shell. [very useful]Rr ("*/*"). % load the record in all modules in this subdirectory to the shell RP (expression ). % print all elements of expression to shell [very useful]
RL (). % lists all defined record. RF (recordname). % does not load a record named recordname
%If debug_info information is added to your code [compile: file (module, [debug_info])], you can view the source code in this way.{OK, {_, [{abstract_code, {_, AC}] }= beam_lib: chunks (beam, [abstract_code]), IO: fwrite ("~ S ~ N ", [erl_prettypr: Format (erl_syntax: form_list (AC)]).

13.Erlang has many irreversible functions: such as binary_to_list/1, list_to_binary/1

  > binary_to_list(list_to_binary([ <<1,2>>,<<3,4>>])).    [1,2,3,4]

This pitfall is a bit deep. You only know it after you step on it! There is also a small tip with binary:

   > <<"xyz","ets","bt">> =:= <<"xyzetsbt">>.                true

Binary is equivalent.

14.Inside the Protection Type,Equivalent to andalso;It is equivalent to orelse ------[However: Not exactly the same, for the following reasons:]

% Similarities:
Right_age (x) when x> = 16, x = <104-> % x> = 16 andalso x = <104 true; right_age (_)-> false.

Differences:

% When condition1 has an exception, condtion2 will be determined. If orelse is used, false func () When condition1; condition2-> OK; func ()-> error will be returned directly.

Can be compared with the first http://www.cnblogs.com/zhongwencool/p/3712909.html, understanding deeper Oh.

15.If you want to use a binary tree in practice, see gb_tree.erl and do not recreate unnecessary wheels. [However, you can consider doing exercises]

16.Tail recursion is prohibited in try catch exception handling:

Erlang absolutely believes that exception handling is normal because there is a protection mechanism in this exception handling part. If you use tail recursion here, the following situations will occur:

1) tail recursion becomes an endless loop [which may happen in project practice]; 2) a lot of processes enter this exception, and this tail recursion process is very complicated, after the VM runs for a long time, it blocks a large number of processes and consumes memory.

The result is: the memory is exhausted or the program is very slow. The key is that you still cannot find the cause of the final crash.

Therefore, we recommend that you only use necessary and simple processing to handle exceptions.

17.If you want to kill a process in Erlang: Find the PID and exit (PID, reasno). [The effect is the same as that of kill]

In most cases, it does not work !!!!! Because in the project, if your processes are under the monitoring tree in combination, if you are exit/2, it will be automatically restarted by the monitoring tree, so you need to remove the monitoring tree from the process first !!!

    supervisor:terminate_child(SupPid,Pid),     supervisor: delete_child(SupPid,Pid).

18.Two processes can be connected in two ways or in one way:

Two-way: Link (pid1, pid2). Duplicate calls have the same effect. That is to say, no matter how many links are called for the same two processes, you only need to use one unlink (pid1, pid2) to cancel the connection.

Unidirectional: Erlang: Monitor/2 Erlang: demonitor/1

19.Receive after time

The maximum time value is 50*24*60*60*1000. If the time exceeds 50 days, an error is returned:

Therefore, we need to split the time into a list smaller than the maximum value:

  normalize(Time) –>      Limit = 49*24*60*60,      [Time rem Limit | lists:duplicate(Time div Limit, Limit)].

Then, if the time list is not empty, the list will be reduced and waited until the list is empty:

  loop([T|Next]}) –>     receive        {Server, Ref, cancel} –>           Server ! {Ref, ok}        after T*1000 –>           if Next =:= [] –>               Server ! {done, S#state.name};              Next =/= [] –>              loop(Next})           end     end.

20.The return values of handle_call/3 and handle_cast/2 in gen_server can be added with a timeout time.

If this time is not completed, it will issue a timeout information: handled by handle_info, specific visible: http://www.cnblogs.com/zhongwencool/p/erlang_timer.html in method 2.

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.