External function call Erlang - erlang

I'm trying to call a function (from an external module) in erlang. both beam files are located in the same directory.
-module(drop2).
-export([fall_velocity/1]).
fall_velocity(Distance) -> math:sqrt(2 * 9.8 * Distance).
Then I'm calling
-module(ask).
-export([term/0]).
term() ->
Input = io:read("Enter {x,distance} ? >>"),
Term = element(2,Input),
drop2:fall_velocity(Term).
it gives the following error.I tested individual modules for errors. it is compiling with out any errors or warning.
Eshell V5.10.2 (abort with ^G)
1> ask:term().
Enter {x,distance} ? >>{test,10}.
** exception error: an error occurred when evaluating an arithmetic expression
in function drop2:fall_velocity/1 (drop2.erl, line 3)
Not sure why it is throwing arithmetic expression error .

You could read the documentation to figure out that the result is {ok, Term}. You could try the io:read/1 function in the console, then you'd see following:
1> io:read("Enter > ").
Enter > {test, 42}.
{ok,{test,42}}
2>
That means that you need to deconstruct the result of io:read/1 differently, for example like this:
-module(ask).
-export([term/0]).
term() ->
{ok, {_, Distance}} = io:read("Enter {x, distance} > "),
drop2:fall_velocity(Distance).

Related

Erlang exited with reason - undef (with string:find/2 func)

trying to use function string:find/2but every time getting the error
CRASH REPORT Process <0.779.0> with 0 neighbours exited with reason: {{undef,[{string,find,[[<<208,162,51,32,208,190,208,177,209,137,46,44,32,84,51,32,116,111,116,97,108,44>>],[<<208,186,209,128,208,190,208,178>>]],[]},{proxy_layer_cli_handle_req,do_execute_keysearch,4,[{file,\"/opt/proxy_layer/_build/test/lib/proxy_layer/src/proxy_layer_cli_handle_req.erl\"},{line,222}]},{proxy_layer_cli_handle_req,keysearch,3,[{file,\"/opt/proxy_layer/_build/test/lib/proxy_layer/src/proxy_layer_cli_handle_req.erl\"},{line,...}]},...]},...}
when i use it in terminal - everything is okay
1> string:find(<<208,162,51,32,208,190,208,177,209,137,46,44,32,84,51,32,116,111,116,97,108,44>>,<<208,186,209,128,208,190,208,178>>).
1> nomatch
I'm using Erlang 20.1
here is the code which I use:
do_execute_keysearch([First|Rest], PriceList, Keyword, Acc) ->
Id = utils:get_value(<<"Id">>, First),
case utils:get_value(<<"Keywords">>, First) of
<<>> -> do_execute_keysearch(Rest, PriceList, Keyword, Acc);
undefined -> do_execute_keysearch(Rest, PriceList, Keyword, Acc);
Keys ->
case string:find(Keys, Keyword) of
nomatch ->
do_execute_keysearch(Rest, PriceList, Keyword, Acc);
_ ->
Price = find_price_by_service_id(PriceList, Id),
NewAcc = [lists:append(Price, First) | Acc],
do_execute_keysearch(Rest, PriceList, Keyword, NewAcc)
end
end;
UPDATE:
Issue fixed after changing Erlang version in docker container. (Changed to Erlang 20.1)
Don’t know why there are some modules undefined in Erlang 19
So the problem solved now
string:find/2 was added to Erlang in version 20 which is why you were getting an undef error in Erlang 19. The solution is to upgrade to Erlang 20 (which you've already done).
Look at the error more carefully. Or rather, try to reproduce it. Which of these looks more like your error?
1> catch lists:nonexist(<<1>>, <<2>>).
{'EXIT',{undef,[{lists,nonexist,[<<1>>,<<2>>],[]},
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
{erl_eval,expr,5,[{file,"erl_eval.erl"},{line,431}]},
{shell,exprs,7,[{file,"shell.erl"},{line,687}]},
{shell,eval_exprs,7,[{file,"shell.erl"},{line,642}]},
{shell,eval_loop,3,[{file,"shell.erl"},{line,627}]}]}}
2> catch lists:nonexist([<<1>>], [<<2>>]).
{'EXIT',{undef,[{lists,nonexist,[[<<1>>],[<<2>>]],[]},
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
{erl_eval,expr,5,[{file,"erl_eval.erl"},{line,431}]},
{shell,exprs,7,[{file,"shell.erl"},{line,687}]},
{shell,eval_exprs,7,[{file,"shell.erl"},{line,642}]},
{shell,eval_loop,3,[{file,"shell.erl"},{line,627}]}]}}

Erlang: misunderstanding of an error that is connected with ranch

I am trying to make this application FIX protocol
I start the Erlang shell using erl -pa ./ebin -pa ebin ./deps/*/ebin.
And run the application like this: application:start(fix).
After that I invoke "start_listener" function like this: fix:start_listener().
As a result this mistake appears:
exception error: no match of right hand side value
{error,
{{shutdown,
{failed_to_start_child,ranch_acceptors_sup,
badarg}},
{child,undefined,
{ranch_listener_sup,fix_listener},
{ranch_listener_sup,start_link,
[fix_listener,10,ranch_tcp,
[{port,[8501]}],
fix_server,[]]},
permanent,5000,supervisor,
[ranch_listener_sup]}}}
in function fix:start_listener/0 (src/fix.erl, line 21)
What does it all mean? And how to fix this mistake?
My code is:
`-module(fix).
-author('Max Lapshin <max#maxidoors.ru>').
-include("log.hrl").
% -include("../include/admin.hrl").
-include("../include/business.hrl").
-compile(export_all).
%% #doc Start acceptor with `ranch' on port, specified in application environment under fix_port%%
-spec start_listener() -> {ok, pid()}.
start_listener() ->
application:start(ranch),
Spec = ranch:child_spec(fix_listener, 10,
ranch_tcp, [{port, fix:get_value(fix_port)}],
fix_server, []
),
{ok, Pid} = supervisor:start_child(fix_sup, Spec),
error_logger:info_msg("Starting FIX server on port ~p~n",[fix:get_value(fix_port)]),
{ok, Pid}.
`
This is a piece of code that shows a mistake.
This is incorrect:
[{port,[8501]}]
Port value must be an integer.
Your fix:get_value function returns list [8501] instead 8501 and you get this badarg error.

why this code get different result,input with different order in shell?

consider the code bleow:
-module(add_two).
-compile(export_all).
start()->
process_flag(trap_exit, true),
Pid = spawn_link(add_two, loop, []),
register(add_two, Pid),
ok.
request()->
add_two ! bad,
receive
{'EXIT', _Pid, Reason} -> io:format("self:~w~n", [{error, Reason}]);
{Result} -> io:format("result:~w~n", [Result])
after
1000->timeout
end.
loop()->
receive
bad -> exit(nogoodreason_bad);
{request, Pid, Msg} -> Pid ! {result, Msg + 2}
end,
loop().
when I test the code above in shell,I get two different results with different input order,but why?
first input order :
Eshell V5.9.1 (abort with ^G)
1> add_two:request(ddd).
** exception error: undefined function add_two:request/1
2> add_two:start().
ok
3> add_two:request().
self:{error,nogoodreason_bad}
ok
second input order:
Eshell V5.9.1 (abort with ^G)
1> add_two:start().
ok
2> add_two:request(ddd).
** exception error: undefined function add_two:request/1
3> add_two:request().
** exception error: bad argument
in function add_two:request/0 (add_two.erl, line 11)
The call add_two:request(ddd) makes the shell process die, taking add_two down with it, due to the spawn_link() call. You can confirm this by checking the shell's pid before and after an exception. It doesn't even have to involve the add_two module:
12> self().
<0.61.0>
13> 2+2.
4
14> self().
<0.61.0>
15> 1/0.
** exception error: bad argument in an arithmetic expression
in operator '/'/2
called as 1 / 0
16> self().
<0.69.0>
17>
You can avoid this effect either by calling spawn instead of spawn_link, or trapping and handling exits in the spawned process.

Erlang rr() returns missing_chunk

Newbie question on erlang using by wings 3d shell (windows 7 pro, wings 3d 1.4.1). When I write command to read records definitons:
rr(wings).
I always get an error:
{error,beam_lib,
{missing_chunk,'d:/temp/erlang/lib/wings/ebin/wings.beam',"Abst"}}
What I am doing wrong?
rr/1 "reads record definitions from a module's BEAM file. If there are no record definitions in the BEAM file, the source file is located and read instead."
My guess is that the abstract form hasn't been included in the .BEAM file and that in your installation the source files are not available.
UPDATE: Digging into the shell:read_file_records/2 function, I've found the following:
read_file_records(File, Opts) ->
case filename:extension(File) of
".beam" ->
case beam_lib:chunks(File, [abstract_code,"CInf"]) of
{ok,{_Mod,[{abstract_code,{Version,Forms}},{"CInf",CB}]}} ->
case record_attrs(Forms) of
[] when Version =:= raw_abstract_v1 ->
[];
[] ->
%% If the version is raw_X, then this test
%% is unnecessary.
try_source(File, CB);
Records ->
Records
end;
{ok,{_Mod,[{abstract_code,no_abstract_code},{"CInf",CB}]}} ->
try_source(File, CB);
Error ->
%% Could be that the "Abst" chunk is missing (pre R6).
Error
end;
_ ->
parse_file(File, Opts)
end.
It looks like, if the "Abst" chunk is missing, it doesn't even try to read the source code. What does beam_lib:chunks(File, [abstract_code,"CInf"]) return for you? Which Erlang version are you running?
I am using:
Erlang R14B01 (erts-5.8.2) [rq:1] [async-threads:0]
Eshell V5.8.2 (abort with ^G)
Calling this works fine:
rr("d:/temp/erlang/src/wings.hrl").
Calling:
beam_lib:chunks("wings", [abstract_code,"CInf"]).
returns:
{error,beam_lib,{file_error,"wings.beam",enoent}}
rr/1 requires the actual file name or the relative PATH to the file name. Like this:
rr("wings.hrl").
OR
rr("./apps/MYAPP-1.0/include/my_include_file.hrl").
OR
rr("./src/wings.erl").
In other words, pass it the actual relative PATH from your pwd() to the file which contains the definitions.

Erlang runtime error

I'm working on an erlang program and getting a strange runtime error. Any idea why? Thanks!
The errors are (after compiling the program successfully):
8> PID = spawn(planner,start,[]).
** exception error: no match of right hand side value <0.65.0>
9>
This is the program:
-module(planner).
-export([start/0]).
start() ->
loop([],[]).
loop(ContactsList,EventsList) ->
receive
{contact, Last, First, Number} ->
loop([{Last,First,Number}|ContactsList],EventsList);
{event, Date, Time, What} ->
loop([{Date,Time,What}|ContactsList],EventsList);
print_contacts ->
NewList=lists:sort(ContactsList),
lists:foreach(fun(Elem)->io:format("~p~n", [Elem]) end, NewList),
loop(ContactsList,EventsList);
print_events ->
NewList=lists:sort(EventsList),
lists:foreach(fun(Elem)->io:format("~p~n", [Elem]) end, NewList),
loop(ContactsList,EventsList);
exit ->
io:format("Exiting.~n");
_ ->
io:format("Dude, I don't even know what you're talking about.~n"),
loop(ContactsList,EventsList)
end.
The variable PID is probably set to something else but <0.65.0> from an earlier line you entered in the shell:
5> PID = spawn(...).
<0.42.0>
8> PID = spawn(...).
** exception error: no match of right hand side value <0.65.0>
This effectively makes the line which generates the error something like
8> <0.42.0> = <0.65.0>.
which will result in a "no match" error.
More obvious illustration of the issue:
1> X = 1.
1
2> X = 2.
** exception error: no match of right hand side value 2
As to solving your issue: You probably want to run f(PID) to have the shell forget just the PID variable, or even f() to have the shell forget ALL variables.

Resources