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 filehandle. To open a file for reading, use:

Get hands-on with 1200+ tech skills courses.