Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 704
Ted Karmel Avatar asked Sep 17 '10 19:09

Ted Karmel


2 Answers

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

like image 107
Daniel Luna Avatar answered Oct 18 '22 13:10

Daniel Luna


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.

like image 25
Daniel Yankowsky Avatar answered Oct 18 '22 14:10

Daniel Yankowsky