Commit | Line | Data |
---|---|---|
f8284313 TC |
1 | =head1 NAME |
2 | ||
3 | perlopentut - tutorial on opening things in Perl | |
4 | ||
5 | =head1 DESCRIPTION | |
6 | ||
7 | Perl has two simple, built-in ways to open files: the shell way for | |
8 | convenience, and the C way for precision. The choice is yours. | |
9 | ||
10 | =head1 Open E<agrave> la shell | |
11 | ||
12 | Perl's C<open> function was designed to mimic the way command-line | |
13 | redirection in the shell works. Here are some basic examples | |
14 | from the shell: | |
15 | ||
16 | $ myprogram file1 file2 file3 | |
17 | $ myprogram < inputfile | |
18 | $ myprogram > outputfile | |
19 | $ myprogram >> outputfile | |
20 | $ myprogram | otherprogram | |
21 | $ otherprogram | myprogram | |
22 | ||
23 | And here are some more advanced examples: | |
24 | ||
25 | $ otherprogram | myprogram f1 - f2 | |
26 | $ otherprogram 2>&1 | myprogram - | |
27 | $ myprogram <&3 | |
28 | $ myprogram >&4 | |
29 | ||
30 | Programmers accustomed to constructs like those above can take comfort | |
31 | in learning that Perl directly supports these familiar constructs using | |
32 | virtually the same syntax as the shell. | |
33 | ||
34 | =head2 Simple Opens | |
35 | ||
36 | The C<open> function takes two arguments: the first is a filehandle, | |
37 | and the second is a single string comprising both what to open and how | |
38 | to open it. C<open> returns true when it works, and when it fails, | |
39 | returns a false value and sets the special variable $! to reflect | |
40 | the system error. If the filehandle was previously opened, it will | |
41 | be implicitly closed first. | |
42 | ||
43 | For example: | |
44 | ||
45 | open(INFO, "datafile") || die("can't open datafile: $!"); | |
46 | open(INFO, "< datafile") || die("can't open datafile: $!"); | |
47 | open(RESULTS,"> runstats") || die("can't open runstats: $!"); | |
48 | open(LOG, ">> logfile ") || die("can't open logfile: $!"); | |
49 | ||
50 | If you prefer the low-punctuation version, you could write that this way: | |
51 | ||
52 | open INFO, "< datafile" or die "can't open datafile: $!"; | |
53 | open RESULTS,"> runstats" or die "can't open runstats: $!"; | |
54 | open LOG, ">> logfile " or die "can't open logfile: $!"; | |
55 | ||
56 | A few things to notice. First, the leading less-than is optional. | |
57 | If omitted, Perl assumes that you want to open the file for reading. | |
58 | ||
59 | The other important thing to notice is that, just as in the shell, | |
60 | any white space before or after the filename is ignored. This is good, | |
61 | because you wouldn't want these to do different things: | |
62 | ||
63 | open INFO, "<datafile" | |
64 | open INFO, "< datafile" | |
65 | open INFO, "< datafile" | |
66 | ||
67 | Ignoring surround whitespace also helps for when you read a filename in | |
68 | from a different file, and forget to trim it before opening: | |
69 | ||
70 | $filename = <INFO>; # oops, \n still there | |
71 | open(EXTRA, "< $filename") || die "can't open $filename: $!"; | |
72 | ||
73 | This is not a bug, but a feature. Because C<open> mimics the shell in | |
74 | its style of using redirection arrows to specify how to open the file, it | |
75 | also does so with respect to extra white space around the filename itself | |
13a2d996 SP |
76 | as well. For accessing files with naughty names, see |
77 | L<"Dispelling the Dweomer">. | |
f8284313 TC |
78 | |
79 | =head2 Pipe Opens | |
80 | ||
81 | In C, when you want to open a file using the standard I/O library, | |
82 | you use the C<fopen> function, but when opening a pipe, you use the | |
83 | C<popen> function. But in the shell, you just use a different redirection | |
84 | character. That's also the case for Perl. The C<open> call | |
85 | remains the same--just its argument differs. | |
86 | ||
f5daac4a | 87 | If the leading character is a pipe symbol, C<open> starts up a new |
f8284313 TC |
88 | command and open a write-only filehandle leading into that command. |
89 | This lets you write into that handle and have what you write show up on | |
90 | that command's standard input. For example: | |
91 | ||
92 | open(PRINTER, "| lpr -Plp1") || die "cannot fork: $!"; | |
93 | print PRINTER "stuff\n"; | |
94 | close(PRINTER) || die "can't close lpr: $!"; | |
95 | ||
96 | If the trailing character is a pipe, you start up a new command and open a | |
97 | read-only filehandle leading out of that command. This lets whatever that | |
98 | command writes to its standard output show up on your handle for reading. | |
99 | For example: | |
100 | ||
101 | open(NET, "netstat -i -n |") || die "cannot fork: $!"; | |
102 | while (<NET>) { } # do something with input | |
103 | close(NET) || die "can't close netstat: $!"; | |
104 | ||
105 | What happens if you try to open a pipe to or from a non-existent command? | |
106 | In most systems, such an C<open> will not return an error. That's | |
107 | because in the traditional C<fork>/C<exec> model, running the other | |
108 | program happens only in the forked child process, which means that | |
109 | the failed C<exec> can't be reflected in the return value of C<open>. | |
13a2d996 SP |
110 | Only a failed C<fork> shows up there. See |
111 | L<perlfaq8/"Why doesn't open() return an error when a pipe open fails?"> | |
112 | to see how to cope with this. There's also an explanation in L<perlipc>. | |
f8284313 TC |
113 | |
114 | If you would like to open a bidirectional pipe, the IPC::Open2 | |
13a2d996 SP |
115 | library will handle this for you. Check out |
116 | L<perlipc/"Bidirectional Communication with Another Process"> | |
f8284313 TC |
117 | |
118 | =head2 The Minus File | |
119 | ||
120 | Again following the lead of the standard shell utilities, Perl's | |
121 | C<open> function treats a file whose name is a single minus, "-", in a | |
122 | special way. If you open minus for reading, it really means to access | |
123 | the standard input. If you open minus for writing, it really means to | |
124 | access the standard output. | |
125 | ||
40b7eeef | 126 | If minus can be used as the default input or default output, what happens |
f8284313 | 127 | if you open a pipe into or out of minus? What's the default command it |
40b7eeef | 128 | would run? The same script as you're currently running! This is actually |
13a2d996 SP |
129 | a stealth C<fork> hidden inside an C<open> call. See |
130 | L<perlipc/"Safe Pipe Opens"> for details. | |
f8284313 TC |
131 | |
132 | =head2 Mixing Reads and Writes | |
133 | ||
134 | It is possible to specify both read and write access. All you do is | |
135 | add a "+" symbol in front of the redirection. But as in the shell, | |
136 | using a less-than on a file never creates a new file; it only opens an | |
137 | existing one. On the other hand, using a greater-than always clobbers | |
138 | (truncates to zero length) an existing file, or creates a brand-new one | |
139 | if there isn't an old one. Adding a "+" for read-write doesn't affect | |
140 | whether it only works on existing files or always clobbers existing ones. | |
141 | ||
142 | open(WTMP, "+< /usr/adm/wtmp") | |
143 | || die "can't open /usr/adm/wtmp: $!"; | |
144 | ||
145 | open(SCREEN, "+> /tmp/lkscreen") | |
146 | || die "can't open /tmp/lkscreen: $!"; | |
147 | ||
148 | open(LOGFILE, "+>> /tmp/applog" | |
149 | || die "can't open /tmp/applog: $!"; | |
150 | ||
151 | The first one won't create a new file, and the second one will always | |
152 | clobber an old one. The third one will create a new file if necessary | |
153 | and not clobber an old one, and it will allow you to read at any point | |
154 | in the file, but all writes will always go to the end. In short, | |
155 | the first case is substantially more common than the second and third | |
156 | cases, which are almost always wrong. (If you know C, the plus in | |
157 | Perl's C<open> is historically derived from the one in C's fopen(3S), | |
158 | which it ultimately calls.) | |
159 | ||
160 | In fact, when it comes to updating a file, unless you're working on | |
161 | a binary file as in the WTMP case above, you probably don't want to | |
162 | use this approach for updating. Instead, Perl's B<-i> flag comes to | |
163 | the rescue. The following command takes all the C, C++, or yacc source | |
164 | or header files and changes all their foo's to bar's, leaving | |
165 | the old version in the original file name with a ".orig" tacked | |
166 | on the end: | |
167 | ||
168 | $ perl -i.orig -pe 's/\bfoo\b/bar/g' *.[Cchy] | |
169 | ||
170 | This is a short cut for some renaming games that are really | |
171 | the best way to update textfiles. See the second question in | |
172 | L<perlfaq5> for more details. | |
173 | ||
174 | =head2 Filters | |
175 | ||
176 | One of the most common uses for C<open> is one you never | |
177 | even notice. When you process the ARGV filehandle using | |
c47ff5f1 | 178 | C<< <ARGV> >>, Perl actually does an implicit open |
f8284313 TC |
179 | on each file in @ARGV. Thus a program called like this: |
180 | ||
181 | $ myprogram file1 file2 file3 | |
182 | ||
183 | Can have all its files opened and processed one at a time | |
184 | using a construct no more complex than: | |
185 | ||
186 | while (<>) { | |
187 | # do something with $_ | |
188 | } | |
189 | ||
190 | If @ARGV is empty when the loop first begins, Perl pretends you've opened | |
191 | up minus, that is, the standard input. In fact, $ARGV, the currently | |
c47ff5f1 | 192 | open file during C<< <ARGV> >> processing, is even set to "-" |
f8284313 TC |
193 | in these circumstances. |
194 | ||
195 | You are welcome to pre-process your @ARGV before starting the loop to | |
196 | make sure it's to your liking. One reason to do this might be to remove | |
197 | command options beginning with a minus. While you can always roll the | |
198 | simple ones by hand, the Getopts modules are good for this. | |
199 | ||
200 | use Getopt::Std; | |
201 | ||
202 | # -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o | |
203 | getopts("vDo:"); | |
204 | ||
205 | # -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o} | |
206 | getopts("vDo:", \%args); | |
207 | ||
208 | Or the standard Getopt::Long module to permit named arguments: | |
209 | ||
210 | use Getopt::Long; | |
211 | GetOptions( "verbose" => \$verbose, # --verbose | |
212 | "Debug" => \$debug, # --Debug | |
213 | "output=s" => \$output ); | |
214 | # --output=somestring or --output somestring | |
215 | ||
216 | Another reason for preprocessing arguments is to make an empty | |
217 | argument list default to all files: | |
218 | ||
219 | @ARGV = glob("*") unless @ARGV; | |
220 | ||
221 | You could even filter out all but plain, text files. This is a bit | |
222 | silent, of course, and you might prefer to mention them on the way. | |
223 | ||
224 | @ARGV = grep { -f && -T } @ARGV; | |
225 | ||
226 | If you're using the B<-n> or B<-p> command-line options, you | |
227 | should put changes to @ARGV in a C<BEGIN{}> block. | |
228 | ||
229 | Remember that a normal C<open> has special properties, in that it might | |
230 | call fopen(3S) or it might called popen(3S), depending on what its | |
231 | argument looks like; that's why it's sometimes called "magic open". | |
232 | Here's an example: | |
233 | ||
234 | $pwdinfo = `domainname` =~ /^(\(none\))?$/ | |
235 | ? '< /etc/passwd' | |
236 | : 'ypcat passwd |'; | |
237 | ||
238 | open(PWD, $pwdinfo) | |
239 | or die "can't open $pwdinfo: $!"; | |
240 | ||
241 | This sort of thing also comes into play in filter processing. Because | |
c47ff5f1 | 242 | C<< <ARGV> >> processing employs the normal, shell-style Perl C<open>, |
f8284313 TC |
243 | it respects all the special things we've already seen: |
244 | ||
245 | $ myprogram f1 "cmd1|" - f2 "cmd2|" f3 < tmpfile | |
246 | ||
247 | That program will read from the file F<f1>, the process F<cmd1>, standard | |
248 | input (F<tmpfile> in this case), the F<f2> file, the F<cmd2> command, | |
249 | and finally the F<f3> file. | |
250 | ||
251 | Yes, this also means that if you have a file named "-" (and so on) in | |
252 | your directory, that they won't be processed as literal files by C<open>. | |
253 | You'll need to pass them as "./-" much as you would for the I<rm> program. | |
254 | Or you could use C<sysopen> as described below. | |
255 | ||
256 | One of the more interesting applications is to change files of a certain | |
257 | name into pipes. For example, to autoprocess gzipped or compressed | |
258 | files by decompressing them with I<gzip>: | |
259 | ||
260 | @ARGV = map { /^\.(gz|Z)$/ ? "gzip -dc $_ |" : $_ } @ARGV; | |
261 | ||
262 | Or, if you have the I<GET> program installed from LWP, | |
263 | you can fetch URLs before processing them: | |
264 | ||
265 | @ARGV = map { m#^\w+://# ? "GET $_ |" : $_ } @ARGV; | |
266 | ||
c47ff5f1 | 267 | It's not for nothing that this is called magic C<< <ARGV> >>. |
f8284313 TC |
268 | Pretty nifty, eh? |
269 | ||
270 | =head1 Open E<agrave> la C | |
271 | ||
272 | If you want the convenience of the shell, then Perl's C<open> is | |
273 | definitely the way to go. On the other hand, if you want finer precision | |
274 | than C's simplistic fopen(3S) provides, then you should look to Perl's | |
275 | C<sysopen>, which is a direct hook into the open(2) system call. | |
276 | That does mean it's a bit more involved, but that's the price of | |
277 | precision. | |
278 | ||
279 | C<sysopen> takes 3 (or 4) arguments. | |
280 | ||
281 | sysopen HANDLE, PATH, FLAGS, [MASK] | |
282 | ||
283 | The HANDLE argument is a filehandle just as with C<open>. The PATH is | |
284 | a literal path, one that doesn't pay attention to any greater-thans or | |
285 | less-thans or pipes or minuses, nor ignore white space. If it's there, | |
286 | it's part of the path. The FLAGS argument contains one or more values | |
287 | derived from the Fcntl module that have been or'd together using the | |
288 | bitwise "|" operator. The final argument, the MASK, is optional; if | |
289 | present, it is combined with the user's current umask for the creation | |
290 | mode of the file. You should usually omit this. | |
291 | ||
292 | Although the traditional values of read-only, write-only, and read-write | |
293 | are 0, 1, and 2 respectively, this is known not to hold true on some | |
294 | systems. Instead, it's best to load in the appropriate constants first | |
295 | from the Fcntl module, which supplies the following standard flags: | |
296 | ||
297 | O_RDONLY Read only | |
298 | O_WRONLY Write only | |
299 | O_RDWR Read and write | |
300 | O_CREAT Create the file if it doesn't exist | |
301 | O_EXCL Fail if the file already exists | |
302 | O_APPEND Append to the file | |
303 | O_TRUNC Truncate the file | |
304 | O_NONBLOCK Non-blocking access | |
305 | ||
ca6e1c26 JH |
306 | Less common flags that are sometimes available on some operating |
307 | systems include C<O_BINARY>, C<O_TEXT>, C<O_SHLOCK>, C<O_EXLOCK>, | |
308 | C<O_DEFER>, C<O_SYNC>, C<O_ASYNC>, C<O_DSYNC>, C<O_RSYNC>, | |
309 | C<O_NOCTTY>, C<O_NDELAY> and C<O_LARGEFILE>. Consult your open(2) | |
310 | manpage or its local equivalent for details. (Note: starting from | |
311 | Perl release 5.6 the O_LARGEFILE flag, if available, is automatically | |
106325ad | 312 | added to the sysopen() flags because large files are the default.) |
f8284313 TC |
313 | |
314 | Here's how to use C<sysopen> to emulate the simple C<open> calls we had | |
315 | before. We'll omit the C<|| die $!> checks for clarity, but make sure | |
316 | you always check the return values in real code. These aren't quite | |
317 | the same, since C<open> will trim leading and trailing white space, | |
318 | but you'll get the idea: | |
319 | ||
320 | To open a file for reading: | |
321 | ||
322 | open(FH, "< $path"); | |
323 | sysopen(FH, $path, O_RDONLY); | |
324 | ||
325 | To open a file for writing, creating a new file if needed or else truncating | |
326 | an old file: | |
327 | ||
328 | open(FH, "> $path"); | |
329 | sysopen(FH, $path, O_WRONLY | O_TRUNC | O_CREAT); | |
330 | ||
331 | To open a file for appending, creating one if necessary: | |
332 | ||
333 | open(FH, ">> $path"); | |
334 | sysopen(FH, $path, O_WRONLY | O_APPEND | O_CREAT); | |
335 | ||
336 | To open a file for update, where the file must already exist: | |
337 | ||
338 | open(FH, "+< $path"); | |
339 | sysopen(FH, $path, O_RDWR); | |
340 | ||
341 | And here are things you can do with C<sysopen> that you cannot do with | |
342 | a regular C<open>. As you see, it's just a matter of controlling the | |
343 | flags in the third argument. | |
344 | ||
345 | To open a file for writing, creating a new file which must not previously | |
346 | exist: | |
347 | ||
348 | sysopen(FH, $path, O_WRONLY | O_EXCL | O_CREAT); | |
349 | ||
350 | To open a file for appending, where that file must already exist: | |
351 | ||
352 | sysopen(FH, $path, O_WRONLY | O_APPEND); | |
353 | ||
354 | To open a file for update, creating a new file if necessary: | |
355 | ||
356 | sysopen(FH, $path, O_RDWR | O_CREAT); | |
357 | ||
358 | To open a file for update, where that file must not already exist: | |
359 | ||
360 | sysopen(FH, $path, O_RDWR | O_EXCL | O_CREAT); | |
361 | ||
362 | To open a file without blocking, creating one if necessary: | |
363 | ||
364 | sysopen(FH, $path, O_WRONLY | O_NONBLOCK | O_CREAT); | |
365 | ||
366 | =head2 Permissions E<agrave> la mode | |
367 | ||
368 | If you omit the MASK argument to C<sysopen>, Perl uses the octal value | |
369 | 0666. The normal MASK to use for executables and directories should | |
370 | be 0777, and for anything else, 0666. | |
371 | ||
372 | Why so permissive? Well, it isn't really. The MASK will be modified | |
373 | by your process's current C<umask>. A umask is a number representing | |
374 | I<disabled> permissions bits; that is, bits that will not be turned on | |
375 | in the created files' permissions field. | |
376 | ||
377 | For example, if your C<umask> were 027, then the 020 part would | |
378 | disable the group from writing, and the 007 part would disable others | |
379 | from reading, writing, or executing. Under these conditions, passing | |
380 | C<sysopen> 0666 would create a file with mode 0640, since C<0666 &~ 027> | |
381 | is 0640. | |
382 | ||
383 | You should seldom use the MASK argument to C<sysopen()>. That takes | |
384 | away the user's freedom to choose what permission new files will have. | |
385 | Denying choice is almost always a bad thing. One exception would be for | |
386 | cases where sensitive or private data is being stored, such as with mail | |
387 | folders, cookie files, and internal temporary files. | |
388 | ||
389 | =head1 Obscure Open Tricks | |
390 | ||
391 | =head2 Re-Opening Files (dups) | |
392 | ||
393 | Sometimes you already have a filehandle open, and want to make another | |
394 | handle that's a duplicate of the first one. In the shell, we place an | |
395 | ampersand in front of a file descriptor number when doing redirections. | |
c47ff5f1 | 396 | For example, C<< 2>&1 >> makes descriptor 2 (that's STDERR in Perl) |
f8284313 TC |
397 | be redirected into descriptor 1 (which is usually Perl's STDOUT). |
398 | The same is essentially true in Perl: a filename that begins with an | |
399 | ampersand is treated instead as a file descriptor if a number, or as a | |
400 | filehandle if a string. | |
401 | ||
402 | open(SAVEOUT, ">&SAVEERR") || die "couldn't dup SAVEERR: $!"; | |
403 | open(MHCONTEXT, "<&4") || die "couldn't dup fd4: $!"; | |
404 | ||
405 | That means that if a function is expecting a filename, but you don't | |
406 | want to give it a filename because you already have the file open, you | |
407 | can just pass the filehandle with a leading ampersand. It's best to | |
408 | use a fully qualified handle though, just in case the function happens | |
409 | to be in a different package: | |
410 | ||
411 | somefunction("&main::LOGFILE"); | |
412 | ||
413 | This way if somefunction() is planning on opening its argument, it can | |
414 | just use the already opened handle. This differs from passing a handle, | |
415 | because with a handle, you don't open the file. Here you have something | |
416 | you can pass to open. | |
417 | ||
418 | If you have one of those tricky, newfangled I/O objects that the C++ | |
419 | folks are raving about, then this doesn't work because those aren't a | |
420 | proper filehandle in the native Perl sense. You'll have to use fileno() | |
421 | to pull out the proper descriptor number, assuming you can: | |
422 | ||
423 | use IO::Socket; | |
424 | $handle = IO::Socket::INET->new("www.perl.com:80"); | |
425 | $fd = $handle->fileno; | |
426 | somefunction("&$fd"); # not an indirect function call | |
427 | ||
428 | It can be easier (and certainly will be faster) just to use real | |
429 | filehandles though: | |
430 | ||
431 | use IO::Socket; | |
432 | local *REMOTE = IO::Socket::INET->new("www.perl.com:80"); | |
433 | die "can't connect" unless defined(fileno(REMOTE)); | |
434 | somefunction("&main::REMOTE"); | |
435 | ||
436 | If the filehandle or descriptor number is preceded not just with a simple | |
437 | "&" but rather with a "&=" combination, then Perl will not create a | |
438 | completely new descriptor opened to the same place using the dup(2) | |
439 | system call. Instead, it will just make something of an alias to the | |
440 | existing one using the fdopen(3S) library call This is slightly more | |
441 | parsimonious of systems resources, although this is less a concern | |
442 | these days. Here's an example of that: | |
443 | ||
444 | $fd = $ENV{"MHCONTEXTFD"}; | |
445 | open(MHCONTEXT, "<&=$fd") or die "couldn't fdopen $fd: $!"; | |
446 | ||
c47ff5f1 GS |
447 | If you're using magic C<< <ARGV> >>, you could even pass in as a |
448 | command line argument in @ARGV something like C<"<&=$MHCONTEXTFD">, | |
f8284313 TC |
449 | but we've never seen anyone actually do this. |
450 | ||
451 | =head2 Dispelling the Dweomer | |
452 | ||
453 | Perl is more of a DWIMmer language than something like Java--where DWIM | |
454 | is an acronym for "do what I mean". But this principle sometimes leads | |
455 | to more hidden magic than one knows what to do with. In this way, Perl | |
456 | is also filled with I<dweomer>, an obscure word meaning an enchantment. | |
457 | Sometimes, Perl's DWIMmer is just too much like dweomer for comfort. | |
458 | ||
459 | If magic C<open> is a bit too magical for you, you don't have to turn | |
460 | to C<sysopen>. To open a file with arbitrary weird characters in | |
461 | it, it's necessary to protect any leading and trailing whitespace. | |
462 | Leading whitespace is protected by inserting a C<"./"> in front of a | |
463 | filename that starts with whitespace. Trailing whitespace is protected | |
464 | by appending an ASCII NUL byte (C<"\0">) at the end off the string. | |
465 | ||
466 | $file =~ s#^(\s)#./$1#; | |
467 | open(FH, "< $file\0") || die "can't open $file: $!"; | |
468 | ||
469 | This assumes, of course, that your system considers dot the current | |
470 | working directory, slash the directory separator, and disallows ASCII | |
471 | NULs within a valid filename. Most systems follow these conventions, | |
472 | including all POSIX systems as well as proprietary Microsoft systems. | |
473 | The only vaguely popular system that doesn't work this way is the | |
474 | proprietary Macintosh system, which uses a colon where the rest of us | |
475 | use a slash. Maybe C<sysopen> isn't such a bad idea after all. | |
476 | ||
c47ff5f1 | 477 | If you want to use C<< <ARGV> >> processing in a totally boring |
f8284313 TC |
478 | and non-magical way, you could do this first: |
479 | ||
480 | # "Sam sat on the ground and put his head in his hands. | |
481 | # 'I wish I had never come here, and I don't want to see | |
482 | # no more magic,' he said, and fell silent." | |
483 | for (@ARGV) { | |
484 | s#^([^./])#./$1#; | |
485 | $_ .= "\0"; | |
486 | } | |
487 | while (<>) { | |
488 | # now process $_ | |
489 | } | |
490 | ||
491 | But be warned that users will not appreciate being unable to use "-" | |
492 | to mean standard input, per the standard convention. | |
493 | ||
494 | =head2 Paths as Opens | |
495 | ||
496 | You've probably noticed how Perl's C<warn> and C<die> functions can | |
497 | produce messages like: | |
498 | ||
1761cee5 | 499 | Some warning at scriptname line 29, <FH> line 7. |
f8284313 TC |
500 | |
501 | That's because you opened a filehandle FH, and had read in seven records | |
502 | from it. But what was the name of the file, not the handle? | |
503 | ||
504 | If you aren't running with C<strict refs>, or if you've turn them off | |
505 | temporarily, then all you have to do is this: | |
506 | ||
507 | open($path, "< $path") || die "can't open $path: $!"; | |
508 | while (<$path>) { | |
509 | # whatever | |
510 | } | |
511 | ||
512 | Since you're using the pathname of the file as its handle, | |
513 | you'll get warnings more like | |
514 | ||
1761cee5 | 515 | Some warning at scriptname line 29, </etc/motd> line 7. |
f8284313 TC |
516 | |
517 | =head2 Single Argument Open | |
518 | ||
519 | Remember how we said that Perl's open took two arguments? That was a | |
520 | passive prevarication. You see, it can also take just one argument. | |
521 | If and only if the variable is a global variable, not a lexical, you | |
522 | can pass C<open> just one argument, the filehandle, and it will | |
523 | get the path from the global scalar variable of the same name. | |
524 | ||
525 | $FILE = "/etc/motd"; | |
526 | open FILE or die "can't open $FILE: $!"; | |
527 | while (<FILE>) { | |
528 | # whatever | |
529 | } | |
530 | ||
531 | Why is this here? Someone has to cater to the hysterical porpoises. | |
532 | It's something that's been in Perl since the very beginning, if not | |
533 | before. | |
534 | ||
535 | =head2 Playing with STDIN and STDOUT | |
536 | ||
537 | One clever move with STDOUT is to explicitly close it when you're done | |
538 | with the program. | |
539 | ||
540 | END { close(STDOUT) || die "can't close stdout: $!" } | |
541 | ||
542 | If you don't do this, and your program fills up the disk partition due | |
543 | to a command line redirection, it won't report the error exit with a | |
544 | failure status. | |
545 | ||
546 | You don't have to accept the STDIN and STDOUT you were given. You are | |
547 | welcome to reopen them if you'd like. | |
548 | ||
549 | open(STDIN, "< datafile") | |
550 | || die "can't open datafile: $!"; | |
551 | ||
552 | open(STDOUT, "> output") | |
553 | || die "can't open output: $!"; | |
554 | ||
555 | And then these can be read directly or passed on to subprocesses. | |
556 | This makes it look as though the program were initially invoked | |
557 | with those redirections from the command line. | |
558 | ||
559 | It's probably more interesting to connect these to pipes. For example: | |
560 | ||
561 | $pager = $ENV{PAGER} || "(less || more)"; | |
562 | open(STDOUT, "| $pager") | |
563 | || die "can't fork a pager: $!"; | |
564 | ||
565 | This makes it appear as though your program were called with its stdout | |
566 | already piped into your pager. You can also use this kind of thing | |
567 | in conjunction with an implicit fork to yourself. You might do this | |
568 | if you would rather handle the post processing in your own program, | |
569 | just in a different process: | |
570 | ||
571 | head(100); | |
572 | while (<>) { | |
573 | print; | |
574 | } | |
575 | ||
576 | sub head { | |
577 | my $lines = shift || 20; | |
578 | return unless $pid = open(STDOUT, "|-"); | |
579 | die "cannot fork: $!" unless defined $pid; | |
580 | while (<STDIN>) { | |
581 | print; | |
582 | last if --$lines < 0; | |
583 | } | |
584 | exit; | |
585 | } | |
586 | ||
587 | This technique can be applied to repeatedly push as many filters on your | |
588 | output stream as you wish. | |
589 | ||
590 | =head1 Other I/O Issues | |
591 | ||
592 | These topics aren't really arguments related to C<open> or C<sysopen>, | |
593 | but they do affect what you do with your open files. | |
594 | ||
595 | =head2 Opening Non-File Files | |
596 | ||
597 | When is a file not a file? Well, you could say when it exists but | |
598 | isn't a plain file. We'll check whether it's a symbolic link first, | |
599 | just in case. | |
600 | ||
601 | if (-l $file || ! -f _) { | |
602 | print "$file is not a plain file\n"; | |
603 | } | |
604 | ||
605 | What other kinds of files are there than, well, files? Directories, | |
606 | symbolic links, named pipes, Unix-domain sockets, and block and character | |
607 | devices. Those are all files, too--just not I<plain> files. This isn't | |
608 | the same issue as being a text file. Not all text files are plain files. | |
609 | Not all plain files are textfiles. That's why there are separate C<-f> | |
610 | and C<-T> file tests. | |
611 | ||
612 | To open a directory, you should use the C<opendir> function, then | |
613 | process it with C<readdir>, carefully restoring the directory | |
614 | name if necessary: | |
615 | ||
616 | opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; | |
617 | while (defined($file = readdir(DIR))) { | |
618 | # do something with "$dirname/$file" | |
619 | } | |
620 | closedir(DIR); | |
621 | ||
622 | If you want to process directories recursively, it's better to use the | |
623 | File::Find module. For example, this prints out all files recursively, | |
624 | add adds a slash to their names if the file is a directory. | |
625 | ||
626 | @ARGV = qw(.) unless @ARGV; | |
627 | use File::Find; | |
628 | find sub { print $File::Find::name, -d && '/', "\n" }, @ARGV; | |
629 | ||
630 | This finds all bogus symbolic links beneath a particular directory: | |
631 | ||
632 | find sub { print "$File::Find::name\n" if -l && !-e }, $dir; | |
633 | ||
634 | As you see, with symbolic links, you can just pretend that it is | |
635 | what it points to. Or, if you want to know I<what> it points to, then | |
636 | C<readlink> is called for: | |
637 | ||
638 | if (-l $file) { | |
639 | if (defined($whither = readlink($file))) { | |
640 | print "$file points to $whither\n"; | |
641 | } else { | |
642 | print "$file points nowhere: $!\n"; | |
643 | } | |
644 | } | |
645 | ||
646 | Named pipes are a different matter. You pretend they're regular files, | |
647 | but their opens will normally block until there is both a reader and | |
648 | a writer. You can read more about them in L<perlipc/"Named Pipes">. | |
649 | Unix-domain sockets are rather different beasts as well; they're | |
650 | described in L<perlipc/"Unix-Domain TCP Clients and Servers">. | |
651 | ||
652 | When it comes to opening devices, it can be easy and it can tricky. | |
653 | We'll assume that if you're opening up a block device, you know what | |
654 | you're doing. The character devices are more interesting. These are | |
655 | typically used for modems, mice, and some kinds of printers. This is | |
656 | described in L<perlfaq8/"How do I read and write the serial port?"> | |
657 | It's often enough to open them carefully: | |
658 | ||
659 | sysopen(TTYIN, "/dev/ttyS1", O_RDWR | O_NDELAY | O_NOCTTY) | |
660 | # (O_NOCTTY no longer needed on POSIX systems) | |
661 | or die "can't open /dev/ttyS1: $!"; | |
662 | open(TTYOUT, "+>&TTYIN") | |
663 | or die "can't dup TTYIN: $!"; | |
664 | ||
665 | $ofh = select(TTYOUT); $| = 1; select($ofh); | |
666 | ||
667 | print TTYOUT "+++at\015"; | |
668 | $answer = <TTYIN>; | |
669 | ||
670 | With descriptors that you haven't opened using C<sysopen>, such as a | |
671 | socket, you can set them to be non-blocking using C<fcntl>: | |
672 | ||
673 | use Fcntl; | |
674 | fcntl(Connection, F_SETFL, O_NONBLOCK) | |
675 | or die "can't set non blocking: $!"; | |
676 | ||
677 | Rather than losing yourself in a morass of twisting, turning C<ioctl>s, | |
678 | all dissimilar, if you're going to manipulate ttys, it's best to | |
679 | make calls out to the stty(1) program if you have it, or else use the | |
680 | portable POSIX interface. To figure this all out, you'll need to read the | |
681 | termios(3) manpage, which describes the POSIX interface to tty devices, | |
682 | and then L<POSIX>, which describes Perl's interface to POSIX. There are | |
683 | also some high-level modules on CPAN that can help you with these games. | |
684 | Check out Term::ReadKey and Term::ReadLine. | |
685 | ||
686 | What else can you open? To open a connection using sockets, you won't use | |
13a2d996 SP |
687 | one of Perl's two open functions. See |
688 | L<perlipc/"Sockets: Client/Server Communication"> for that. Here's an | |
689 | example. Once you have it, you can use FH as a bidirectional filehandle. | |
f8284313 TC |
690 | |
691 | use IO::Socket; | |
692 | local *FH = IO::Socket::INET->new("www.perl.com:80"); | |
693 | ||
694 | For opening up a URL, the LWP modules from CPAN are just what | |
695 | the doctor ordered. There's no filehandle interface, but | |
696 | it's still easy to get the contents of a document: | |
697 | ||
698 | use LWP::Simple; | |
6cecdcac | 699 | $doc = get('http://www.linpro.no/lwp/'); |
f8284313 TC |
700 | |
701 | =head2 Binary Files | |
702 | ||
703 | On certain legacy systems with what could charitably be called terminally | |
704 | convoluted (some would say broken) I/O models, a file isn't a file--at | |
705 | least, not with respect to the C standard I/O library. On these old | |
706 | systems whose libraries (but not kernels) distinguish between text and | |
707 | binary streams, to get files to behave properly you'll have to bend over | |
708 | backwards to avoid nasty problems. On such infelicitous systems, sockets | |
709 | and pipes are already opened in binary mode, and there is currently no | |
710 | way to turn that off. With files, you have more options. | |
711 | ||
712 | Another option is to use the C<binmode> function on the appropriate | |
713 | handles before doing regular I/O on them: | |
714 | ||
715 | binmode(STDIN); | |
716 | binmode(STDOUT); | |
717 | while (<STDIN>) { print } | |
718 | ||
719 | Passing C<sysopen> a non-standard flag option will also open the file in | |
720 | binary mode on those systems that support it. This is the equivalent of | |
721 | opening the file normally, then calling C<binmode>ing on the handle. | |
722 | ||
723 | sysopen(BINDAT, "records.data", O_RDWR | O_BINARY) | |
724 | || die "can't open records.data: $!"; | |
725 | ||
726 | Now you can use C<read> and C<print> on that handle without worrying | |
727 | about the system non-standard I/O library breaking your data. It's not | |
728 | a pretty picture, but then, legacy systems seldom are. CP/M will be | |
729 | with us until the end of days, and after. | |
730 | ||
731 | On systems with exotic I/O systems, it turns out that, astonishingly | |
732 | enough, even unbuffered I/O using C<sysread> and C<syswrite> might do | |
733 | sneaky data mutilation behind your back. | |
734 | ||
735 | while (sysread(WHENCE, $buf, 1024)) { | |
736 | syswrite(WHITHER, $buf, length($buf)); | |
737 | } | |
738 | ||
739 | Depending on the vicissitudes of your runtime system, even these calls | |
740 | may need C<binmode> or C<O_BINARY> first. Systems known to be free of | |
741 | such difficulties include Unix, the Mac OS, Plan9, and Inferno. | |
742 | ||
743 | =head2 File Locking | |
744 | ||
745 | In a multitasking environment, you may need to be careful not to collide | |
746 | with other processes who want to do I/O on the same files as others | |
747 | are working on. You'll often need shared or exclusive locks | |
748 | on files for reading and writing respectively. You might just | |
749 | pretend that only exclusive locks exist. | |
750 | ||
751 | Never use the existence of a file C<-e $file> as a locking indication, | |
752 | because there is a race condition between the test for the existence of | |
753 | the file and its creation. Atomicity is critical. | |
754 | ||
755 | Perl's most portable locking interface is via the C<flock> function, | |
756 | whose simplicity is emulated on systems that don't directly support it, | |
757 | such as SysV or WindowsNT. The underlying semantics may affect how | |
758 | it all works, so you should learn how C<flock> is implemented on your | |
759 | system's port of Perl. | |
760 | ||
761 | File locking I<does not> lock out another process that would like to | |
762 | do I/O. A file lock only locks out others trying to get a lock, not | |
763 | processes trying to do I/O. Because locks are advisory, if one process | |
764 | uses locking and another doesn't, all bets are off. | |
765 | ||
766 | By default, the C<flock> call will block until a lock is granted. | |
767 | A request for a shared lock will be granted as soon as there is no | |
768 | exclusive locker. A request for a exclusive lock will be granted as | |
769 | soon as there is no locker of any kind. Locks are on file descriptors, | |
770 | not file names. You can't lock a file until you open it, and you can't | |
771 | hold on to a lock once the file has been closed. | |
772 | ||
773 | Here's how to get a blocking shared lock on a file, typically used | |
774 | for reading: | |
775 | ||
776 | use 5.004; | |
777 | use Fcntl qw(:DEFAULT :flock); | |
778 | open(FH, "< filename") or die "can't open filename: $!"; | |
779 | flock(FH, LOCK_SH) or die "can't lock filename: $!"; | |
780 | # now read from FH | |
781 | ||
782 | You can get a non-blocking lock by using C<LOCK_NB>. | |
783 | ||
784 | flock(FH, LOCK_SH | LOCK_NB) | |
785 | or die "can't lock filename: $!"; | |
786 | ||
787 | This can be useful for producing more user-friendly behaviour by warning | |
788 | if you're going to be blocking: | |
789 | ||
790 | use 5.004; | |
791 | use Fcntl qw(:DEFAULT :flock); | |
792 | open(FH, "< filename") or die "can't open filename: $!"; | |
793 | unless (flock(FH, LOCK_SH | LOCK_NB)) { | |
794 | $| = 1; | |
795 | print "Waiting for lock..."; | |
796 | flock(FH, LOCK_SH) or die "can't lock filename: $!"; | |
797 | print "got it.\n" | |
798 | } | |
799 | # now read from FH | |
800 | ||
801 | To get an exclusive lock, typically used for writing, you have to be | |
802 | careful. We C<sysopen> the file so it can be locked before it gets | |
803 | emptied. You can get a nonblocking version using C<LOCK_EX | LOCK_NB>. | |
804 | ||
805 | use 5.004; | |
806 | use Fcntl qw(:DEFAULT :flock); | |
807 | sysopen(FH, "filename", O_WRONLY | O_CREAT) | |
808 | or die "can't open filename: $!"; | |
809 | flock(FH, LOCK_EX) | |
810 | or die "can't lock filename: $!"; | |
811 | truncate(FH, 0) | |
812 | or die "can't truncate filename: $!"; | |
813 | # now write to FH | |
814 | ||
815 | Finally, due to the uncounted millions who cannot be dissuaded from | |
816 | wasting cycles on useless vanity devices called hit counters, here's | |
817 | how to increment a number in a file safely: | |
818 | ||
819 | use Fcntl qw(:DEFAULT :flock); | |
820 | ||
821 | sysopen(FH, "numfile", O_RDWR | O_CREAT) | |
822 | or die "can't open numfile: $!"; | |
823 | # autoflush FH | |
824 | $ofh = select(FH); $| = 1; select ($ofh); | |
825 | flock(FH, LOCK_EX) | |
826 | or die "can't write-lock numfile: $!"; | |
827 | ||
828 | $num = <FH> || 0; | |
829 | seek(FH, 0, 0) | |
830 | or die "can't rewind numfile : $!"; | |
831 | print FH $num+1, "\n" | |
832 | or die "can't write numfile: $!"; | |
833 | ||
834 | truncate(FH, tell(FH)) | |
835 | or die "can't truncate numfile: $!"; | |
836 | close(FH) | |
837 | or die "can't close numfile: $!"; | |
838 | ||
839 | =head1 SEE ALSO | |
840 | ||
841 | The C<open> and C<sysopen> function in perlfunc(1); | |
842 | the standard open(2), dup(2), fopen(3), and fdopen(3) manpages; | |
843 | the POSIX documentation. | |
844 | ||
845 | =head1 AUTHOR and COPYRIGHT | |
846 | ||
847 | Copyright 1998 Tom Christiansen. | |
848 | ||
849 | When included as part of the Standard Version of Perl, or as part of | |
850 | its complete documentation whether printed or otherwise, this work may | |
851 | be distributed only under the terms of Perl's Artistic License. Any | |
852 | distribution of this file or derivatives thereof outside of that | |
853 | package require that special arrangements be made with copyright | |
854 | holder. | |
855 | ||
856 | Irrespective of its distribution, all code examples in these files are | |
857 | hereby placed into the public domain. You are permitted and | |
858 | encouraged to use this code in your own programs for fun or for profit | |
859 | as you see fit. A simple comment in the code giving credit would be | |
860 | courteous but is not required. | |
861 | ||
862 | =head1 HISTORY | |
863 | ||
864 | First release: Sat Jan 9 08:09:11 MST 1999 |