Search⌘ K
AI Features

All-Caps I/O Program

Explore how to implement an all-caps I/O program in Elixir that reads input and outputs uppercase text by interacting with external code through ports. Understand how to use Port.open with various options, send and receive messages synchronously, and handle message boundaries using packet protocols to ensure reliable communication with programs in other languages.

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