First, you can narrow down the scope which your math3/1
function accepts:
-module(npower62).
-export([math3/1]).
math3(N) when is_number(N) ->
N2 = N * N,
io_lib:format("square of ~p = ~p", [N, N2]).
Note we have rewritten the function quite a bit. It does not accept a list anymore, but any number, N
only. Also, the format string you handed to io_lib:format/2
was off totally, see man -erl io
.
We can now attack the yaws code:
<erl>
kv(K,L) ->
proplists:get_value(K, L, none).
out(A) ->
L = yaws_api:parse_post(A),
N = kv("number", L),
none =/= N, %% Assert we actually got a value from the kv lookup
%% Insert type mangling of N here so N is converted into an Integer I
%% perhaps I = list_to_integer(N) will do. I don t know what type parse_post/1
%% returns.
{html, npower62:math3(I)}
</erl>
Note that your kv/2
function can be written with a proplists lookup function. In your variant of the code, return from kv/2
was the value {value, {K, V}}
which will never be correct in your version of math3/1
. proplists:get_value/3
returns the V
part only. Also note I hoisted {html, ...} to this level. It is bad style to let npower62 handle it since it should know nothing about the fact it is called from within yaws.
My guess is that you need to call list_to_integer(N). The easiest way to figure this out is to use a call to error_logger:info_report([{v, N}])
and look for the INFO REPORT in the shell or in the log file and see what term N is.
TL;DR: The problem is that your values are not matching all over the place. So you are faced with numerous crashes of the function which yaws probably catches, logs and then survives. This then confuses you to no end.
Also, test your function npower62:math3/1
function from the erl
shell. That way, you would have found it were wrong from the start, reducing your confusion.