Elixir- read from stdin

Hi everybody!

I have a problem trying to solve a challenge in Elixir.

I decided to solve my challenge using Elixir because I would like to learn something new while I’m facing these challenges. So… here’s my situation:

At my first attempt at MR some exercise I was reading the data directly from the file using the command File.read(“DATA.lst”). my merge request was close and I received a message that says “read data from stdin”. Ok… I’m a rookie in programming so I needed to google what does it means and I found that stdin is short for standard input , stdin is an input stream where data is sent to and read by a program and the way to use it in Elixir its the following:

  • change File.read(“DATA.lst”) for IO.read(:stdio, :line)
  • In terminal type cat(type) DATA.lst | elixir program.exs

Ok, here is where I need you to tell me if I’m wrong. but I will continue with other things that I saw. This way of executing an Elixir file is named running elixir file as a script and this is what it does:

  1. A BEAM instance is started
  2. The file prueba.exs is loaded and compiled in memory. The interpreted symbols are loaded into the VM.
  3. The code after the module, (i.e. Prueba.main) is interpreted and run.
  4. After completing the execution, the BEAM instance is shutdown.

You can read this on this page: https://thinkingelixir.com/2019-04-running-an-elixir-file-as-a-script/

So, to expose my problem(without show my solution jeje) I made the following program:


A simple hello world program that takes from stdin a string from a file to print a message, this is what I type in the terminal and the output:


So… It works!!! :star_struck: :partying_face: :partying_face:. I try the same with my program and this was the output:

everything was so happy at this point. uwu

Ok I push to my branch my solution and the pipeline crush :sob: :sob: :sob: and this was the error message:

I look at the message and I saw something odd, the function String.split is receiving :eof and, as it says in this page: https://hexdocs.pm/elixir/IO.html, :oef is returned when an empty string is passed from stdin(or well… that’s what I understood).

The error from the pipeline says that it was a compilation error, the same error I get when I use the IEX terminal and type the following command to compile:

  • iex(1) > c(“rechavar.exs”)

So, i think that maybe, when it compiles, it reads the last line (Prueba.main in my example) and execute the program. that’s why I try to push it without the final line. And it works!!! but when I try to execute it in the terminal that’s what happens:


At this point I really don’t know what to do… I solved the message that i received in the MR yesterday like in 20 min, but this problem doesn’t let me continue.

Hope you are having such a great day!

1 Like

I exceeded the maximum of characters so I continue:
I don’t know if I should push this without the final line even if it does execute the program and if I push this with the final line, the pipeline will give me an error… So… am i wrong? what should I do?

I’m just here to say that I loved your storytelling. :smile:

1 Like

jejej thanks!!! write this help me to let my feelings flow :disappointed_relieved:

1 Like

I think the issue you’re facing during pipeline time is not having access to the stdin data your program uses to work.

From what I understood from your post, for some reason, the data obtained from stdin is still necessary when compiling. This is what should not be happening. Your program should compile properly without needing any input data. It should only ask for it once compiled and in runtime.

Do some research first, in case this is definitely not possible with Elixir (It really should be possible), you can go ahead and remove that last line.

I found a solution!!!
I can remove the last line and, in terminal, change cat DATA.lst | elixir program.exs for cat DATA.lst | elixir -r program.exs -e Prueba.main. AND IT RUUUUUNS!!!

1 Like