Erlang:如何将路径字符串传递给函数?

Erlang:如何将路径字符串传递给函数?,erlang,otp,Erlang,Otp,这周我开始学习二郎。 我根据OTP gen_服务器的行为创建了一个新文件 这是它的开始: -module(appender_server). -behaviour(gen_server). -export([start_link/1, stop/0]). -export([init/1, handle_call/3, handle_cast/2]). start_link(filePath) -> gen_server:start_link({local, ?MODULE},

这周我开始学习二郎。 我根据OTP gen_服务器的行为创建了一个新文件

这是它的开始:

-module(appender_server).
-behaviour(gen_server).

-export([start_link/1, stop/0]).
-export([init/1, handle_call/3, handle_cast/2]).

start_link(filePath) ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, filePath, []).

init(filePath) ->
    {ok, theFile} = file:open(filePath, [append]). % File is created if it does not exist.

...
使用c(appender_服务器)编译的文件正常

当我尝试从shell调用start_link函数时,如下所示:

appender_server:start_link("c:/temp/file.txt").
appender_server:start_link(filePath)
我得到:

**异常错误:没有与appender\u server:start\u链接(“c:/temp/file.txt”)(appender\u server.erl、, 第10行)

我做错了什么


谢谢。

文件路径是一个原子,而不是一个变量:

7> is_atom(filePath).
true
在erlang中,变量以大写字母开头。erlang与function子句匹配的唯一方法是如下调用函数:

appender_server:start_link("c:/temp/file.txt").
appender_server:start_link(filePath)
以下是一个例子:

-module(a).
-compile(export_all).

go(x) -> io:format("Got the atom: x~n");
go(y) -> io:format("Got the atom: y~n");
go(X) -> io:format("Got: ~w~n", [X]).
在外壳中:

3> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

4> a:go(y).
Got the atom: y
ok

5> a:go(x).
Got the atom: x
ok

6> a:go(filePath). 
Got: filePath
ok

7> a:go([1, 2, 3]).
Got: [1,2,3]
ok

8> 

filePath
是一个原子,而不是一个变量:

7> is_atom(filePath).
true
在erlang中,变量以大写字母开头。erlang与function子句匹配的唯一方法是如下调用函数:

appender_server:start_link("c:/temp/file.txt").
appender_server:start_link(filePath)
以下是一个例子:

-module(a).
-compile(export_all).

go(x) -> io:format("Got the atom: x~n");
go(y) -> io:format("Got the atom: y~n");
go(X) -> io:format("Got: ~w~n", [X]).
在外壳中:

3> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

4> a:go(y).
Got the atom: y
ok

5> a:go(x).
Got the atom: x
ok

6> a:go(filePath). 
Got: filePath
ok

7> a:go([1, 2, 3]).
Got: [1,2,3]
ok

8> 

@杜什金,如果你还没有读过的话,那就去读Joe Armstrong写的《编程Erlang(第二版)》
。我是一名Java\C++程序员已经有20多年了。我需要记住Erlang中的变量以大写字母开头,这太难了!!!:)Thanks@dushkin,如果您还没有,请阅读Joe Armstrong的《编写Erlang(第二版)》一书。。我是一名Java\C++程序员已有20多年了。我需要记住Erlang中的变量以大写字母开头,这太难了!!!:)谢谢