10

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?

Ted Karmel
  • 1,046
  • 1
  • 12
  • 20
  • How do you really mean? The `-include` directive is a compiletime feature. If you have successfully compiled the file then any functions in it which need "things" from the include file will have them, the compiler checks this. So after compilation the functions will work without access to the include file. What is it that doesn't work? Which access is that you are lacking? – rvirding Sep 18 '10 at 14:01
  • I am looking to install jsonerl which has an hrl file( http://github.com/lambder/jsonerl ). Instead of writing a file and then compiling it, I would like to use the functions in the hrl fiel directly from the shell. – Ted Karmel Sep 19 '10 at 19:41

3 Answers3

12

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

juan.facorro
  • 9,791
  • 2
  • 33
  • 41
Daniel Luna
  • 1,969
  • 14
  • 14
5

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
  • 72,339
  • 21
  • 134
  • 141
  • 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 Sep 17 '10 at 19:24
  • The atom `i`, indicating that the second element is to be included. – Manoj Govindan Sep 17 '10 at 19:26
  • 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 Sep 17 '10 at 19:34
  • 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 Sep 17 '10 at 19:36
  • 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 Sep 17 '10 at 19:54
  • @Ted: `jsonerl.erl` does not define `record_to_json`. Naturally it will give an error. – Manoj Govindan Sep 17 '10 at 20:21
  • @Manoj: Yes, you're absolutely right. Hence, the effort to get jsonerl.hrl (which defines record_to_json) compiled and accessible... – Ted Karmel Sep 18 '10 at 01:46
3

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
  • 6,956
  • 1
  • 35
  • 39