Erlang 新手:添加了 IF,变量不起作用?
有谁擅长调试 Erlang 吗?我一生都无法弄清楚出了什么问题。无论我将 Fields 变量放在哪里,Erlang 都会说该行之前有一个错误...
编译消息:
./eventbus.erl:6: syntax error before: FieldPositions
./eventbus.erl:24: variable 'FieldPositions' is unbound
./eventbus.erl:28: Warning: variable 'Ref' is unused
./eventbus.erl:30: Warning: variable 'List' is unused
error
然后是代码本身。
-module(secret).
-export([listen/1, send/1]).
-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]).
FieldPositions = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM","NN","OO","PP"].
listen(Port) ->
{ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
accept(LSocket).
accept(LSocket) ->
{ok, Socket} = gen_tcp:accept(LSocket),
spawn(fun() -> loop(Socket) end),
accept(LSocket).
discrim(<<>>) ->
ok;
discrim([]) ->
ok;
discrim(Info) ->
EventsList = string:tokens(Info,"|"),
process_events(EventsList, FieldPositions).
process_events([],[]) ->
ok;
process_events([],Ref) ->
ok;
process_events(List,[]) ->
ok;
process_events(List,Ref) ->
[RHead|RTail] = Ref,
[Head|Tail] = List,
if
Head == [] ->
process_events(Tail, RTail);
true ->
io:format("message.bus ~s ~s",[RHead,Head]),
process_events(Tail, RTail),
ok
end.
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
%gen_tcp:send(Socket, Data),
%io:format(Data),
discrim(Data),
io:format("~n"),
loop(Socket);
{error, closed} ->
ok
end.
Anyone good at debugging Erlang? I can't figure out for the life of me what's wrong. Wherever I put the Fields variable, Erlang says that there's an error before that line...
Compile messages:
./eventbus.erl:6: syntax error before: FieldPositions
./eventbus.erl:24: variable 'FieldPositions' is unbound
./eventbus.erl:28: Warning: variable 'Ref' is unused
./eventbus.erl:30: Warning: variable 'List' is unused
error
And then the code itself.
-module(secret).
-export([listen/1, send/1]).
-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]).
FieldPositions = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","BB","CC","DD","EE","FF","GG","HH","II","JJ","KK","LL","MM","NN","OO","PP"].
listen(Port) ->
{ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
accept(LSocket).
accept(LSocket) ->
{ok, Socket} = gen_tcp:accept(LSocket),
spawn(fun() -> loop(Socket) end),
accept(LSocket).
discrim(<<>>) ->
ok;
discrim([]) ->
ok;
discrim(Info) ->
EventsList = string:tokens(Info,"|"),
process_events(EventsList, FieldPositions).
process_events([],[]) ->
ok;
process_events([],Ref) ->
ok;
process_events(List,[]) ->
ok;
process_events(List,Ref) ->
[RHead|RTail] = Ref,
[Head|Tail] = List,
if
Head == [] ->
process_events(Tail, RTail);
true ->
io:format("message.bus ~s ~s",[RHead,Head]),
process_events(Tail, RTail),
ok
end.
loop(Socket) ->
case gen_tcp:recv(Socket, 0) of
{ok, Data} ->
%gen_tcp:send(Socket, Data),
%io:format(Data),
discrim(Data),
io:format("~n"),
loop(Socket);
{error, closed} ->
ok
end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试在模块级别声明一个值
FieldPositions
,这在 Erlang 中是不允许的。 (但是您可以在模块级别声明函数,或在函数内声明值。)有许多替代方法可以实现您想要的效果:
使用
-define
宏,非常类似于TCP_OPTIONS
代码中的一个。将
FieldPositions
的值内联到您想要使用它的位置。编写一个常量返回函数,例如:
field_positions() -> ["A","B",...,"PP"].
顺便说一句,根据我使用各种编译器和解释器的经验,我建议您不要过于字面地理解错误消息。特别是,我认为您应该将收到的错误视为“错误发生在 x 行周围的某处”,而不是字面上的“在 x 行之前”或“在 x 行处”。
You tried to declare a value
FieldPositions
at the module level, which is not allowed in Erlang. (But you can declare functions at the module level, or declare values within functions.)There are a number of alternatives to achieve the effect you want:
Use a
-define
macro, much like the one in your code forTCP_OPTIONS
.Inline the value of
FieldPositions
to where you wanted to use it.Write a constant-returning function, like:
field_positions() -> ["A","B",...,"PP"].
As an aside, from my experience with various compilers and interpreters, I recommend you not to take the error messages too literally. In particular, I think you should treat the errors you got as "error happened somewhere around line x", not literally as "before line x" or "at line x".