Unexpected messages from a port driver

Alexey Romanov alexey.v.romanov@REDACTED
Wed Oct 20 15:58:58 CEST 2010

[Duplicating my question on StackOverflow here.]

Erlang-sqlite3 uses a port driver to connect with the SQLite database,
and receives messages from the port in this way

    wait_result(Port) ->
        {Port, Reply} ->
          % io:format("Reply: ~p~n", [Reply]),
        {error, Reason} ->
          io:format("Error: ~p~n", [Reason]),
          {error, Reason};
        _Else ->
          io:format("Else: ~p~n", [_Else]),

According to the manual
(http://www.erlang.org/doc/reference_manual/ports.html), the following
messages can be received from a port:

    {Port,{data,Data}}    Data is received from the external program.
    {Port,closed}         Reply to Port ! {Pid,close}.
    {Port,connected}      Reply to Port ! {Pid,{connect,NewPid}}
    {'EXIT',Port,Reason}  If the port has terminated for some reason.

So, when uncommenting the `io:format` line in `{Port, Reply}` clause,
I should expect to see `{data, ...}` for actual replies. I don't;
instead I see (for `test.erl`)

    Reply: {ok,101}
    Reply: [{columns,["name"]},{rows,[{<<"user">>}]}]
    Reply: [{columns,["sql"]},
            {rows,[{<<"CREATE TABLE user (id INTEGER PRIMARY KEY, name
TEXT, age INTEGER, wage INTEGER)">>}]}]
    Reply: {id,1}
    Reply: {id,2}
    Reply: [{columns,["id","name","age","wage"]},
    Reply: [{columns,["id","name","age","wage"]},{rows,[{1,<<"abby">>,20,2000}]}]
    Reply: [{columns,["id","name","age","wage"]},
    Reply: {ok,101}
    Reply: [{columns,["id","name","age","wage"]},{rows,[{1,<<"abby">>,20,2000}]}]
    Reply: {ok,101}

1. Am I missing something obvious, or is message format given in the
manual wrong?
2. In the second case, what messages actually can be received from the
port? In particular, on error is the message `{'EXIT',Port,Reason}` or
something else?

Yours, Alexey Romanov

More information about the erlang-questions mailing list