简体   繁体   English

Erlang:从控制台使用include?

[英]Erlang: using include from the console?

The include directive is usually used for a .hrl file at the top of an .erl file. include 伪指令通常用于.erl文件顶部的.hrl文件。

But, I would like to use include from the Erlang console directly. 但是,我想直接使用来自Erlang控制台的include。

I am trying to use some functions in a module. 我试图在模块中使用一些函数。 I have compiled the erl file from the console. 我已经从控制台编译了erl文件。 But, the functions I want to use do not work without access to the hrl file. 但是,如果没有访问hrl文件,我想要使用的函数就无法工作。

Any suggestions? 有什么建议?

"But, the functions I want to use do not work without access to the hrl file." “但是,如果没有访问hrl文件,我想要使用的功能就无法运行。”

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. 这不可能是真的,但是从这里我会猜测你想要访问hrl文件中你没有(通常)在shell中的记录。

If you do rr(MODULE) you will load all records defined in MODULE (including those defined in an include file included by MODULE ). 如果你这样做rr(MODULE)你会加载定义的所有记录MODULE (包括那些由包括一个包含文件中定义MODULE )。

Then you can do everything you need to from the shell. 然后,您可以从shell执行所需的一切。

(Another thing you may possibly want for testing is to add the line -compile(export_all) to your erl file. Ugly, but good sometimes for testing.) (您可能想要进行测试的另一件事是将行-compile(export_all)到您的erl文件。丑陋,但有时用于测试。)

Have you tried the compile:file option? 你试过compile:file选项吗? You can pass a list of modules to be included thus: 您可以传递要包含的模块列表:

compile:file("myfile.erl", [{i, "/path/1/"}, {i, "/path/2/"}])

It's worth nothing that jsonerl.hrl doesn't contain any functions. jsonerl.hrl不包含任何函数是没有价值的。 It contains macros. 它包含宏。 As far as I know, macros are a compile-time-only construct in Erlang. 据我所知,宏是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. 使它们可用的最简单方法是自己创建一个.erl文件,它实际上声明了根据宏实现的函数。 Maybe something like this: 也许是这样的:

-module(jsonerl_helpers).
-include("jsonerl.hrl").

record_to_struct_f(RecordName, Record) ->
    ?record_to_struct(RecordName, Record).

... which, after you compile, you could call as: ...编译后,您可以调用:

jsonerl_helpers:record_to_struct_f(RecordName, Record)

I don't know why the author chose to implement those as macros; 我不知道为什么作者选择将它们作为宏来实现; it seems odd, but I'm sure he had his reasons. 这看起来很奇怪,但我确信他有他的理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM