Erlang:使用控制台中的包含?
include 指令 通常用于 .hrl 文件顶部的 .hrl 文件。 erl 文件。
但是,我想直接使用 Erlang 控制台中的 include 。
我正在尝试使用模块中的一些功能。我已经从控制台编译了 erl 文件。但是,如果不访问 hrl 文件,我想要使用的功能就无法工作。
有什么建议吗?
The include directive is usually used for a .hrl file at the top of an .erl file.
But, I would like to use include from the Erlang console directly.
I am trying to use some functions in a module. I have compiled the erl file from the console. But, the functions I want to use do not work without access to the hrl file.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“但是,如果不访问 hrl 文件,我想要使用的功能就无法工作。”
这不可能是真的,但从这里我将尝试猜测您想要访问
hrl
文件中的记录,而您(通常)在 shell 中没有这些记录。如果执行
rr(MODULE)
,您将加载MODULE
中定义的所有记录(包括MODULE
包含的包含文件中定义的记录)。然后您就可以从 shell 执行您需要执行的所有操作。
(您可能需要进行测试的另一件事是将行
-compile(export_all)
添加到您的erl
文件中。虽然丑陋,但有时对于测试来说很好。)"But, the functions I want to use do not work without access to the hrl file."
This can't be true, but from this I'll take a shot at guessing that you want access to records in the
hrl
file that you don't (normally) have in the shell.If you do
rr(MODULE)
you will load all records defined inMODULE
(including those defined in an include file included byMODULE
).Then you can do everything you need to from the shell.
(Another thing you may possibly want for testing is to add the line
-compile(export_all)
to yourerl
file. Ugly, but good sometimes for testing.)您是否尝试过
compile:file
选项?您可以传递要包含的模块列表,如下所示:Have you tried the
compile:file
option? You can pass a list of modules to be included thus:jsonerl.hrl 不包含任何函数是毫无价值的。它包含宏。据我所知,宏是 Erlang 中仅编译时的构造。
使它们可用的最简单方法是自己创建一个 .erl 文件,该文件实际上声明了根据宏实现的函数。也许是这样的:
... 编译后,您可以调用为:
我不知道为什么作者选择将它们实现为宏;这看起来很奇怪,但我确信他有他的理由。
It's worth nothing that jsonerl.hrl doesn't contain any functions. It contains macros. As far as I know, macros are a compile-time-only construct in Erlang.
The easiest way to make them available would be to create a .erl file yourself that actually declares functions that are implemented in terms of the macro. Maybe something like this:
... which, after you compile, you could call as:
I don't know why the author chose to implement those as macros; it seems odd, but I'm sure he had his reasons.