Erlang Programming Learning Notes-6th chapter compiling and running the program

Source: Internet
Author: User

http://blog.csdn.net/karl_max/article/details/3976372

The 6th chapter compiles and runs the program

1. Erlang:halt () can immediately stop the system from running.

2. The q () command can complete some processing of files and databases before exiting. It is a shell alias for Init:stop ()

3. Erlang can dynamically load code, and the running code can be dynamically trimmed after recompilation without stopping the program from running.

4. Module load related functions:

Code:get_path () percent can get the set value of the current load path.

Code:all_loaded () percent gets all the modules that have been loaded.

Code:clash () to see if the loaded module has a conflict.

Other than that

@spec Code:add_patha (Dir) = True | {error, Bad_directory} percent adds a new directory to the beginning of the load path.

@spec Code:add_pathz (Dir) = True | {error, Bad_directory} percent adds a new directory to the end of the load path.

5. Increase the load path when you start Erlang:

Erl-pa dir1-pa Dir2 ...-pz dirk1-pz DirK2

where-PA equivalent to ADD_PATHA,-PZ equivalent to Add_pathz

6. In the home directory of Erlang (or the current directory) you can add an. erlang file, in which you can write some Erlang code that Erlang executes first at startup.

In general, you can put the command loading the directory into this.

If you do not know the home directory of Erlang under current system, you can use Init:get_argument (home).

The. Erlang file under the current directory has a higher priority than the. erlang file in the home directory.

7. In addition to running a module in the shell, the following methods are available:

Method One: command-line compile run

$ ERLC Hello.erl

$ erl-noshell-s Hello start-s init stop

Percent-noshell means close the shell.

Percent-S Hello start is the start function that runs the Hello module

Percent-init stop means that after you run the Apply (hello, start, []), the Stop function that runs the init module exits

The percent-s command can run any number of

Percent can add-pa/-pz command option on this command line

Percent can write this command line into a shell script (such as a bash script) so that you can directly execute the script to start the Erlang program.

Percent of percent in Windows you can write a bat file that writes this command.

Method Two: Run the program as a escript script (without compiling), only the later version of Erlang r11b-4 support Escript, not in Windows

#!/usr/bin/env Escript

Io:format ("Hello world/n"), Main (_). Percent of this function needs to be main,escript start with this function

Method Three: How to enter parameters on the command line when the program needs parameters.

The-s option is followed by: ModName FuncName args, where args is a parameter list of an atom. So we'd better have a function that handles command-line arguments in the program.

Like this: main ([a])-I = List_to_integer (Atom_to_list (a)), ... the percent is only received here for a parameter, if need two can so main ([A, B]) ...

Escript As with other scripts, the runtime directly puts the parameters on the back.

8. Atom_to_list (Atom)-strings can get the string form of an atom

9. List_to_integer (list) int can get the integer form of a string

10. Quick script: Use-eval to run an Erlang expression directly on the command line, this method is not very useful, I do not seem to show anything in the Windows test.

Erl-eval ' Io:format ("Memory: ~p~n", [Erlang:memory (total)]). '-noshell-s init stop

11. A Makefile Template

# Leave these lines alone

. Suffixes:. erl. Beam. Yrl #. Yrl file is a parsing definition file for the Erlang Parsing generator program (YECC)

. Erl.beam:

Erlc-w $<

. Yrl.erl:

Erlc-w $<

ERL = Erl-boot Start_clean

# Edit the lines below

MODS = Module1 Module2/

Module3 ... special1 .../

Mudulen

All:compile

Compile: ${mods:%=%.beam} subdirs

#特殊情况, special compilation

Special1.beam:special1.erl

${erl}-dflag1-w0 Special1.erl

#运行程序

Application1:compile

${erl}-pa dir1-s application1 start Arg1 Arg2

#编译子目录, makefile are also available in subdirectories

Subdirs:

CD Dir1; Make

CD Dir2; Make

Clean

RM-RF *.beam erl_crash.dump #erl_crash. Dump is a crash dump file for Erlang

CD Dir1; Make clean

CD Dir2; Make clean

12. A streamlined makefile

. Suffixes:. Erl. Beam

. Erl.beam:

Erlc-w $<

ERL = Erl-boot Start_clean

MODS = Module1 Module2 module3

#编译加运行

All:compile

${erl}-pa dir1-s module start

Compile: ${mods:%=%.beam}

Clean

RM-RF *.beam Erl_crash.dump

The Erlang Shell has a subset of line editing commands in the Emacs editor. It's no use for people without Emacs, like me.

Erlang starts with a-detached option that runs behind the scenes

When Erlang starts with a-heart cmd option, a monitoring process is initiated, and if the Erlang process is found to be dead, the monitoring process executes cmd and normally cmd restarts the Erlang system.

16. If a undef error occurs when calling a ERALNG function, there are several possible reasons for this:

(1) The module or function does not exist in the system and may be misspelled.

(2) This module is in the system, but it is not compiled.

(3) has been compiled, but the directory where it resides is not in the load path of Erlang.

(4) Several different versions are loaded, and we can look at the Code:clash () function to see if there are any modules with duplicate names.

. JCL (Job Control Language) mode: Pressing CTRL+G in the Erlang shell enters JCL mode and you can enter H for help.

Man: In the command line of the UNIX system: $ erl-man lists can see the help of the lists module. Of course, you should first install the Help manual for Erlang (not the default installation).

19. Enter the command Help () in the Erlang shell. You will see the built-in commands defined in the module Shell_default.

20. A User_default module can be defined, compiled and placed into the loading directory. This allows the function to be called directly in the Erlang shell without having to write the module name.

21. If Erlang crashes, it will leave a file called Erl_crash.dump. Erlang has a web-based crash analyzer. Start Webtool:start () in the Erlang shell. It started.

Erlang Programming Learning Notes-6th chapter compiling and running the program

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.