Opening files#
Everything a Perl program does with a file, a pipe, or a buffer goes
through a filehandle. This chapter is a recipe collection for the
open built-in: how to read, write, append, pipe, encode, and recover
from errors — each recipe small enough to copy, each warning pinned
to the line that bites.
The pre-opened handles — STDIN, STDOUT, STDERR, ARGV — are
available from the first line of a program:
print STDERR "debug: entered phase 2\n";
print STDOUT "name? ";
my $name = <STDIN> // die "no input";
while (<ARGV>) { ... }
Every other handle you open yourself. The modern form is always three arguments — handle, mode, target — and the handle is a lexical scalar:
open my $fh, "<", $path or die "open $path: $!";
Read that form until it is muscle memory. Everything else on the following pages is a variation on it.
What this chapter does not cover#
Where to go next#
Reference cross-links#
Every recipe here links into the per-built-in reference when a function’s full behaviour matters: