All-Caps I/O Program

Explore ports communication more in this lesson to take it to the next level.

Example

In this lesson, we’re going to implement an all-caps I/O program in Elixir and interact with it using ports. The program is going to read lines from the standard input, convert them into uppercase, and then write them to the standard output. In practice, we’ll use ports to interact with software written in all kinds of languages except Elixir itself. Using Elixir here is enough to learn how it all works.

This time, we’ll work with two script files instead of creating a full Elixir project—one to provide the all-caps implementation and the other to run it. Please see the steps written below to successfully execute the program.

Please, ignore the file named program_command.exs for now, we’ll discuss it later in the lesson.

for line <- IO.stream(:stdio, :line) do
  IO.write String.upcase(line)
end
Running the all_caps.io

When we run the program.exs file, the program opens up a port by spawning elixir all_caps.exs and configures it to return binaries. Then, we send messages to the port, using send/2, just as if it were an ...