Search code examples
ioerlangstdoutstdin

Erlang read from StdIn without prompt


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

I have a program A which pipes it's output to my Erlang program B, therefore making A's stdout to B's stdin.

How can I just read that stdIn in a loop, since I get a msg every Xms.

what I want is something like this

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

Solution

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

    It looks like you can use "" for the prompt. Reading line oriented input from stdin:

    -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.
    

    In a 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$ 
    

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