How to pipe input from a file as stdin to an erlang program running in the shell as well as standalone?
I have a file hr.erl
and I compile it from the shell. There is a function in it which accepts input from stdin using io:fread()
. I have written a case expression with guards where if it matches {ok, [0]}
it should terminate. Instead of 0
, I actually need it to be eof
.
- How to send eof when running in a shell?
- I have a file
inp.txt
with values1
2
3
and0
on each line. How can I pass it using the<
pipe operator? Is there anything likeerl -hr <inp.txt
? Can I pipe it to stdin within the shell.
Here is my program so far (updated to include eof
).
-module(hr).
-export([main/0]).
r(L) ->
case io:fread("", "~d") of
eof ->
io:format("eof~n", []),
ok;
{ok, [0]} ->
io:format("terminate~n", []),
lists:reverse(L);
{ok, [N]} ->
io:format("inp ~p~n", [N]),
r([N|L])
end.
main() -> r([]).
From shell
1> c(hr).
{ok,hr}
2> hr:main().
1
inp 1
2
inp 2
3
inp 3
0
terminate
[1,2,3]
Thanks.
I am able to pipe input when using escript. Write the erlang program without module or export info, with
main(_)
function, i.e., inescript
compatible way. Then we can pipe input usingcat
likeThis works and program terminates when it encounters
eof
. But I still don't know why it's not working when using redirect operator<
.Look here for the fastest line oriented IO for Erlang known to me. Note the usage of
-noshell
and-noinput
command line parameters. Key part isEDIT: Note that the line oriented IO was fixed in R16B http://erlang.org/pipermail/erlang-questions/2013-February/072531.html so you need this trick no longer.
EDIT2: There is an answer using fixed
file:read_line/1
.This is one of the Erlang FAQs:
http://www.erlang.org/faq/how_do_i.html#id49435