Playing with the file module (part 1). (Learning Erlang 7)

Published on Oct 01, 2012

Other articles in the Learning Erlang series.

  1. Learning Erlang.
  2. Heads and tails, working with lists. (Learning Erlang 2)
  3. Variables, comparisons and dynamic typing. (Learning Erlang 3)
  4. Modules and functions, Erlang building blocks (Learning Erlang 4).
  5. Module attributes and compilation (Learning Erlang 5).
  6. Playing with recursion, (Learning Erlang 6).
  7. The file module (part 2). (Learning Erlang 8)
  8. String manipulation in Erlang. (Learning Erlang 9)
  9. Records. (Learning Erlang 10)
  10. Functional arrays. (Learning Erlang 11)

At one point or another, while writing code, is inevitable to interact with the file system.

The file module in Erlang contains most of the functions you are used to in other languages for reading, writing, opening, creating and deleting files.

The main surprise for me was to find a number of functions used to evaluate or execute Erlang code contained on files.

Note of caution. These are my notes while learning Erlang. You are welcome to follow along and use them as a guide. Please make sure to check the Erlang language site

I would expect this functions (eval and such) to be in another module, maybe even implemented as BIF. It feels a bit odd and out of place to have code executing functions in a module that deals with files.

Keep in mind that maybe there are other ways to evaluate code in Erlang that I’m not aware yet.

Let’s explore these functions first.

file:consult/1

It takes the name of a file as the sole argument. It reads a list of terms from the file. Each term should be separated by a dot.

For example, let’s assume we have a file with the name sci-fi-shows.txt with this content:


  {show, "Alphas", "sci-fi"}.
  {show, "Dr. Who", "sci-fi"}.
  {show, "Life on Mars", "sci-fi"}.
  {show, "Misfits", "sci-fi"}.
  {show, "Warehouse 13", "sci-fi"}.

When reading it we should get a tuple with the following result.


  1> file:consult("sci-fi-shows.txt").
  {ok,[{show,"Alphas","sci-fi"},
       {show,"Dr. Who","sci-fi"},
       {show,"Life on Mars","sci-fi"},
       {show,"Misfits","sci-fi"},
       {show,"Warehouse 13","sci-fi"}]}

We can assign the result to a tuple as well.


  1> {ok, SciFiShows} = file:consult("sci-fi-shows.txt").
  2> SciFiShows.
  [{show,"Alphas","sci-fi"},
   {show,"Dr. Who","sci-fi"},
   {show,"Life on Mars","sci-fi"},
   {show,"Misfits","sci-fi"},
   {show,"Warehouse 13","sci-fi"}]

If we pass the wrong name we will get an error Tuple.


  1> file:consult("this-is-not-a-file").
  {error,enoent}

It will also return an error if the file has the wrong content, in this case the error usually contains the line number, the error type and some details.


  1> file:consult("wrong-content.txt").
  {error, {1, erl_parse, ["syntax error before: ","is"]}}

file:eval/1 and file:eval/2

These functions evaluate and execute the code in the file but they don’t return the result of the evaluation.

You can use it to dynamically run code, given a file with this code.


  io:format("hello").

We can run it and we will see this.


  1> file:eval("eval-1.txt").
  hellook

file:eval/2 takes a second argument bindings used in the evaluation of the code.

file:script/1 and file:script/2

Similar to the eval functions but returning the result of the last statement in the file.

Given a file with this code.


  Result = 1 + 1.

This is the result.


  1> file:script("script-1.txt").
  {ok,2}

The path functions

The consult/1, eval/1 and script/2 functions have corresponding path functions.


  file:path_consult(["/bin", "/lib", "/configs"], "filename.txt").
  file:path_eval(["/bin", "/lib", "/configs"], "filename.txt").
  file:path_script(["/bin", "/lib", "/configs"], "filename.txt").
  file:path_script(["/bin", "/lib", "/configs"], "filename.txt", Bindings).

They are equivalent to the non path_ functions but they will search for the file in the the list of given paths until it find it.

Next time.

We will continue exploring the file module, looking at the functions that allow us to work with files in a more traditional way.