Configuration 如何定义配置文件变量?
我有一个配置文件,其中包含:Configuration 如何定义配置文件变量?,configuration,erlang,otp,Configuration,Erlang,Otp,我有一个配置文件,其中包含: {path, "/mnt/test/"}. {name, "Joe"}. 用户可以更改路径和名称。正如我所知,有一种方法可以通过使用中的file:consult/1将这些变量保存在模块中 -define(VARIABLE, <parsing of the config file>). -定义(变量,)。 当模块开始工作而不在-define中创建解析函数时,有没有更好的方法来读取配置文件?(据我所知,根据Erlang开发人员的说法,在-define中
{path, "/mnt/test/"}.
{name, "Joe"}.
用户可以更改路径和名称。正如我所知,有一种方法可以通过使用中的file:consult/1
将这些变量保存在模块中
-define(VARIABLE, <parsing of the config file>).
-定义(变量,)。
当模块开始工作而不在-define中创建解析函数时,有没有更好的方法来读取配置文件?(据我所知,根据Erlang开发人员的说法,在-define中创建复杂函数并不是最好的方法)如果您只需要在启动应用程序时存储配置,则可以使用“rebar.config”中定义的应用程序配置文件
{profiles, [
{local,
[{relx, [
{dev_mode, false},
{include_erts, true},
{include_src, false},
{vm_args, "config/local/vm.args"}]
{sys_config, "config/local/yourapplication.config"}]
}]
}
]}.
更多信息请点击此处:
创建yourapplication.config
的下一步-将其存储在应用程序文件夹/app/config/local/yourapplication.config
此配置应具有与此示例类似的结构
[
{
yourapplicationname, [
{path, "/mnt/test/"},
{name, "Joe"}
]
}
].
因此,当您的应用程序启动时
您可以使用
{ok, "/mnt/test/"} = application:get_env(yourapplicationname, path)
{ok, "Joe"} = application:get_env(yourapplicationname, name)
现在,您可以定义如下变量:
-define(VARIABLE,
case application:get_env(yourapplicationname, path) of
{ok, Data} -> Data
_ -> undefined
end
).
谢谢你的详细回答!另一个问题是如何使这两个变量成为模块的全局变量?据我所知,唯一的方法是通过应用程序声明变量:get_env in“-define”,对吗?P.S.无ets/dets/mnesiausage@NickSaw更新了答案(添加-定义变量)谢谢你的回复,我明白了