CodeMan

Each of us must find a way to love the world. You have found yours.

0%

Programming Erlang exercise 2.4

1. 启动并停止Erlang Shell
1
2
3
4
5
6
yfydemacbook-pro:~ yfy$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1 (abort with ^G)
1>

启动后显示的提示信息解释:

Erlang/OPT 18

erts

smp

async-threads

  • Async threads pool

hipe

kernel-poll

  • 是否启用内核poll模式,可以降低CPU战用就绪

dtrace

2. 在erlang shell中输入一些命令,不要忘了以句号和空白结束命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yfydemacbook-pro:~ yfy$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1 (abort with ^G)
1> hello
1> .
hello
2> 123.
123
3> True.
* 1: variable 'True' is unbound
4> true.
true
5> foo
5> .
foo
6>

3. 对hello.erl做一些小改动,在shell里面编译并运行它们,如果有错,中止Erlang shell并重启shell。

原hello.erl

1
2
3
4
5
-module(hello).
-export([start/0]).

start() ->
io:format("Hello world~n").

更改后hello1.erl

1
2
3
4
5
-module(hello1).
-export([start/0]).

start() ->
io:format("Hello world~n");

中止后重新运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yfydemacbook-pro:src yfy$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1 (abort with ^G)
1> c(hello2).
hello2.erl: no such file or directory
error
2>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
a
yfydemacbook-pro:src yfy$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1 (abort with ^G)
1>
4. 运行客户端和服务器代码。加入一个名为put_file的命令。你需要添加何种消息?学习如何查阅手册页。查阅手册页里的file模块。
  • 修改后的代码

afile_client.erl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
%%%-------------------------------------------------------------------
%%% @author yfy
%%% @copyright (C) 2016, <COMPANY>
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(afile_client).
-author("yfy").

%% API
-export([ls/1, get_file/2,put_file/2]).

ls(Server) ->
Server ! {self(), list_dir},
receive
{Server, FileList} ->
FileList
end.

get_file(Server, File) ->
Server ! {self(), {get_file, File}},
receive
{Server, Content} ->
Content
end.

put_file(Server, File) ->
Server ! {self(), {put_file, File}},
receive
{Server, Content} ->
Content
end.

file_server.erl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
%%%-------------------------------------------------------------------
%%% @author yfy
%%% @copyright (C) 2016, <COMPANY>
%%% @doc
%%% @end
%%%-------------------------------------------------------------------
-module(afile_server).
-author("yfy").

%% API
-export([start/1, loop/1]).

start(Dir) -> spawn(afile_server, loop, [Dir]).

loop(Dir) ->
receive
{Client, list_dir} ->
Client ! {self(), file:list_dir(Dir)};
{Client, {get_file, File}} ->
Full = filename:join(Dir, File),
Client ! {self(), file:read_file(Full)};
{Client, {put_file, File}} ->
NewFilename = "newfilea.txt",
NewFull = filename:join(Dir, NewFilename),
Origion = filename:join(Dir, File),
{ok,Binary} = file:read_file(Origion),
{ok,S} = file:open(NewFull,[raw,write,binary]),
Client ! {self(), file:pwrite(S,0,Binary)}
end,
loop(Dir).

put_file只是把文件读出来,以另外的文件重新保存。

1
2
yfydemacbook-pro:cp2_3 yfy$ erl -man file

  • 调试erlang程序
1
2
3
4
5
6
7
8
9
10
11
12
yfydemacbook-pro:cp2_3 yfy$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V7.2.1 (abort with ^G)
1> c(afile_server,[debug_info]).
afile_server.erl:7: Warning: Non-UTF-8 character(s) detected, but no encoding declared. Encode the file in UTF-8 or add "%% coding: latin-1" at the beginning of the file. Retrying with latin-1 encoding.
{ok,afile_server}
2> c(afile_server,[debug_info]).
{ok,afile_server}
3> c(afile_client,[debug_info]).
{ok,afile_client}
4> FileServer=debugger:quick(afile_server,start,["."]).

解决Warning方法是查看erl文件中包含有中文字符。这里我直接去掉后重新编译就没有了。

如需要使用debugger工具,编译方式如:c(xxx,[debug_info]). 使用debugger:quick启动debugger后界面:
debugger界面

可以在其中添加断点,调试。