English 中文(简体)
yaws and erlang beam files in ebin
原标题:

I am having problems when i have integers and float numbers in my form posts and receive these in my ebin file where i have beam files. Hope someone can help me.

npower.yaws

   <erl>
kv(K,L) ->
{value, {K, V}} = lists:keysearch(K,1,L), V.        
out(A) ->
L = yaws_api:parse_post(A),
N = kv("number", L),
    npower62:math3(N).
    </erl>

npower62.erl compiled to beam file
-module(npower62).
-export([math3/1]).

math3( [N] ) ->
Number = N,
Nsquare = Number * Number,
{html, io_lib:format("square of ~c = ~w", [N, Nsquare])}.

Gives me square of 3 = 2601
instead of
square of 3 = 9
i have tried to use Number = list_to_integer(atom_to_list(N)) (doesnt work)
i have tried to use Number = list_to_float(atom_to_list(N)) (doesnt work)
i have tried to use Number = list_to_integer(N) (doesnt work)

最佳回答

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.

问题回答

暂无回答




相关问题
Comparison among NIO webservers [closed]

We need to put in our architecture a server for streaming contents to (potentially) millions of phones. Here the architects and operations people know only about Java, but I d really to propose ...

yaws and erlang beam files in ebin

I am having problems when i have integers and float numbers in my form posts and receive these in my ebin file where i have beam files. Hope someone can help me. npower.yaws <erl> kv(K,L) -...

How to set up yaws-1.89 in ubuntu?

I need help with setting up yaws-1.89 in ubuntu. This is the error I get: =INFO REPORT==== 3-Dec-2010::16:52:40 === Yaws: Using config file /home/hudson2010/yaws.conf =ERROR REPORT==== 3-Dec-2010::...

Yaws and PUT requests

I just started working with Yaws to try to create some simple RESTful web services, however I ran into an unexpected issue: I can t seem to access my data when I do a PUT request. When I try to use ...

Erlang s maximum number of simultaneous open ports?

Does the erlang TCP/IP library have some limitations? I ve done some searching but can t find any definitive answers. I have set the ERL_MAX_PORTS environment variable to 12000 and configured Yaws to ...

How do I disable debug checks in yaws?

When i start yaws (yaws -i --conf config/yaws.conf) i get this line in the output: Running with debug checks turned on (slower server) It depends on the "-i" option? Or where did i turned on debug ...

Mochiweb : Reading a file as it is uploaded

I want to be able to read a file just as it is being uploaded by the user, i.e. I want to read the incoming stream of bytes from the user s browser.. Is that possible with Mochiweb? If not, where do I ...

热门标签