Search⌘ K
AI Features

Opening Files

Explore how to open and manipulate files in Perl using filehandles, understand different file modes for reading, writing, and appending, and learn about safer practices with three-argument open. Discover how to handle Unicode encodings and the special DATA filehandle to efficiently manage real-world input and output.

Most programs interact with the real world mostly by reading, writing, and otherwise manipulating files. Perl began as a tool for system administrators and is still a language well suited for text processing.

Input and output

A filehandle represents the current state of one specific channel of input or output. Every Perl program starts with three standard filehandles:

  • STDIN (the input to the program).
  • STDOUT (the output from the program).
  • STDERR (the error output from the program).

By default, everything we print or say goes to STDOUT, while errors and warnings go to STDERR. This separation of output allows us to redirect useful output and errors to two different places—an output file and error logs, for example.

Initialize a filehandle

Use the open built-in to initialize a ...