Erlang 在没有提示的情况下从 StdIn 读取

Erlang read from StdIn without prompt

刚看了Erlang的IO模块,所有的输入函数都是以prompt()开头的。

我有一个程序 A,它通过管道将其输出到我的 Erlang 程序 B,因此将 A 的 stdout 转换为 B 的 stdin

我怎样才能循环读取 stdIn, 因为我每 Xms 都会收到一条消息。

我想要的是这样的

loop()->
  NewMsg = readStdIn() %% thats the function I am looking for
  do_something(NewMsg),
  loop.

I just read Erlang's IO module, all the input functions start with a prompt().

看来您可以使用 "" 作为提示。从标准输入读取面向行的输入:

-module(my).
-compile(export_all).

read_stdin() ->
    case io:get_line("") of
        eof ->
            init:stop(); 
        Line ->
            io:format("Read from stdin: ~s", [Line]),
            read_stdin()
    end.

在 bash shell:

~/erlang_programs$ erl -compile my.erl
my.erl:2: Warning: export_all flag enabled - all functions will be exported

~/erlang_programs$ echo -e "hello\nworld" | erl -noshell -s my read_stdin
Read from stdin: hello
Read from stdin: world
~/erlang_programs$ 

Erlang How Do I...write a unix pipe program in Erlang?