[erlang-questions] Programmatic interface to the shell

Joe Armstrong erlang@REDACTED
Mon Aug 10 20:11:00 CEST 2015


On Mon, Aug 10, 2015 at 7:14 PM, Fred Hebert <mononcqc@REDACTED> wrote:
> On 08/10, Joe Armstrong wrote:
>>
>> Do you have any idea how to dig out the error message so it's exactly the
>> same?
>>
>> I'm asking because I'm writing a tutorial in a "markdown like" language
>> and I want to automate production of examples :-)
>>
>
> Probably would need to gather the Erlang stacktrace and raise it there too
> with erlang:get_stacktrace() and erlang:raise(T,R,Stacktrace) so that the
> shell evaluators  (with lib:format_stacktrace) can handle it right:
>
>    Eval = fun EvalLoop(Bindings) ->
>        receive
>            {cmd, Caller, Ref, String} ->
>                try
>                    {ok, Tokens, _} = erl_scan:string(String),
>                    %% many forms can be comma-separated
>                    {ok, Forms} = erl_parse:parse_exprs(Tokens),
>                    %% eval individually
>                    {value, Val, NewBindings} = erl_eval:exprs(Forms,
> Bindings),
>                    Caller ! {ok, Ref, Val},
>                    EvalLoop(NewBindings)
>                catch
>                    T:R ->
>                        Caller ! {raise, Ref, T, R, erlang:get_stacktrace},
>                        EvalLoop(Bindings)
>                end
>        end end.
>
>    Send = fun(Pid, String) ->
>        Ref = erlang:monitor(process, Pid),
>        Pid ! {cmd, self(), Ref, String},
>        receive
>            {ok, Ref, Value} -> Value;
>            {raise, Ref, T, R, S} -> erlang:raise(T,R,S)
>        end end.
>
> This should do it.

erlang:raise seems to print a string (as a side effect) and then kill the
process. This is not really what I want. I also found that the code
you posted above behaves differently when compiled and when run
in the shell.

So some code takes the exception and turns it into a string
which the shell prints - I've no idea where this happens - investigation
proceeds ...

/Joe



More information about the erlang-questions mailing list