简体   繁体   中英

Erlang: using include from the console?

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?

"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 in MODULE (including those defined in an include file included by MODULE ).

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 your erl file. Ugly, but good sometimes for testing.)

Have you tried the compile:file option? 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. 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:

-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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