Erlang - Preprocessors



Before an Erlang module is compiled, it is automatically processed by the Erlang Preprocessor. The preprocessor expands any macros that might be in the source file and inserts any necessary include files.

Ordinarily, you won’t need to look at the output of the preprocessor, but in exceptional circumstances (for example, when debugging a faulty macro), you might want to save the output of the preprocessor. To see the result of preprocessing the module some_module.erl give the OS shell command.

erlc -P some_module.erl

For example, suppose if we had the following code file −

Example

-module(helloworld). 
-export([start/0]). 
-include("user.hrl"). 

start() -> 
   io:fwrite("~w",[?macro1(1,2)]).

And if we executed the following command from the command line −

erlc –P helloworld.erl

A file called helloworld.P would be generated. If you open this file, you would find the following contents which is what the preprocessor would compile.

-file("helloworld.erl", 1). -module(helloworld).

-export([start/0]).
-file("user.hrl", 1).
-file("helloworld.erl", 3).

start() ->
   io:fwrite("~w", [{1 + 2}]).
Advertisements