This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't patch perlopentut: rewrite it completely
[perl5.git] / pod / perlopentut.pod
1 =encoding utf8
2
3 =head1 NAME
4
5 perlopentut - simple recipes for opening files and pipes in Perl
6
7 =head1 DESCRIPTION
8
9 Whenever you do I/O on a file in Perl, you do so through what in Perl is
10 called a B<filehandle>.  A filehandle is an internal name for an external
11 file.  It is the job of the C<open> function to make the association
12 between the internal name and the external name, and it is the job
13 of the C<close> function to break that associations.
14
15 For your convenience, Perl sets up a few special filehandles that are
16 already open when you run.  These include C<STDIN>, C<STDOUT>, C<STDERR>,
17 and C<ARGV>.  Since those are pre-opened, you can use them right away
18 without having to go to the trouble of opening them yourself:
19
20     print STDERR "This is a debugging message.\n";
21
22     print STDOUT "Please enter something: ";
23     $response = <STDIN> // die "how come no input?";
24     print STDOUT "Thank you!\n";
25
26     while (<ARGV>) { ... }
27
28 As you see from those examples, C<STDOUT> and C<STDERR> are output
29 handles, and C<STDIN> and C<ARGV> are input handles.  Those are
30 in all capital letters because they are reserved to Perl, much
31 like the C<@ARGV> array and the C<%ENV> hash are.  Their external
32 associations were set up by your shell.
33
34 For eveyrthing else, you will need to open it on your own. Although there
35 are many other variants, the most common way to call Perl's open() function
36 is with three arguments and one return value:
37
38 C<    I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)>
39
40 Where:
41
42 =over
43
44 =item I<OK>
45
46 will be some defined value if the open succeeds, but
47 C<undef> if it fails;
48
49 =item I<HANDLE>
50
51 should be an undefined scalar variable to be filled in by the
52 C<open> function if it succeeds;
53
54 =item I<MODE>
55
56 is the access mode and the encoding format to open the file with;
57
58 =item I<PATHNAME>
59
60 is the external name of the file you want opened.
61
62 =back
63
64 Most of the complexity of the C<open> function lies in the many
65 possible values that the I<MODE> parameter can take on.
66
67 One last thing before we show you how to open files: opening
68 files does not (usually) automatically lock them in Perl.  See
69 L<perlfaq4> for how to lock.
70
71 =head1 Opening Text Files
72
73 =head2 Opening Text Files for Reading
74
75 If you want to read from a text file, first open it in
76 read-only mode like this:
77
78     my $filename = "/some/path/to/a/textfile/goes/here";
79     my $encoding = ":encoding(UTF-8)";
80     my $handle   = undef;     # this will be filled in on success
81
82     open($handle, "< $encoding", $filename)
83         || die "$0: can't open $filename for reading: $!\n";
84
85 As with the shell, in Perl the C<< "<" >> is used to open the file in
86 read-only mode.  If it succeeds, Perl allocates a brand new filehandle for
87 you and fills in your previously undefined C<$handle> argument with a
88 reference to that handle.
89
90 Now you may use functions like C<readline>, C<read>, C<getc>, and
91 C<sysread> on that handle.  Probably the most common input function
92 is the one that looks like an operator:
93
94     $line = readline($handle);
95     $line = <$handle>;          # same thing
96
97 Because the C<readline> function returns C<undef> at end of file or
98 upon error, you will sometimes see it used this way:
99
100     $line = <$handle>;
101     if (defined $line) {
102         # do something with $line
103     }
104     else {
105         # $line is not valid, so skip it
106     }
107
108 You can also just quickly C<die> on an undefined value this way:
109
110     $line = <$handle> // die "no input found";
111
112 However, if hitting EOF is an expected and normal event, you
113 would not to exit just because you ran out of input.  Instead,
114 you probably just want to exit an input loop.  Immediately
115 afterwards you can then test to see if there was an actual
116 error that caused the loop to terminate, and act accordingly:
117
118     while (<$handle>) {
119         # do something with data in $_
120     }
121     if ($!) {
122         die "unexpected error while reading from $filename: $!";
123     }
124
125 B<A Note on Encodings>: Having to specify the text encoding every time
126 might seem a bit of a bother.  To set up a default encoding for C<open> so
127 that you don't have to supply it each time, you can use the C<open> pragma:
128
129     use open qw< :encoding(UTF-8) >;
130
131 Once you've done that, you can safely omit the encoding part of the
132 open mode:
133
134     open($handle, "<", $filename)
135         || die "$0: can't open $filename for reading: $!\n";
136
137 But never use the bare C<< "<" >> without having set up a default encoding
138 first.  Otherwise, Perl cannot know which of the many, many, many possible
139 flavors of text file you have, and Perl will have no idea how to correctly
140 map the data in your file into actual characters it can work with.  Other
141 common encoding formats including C<"ASCII">, C<"ISO-8859-1">,
142 C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman">, and even C<"UTF-16LE">.
143 See L<perlunitut> for more about encodings.
144
145 =head2 Opening Text Files for Writing
146
147 On the other hand, you want to write to a file, you first have to decide
148 what to do about any existing contents.  You have two basic choices here:
149 to preserve or to clobber.
150
151 If you want to preserve any existing contents, then you want to open the
152 file in append mode.  As in the shell, in Perl you use C<<< ">>" >>> to
153 open an existing file in append mode, and creates the file if it does not
154 already exist.
155
156     my $handle   = undef;
157     my $filename = "/some/path/to/a/textfile/goes/here";
158     my $encoding = ":encoding(UTF-8)";
159
160     open($handle, ">> $encoding", $filename)
161         || die "$0: can't open $filename for appending: $!\n";
162
163 Now you can write to that filehandle using any of C<print>, C<printf>,
164 C<say>, C<write>, or C<syswrite>.
165
166 The file does not have to exist just to open it in append mode.  If the
167 file did not previously exist, then the append-mode open creates it for
168 you.  But if the file does previously exist, its contents are safe from
169 harm because you will be adding your new text past the end of the old text.
170
171 On the other hand, sometimes you want to clobber whatever might already be
172 there.  To empty out a file before you start writing to it, you can open it
173 in write-only mode:
174
175     my $handle   = undef;
176     my $filename = "/some/path/to/a/textfile/goes/here";
177     my $encoding = ":encoding(UTF-8)";
178
179     open($handle, "> $encoding", $filename)
180         || die "$0: can't open $filename in write-open mode: $!\n";
181
182 Here again Perl works just like the shell in that the C<< ">" >> clobbers
183 an existing file.
184
185 As with the append mode, when you open a file in write-only mode,
186 you can now write to that filehandle using any of C<print>, C<printf>,
187 C<say>, C<write>, or C<syswrite>.
188
189 What about read-write mode?  You should probably pretend it doesn't exist,
190 because opening text files in read-write mode is unlikely to do what you
191 would like.  See L<perlfaq4> for details.
192
193 =head1 Opening Binary Files
194
195 If the file to be opened contains binary data instead of text characters,
196 then the C<MODE> argument to C<open> is a little different.  Instead of
197 specifying the encoding, you tell Perl that your data are in raw bytes.
198
199     my $filename = "/some/path/to/a/binary/file/goes/here";
200     my $encoding = ":raw :bytes"
201     my $handle   = undef;     # this will be filled in on success
202
203 And then open as before, choosing C<<< "<" >>>, C<<< ">>" >>>, or
204 C<<< ">" >>> as needed:
205
206     open($handle, "< $encoding", $filename)
207         || die "$0: can't open $filename for reading: $!\n";
208
209     open($handle, ">> $encoding", $filename)
210         || die "$0: can't open $filename for appending: $!\n";
211
212     open($handle, "> $encoding", $filename)
213         || die "$0: can't open $filename in write-open mode: $!\n";
214
215 Alternately, you can change to binary mode on an existing handle this way:
216
217     binmode($handle)    || die "cannot binmode handle";
218
219 This is especially handy for the handles that Perl has already opened for you.
220
221     binmode(STDIN)      || die "cannot binmode STDIN";
222     binmode(STDOUT)     || die "cannot binmode STDOUT";
223
224 You can also pass C<binmode> an explicit encoding to change it on the fly.
225 This isn't exactly "binary" mode, but we still use C<binmode> to do it:
226
227     binmode(STDIN,  ":encoding(MacRoman)")  || die "cannot binmode STDIN";
228     binmode(STDOUT, ":encoding(UTF-8)")     || die "cannot binmode STDOUT";
229
230 Once you have your binary file properly opened in the right mode, you can
231 use all the same Perl I/O functions as you used on text files.  However,
232 you may wish to use the fixed-size C<read> instead of the variable-sized
233 C<readline> for your input.
234
235 Here's an example of how to copy a binary file:
236
237     my $BUFSIZ   = 64 * (2 ** 10);
238     my $name_in  = "/some/input/file";
239     my $name_out = "/some/output/flie";
240
241     my($in_fh, $out_fh, $buffer);
242
243     open($in_fh,  "<", $name_in)   || die "$0: cannot open $name_in for reading: $!";
244     open($out_fh, ">", $name_out)  || die "$0: cannot open $name_out for writing: $!";
245
246     for my $fh ($in_fh, $out_fh)  {
247         binmode($fh)               || die "binmode failed";
248     }
249
250     while (read($in_fh, $buffer, $BUFSIZ)) {
251         unless (print $out_fh $buffer) {
252             die "couldn't write to $name_out: $!";
253         }
254     }
255
256     close($in_fh)       || die "couldn't close $name_in: $!";
257     close($out_fh)      || die "couldn't close $name_out: $!";
258
259 =head1 Opening Pipes
260
261 To be announced.
262
263 =head1 Low-level File Opens via sysopen
264
265 To be announced.  Or deleted.
266
267 =head1 SEE ALSO
268
269 To be announced.
270
271 =head1 AUTHOR and COPYRIGHT
272
273 To be announced.
274
275 =head1 HISTORY
276
277 To be announced.
278
279