views:

73

answers:

3

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?

+2  A: 

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/"}])
Manoj Govindan
what does the first element of the tuple in the list represent .. i ? I presume the second element could represent the path of the .hrl file...
Ted Karmel
The atom `i`, indicating that the second element is to be included.
Manoj Govindan
thanks manoj... this probably works. but for my use case, it doesn't seem to. i am try to run this module from the erlang console http://github.com/lambder/jsonerl
Ted Karmel
Can you post sample code? A small snippet that uses the hrl and I'll try to see if I can make it work here.
Manoj Govindan
i do a clone of the project (with sudo git clone) then in the folder start erl as root. then in first line I do( compile:file("jsonerl.erl", [{i, "jsonerl.hrl"}]. I get ok message. Then I do rd(artist, artist, {name, year_of_birth, city, photo, movies}). This creates the record and works. I create the variable Artist as in the docs of the project. Now I do a variable: Json = jsonerl:record_to_json(artist, Artist). That's when I get an error.
Ted Karmel
@Ted: `jsonerl.erl` does not define `record_to_json`. Naturally it will give an error.
Manoj Govindan
@Manoj: Yes, you're absolutely right. Hence, the effort to get jsonerl.hrl (which defines record_to_json) compiled and accessible...
Ted Karmel
+1  A: 

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

Daniel Luna
A: 

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.

Daniel Yankowsky