Integrate with Sarathy.
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
1 =head1 NAME
2
3 perlfaq5 - Files and Formats ($Revision: 1.38 $, $Date: 1999/05/23 16:08:30 $)
4
5 =head1 DESCRIPTION
6
7 This section deals with I/O and the "f" issues: filehandles, flushing,
8 formats, and footers.
9
10 =head2 How do I flush/unbuffer an output filehandle?  Why must I do this?
11
12 The C standard I/O library (stdio) normally buffers characters sent to
13 devices.  This is done for efficiency reasons, so that there isn't a
14 system call for each byte.  Any time you use print() or write() in
15 Perl, you go though this buffering.  syswrite() circumvents stdio and
16 buffering.
17
18 In most stdio implementations, the type of output buffering and the size of
19 the buffer varies according to the type of device.  Disk files are block
20 buffered, often with a buffer size of more than 2k.  Pipes and sockets
21 are often buffered with a buffer size between 1/2 and 2k.  Serial devices
22 (e.g. modems, terminals) are normally line-buffered, and stdio sends
23 the entire line when it gets the newline.
24
25 Perl does not support truly unbuffered output (except insofar as you can
26 C<syswrite(OUT, $char, 1)>).  What it does instead support is "command
27 buffering", in which a physical write is performed after every output
28 command.  This isn't as hard on your system as unbuffering, but does
29 get the output where you want it when you want it.
30
31 If you expect characters to get to your device when you print them there,
32 you'll want to autoflush its handle.
33 Use select() and the C<$|> variable to control autoflushing
34 (see L<perlvar/$|> and L<perlfunc/select>):
35
36     $old_fh = select(OUTPUT_HANDLE);
37     $| = 1;
38     select($old_fh);
39
40 Or using the traditional idiom:
41
42     select((select(OUTPUT_HANDLE), $| = 1)[0]);
43
44 Or if don't mind slowly loading several thousand lines of module code
45 just because you're afraid of the C<$|> variable:
46
47     use FileHandle;
48     open(DEV, "+</dev/tty");      # ceci n'est pas une pipe
49     DEV->autoflush(1);
50
51 or the newer IO::* modules:
52
53     use IO::Handle;
54     open(DEV, ">/dev/printer");   # but is this?
55     DEV->autoflush(1);
56
57 or even this:
58
59     use IO::Socket;               # this one is kinda a pipe?
60     $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.com',
61                                   PeerPort => 'http(80)',
62                                   Proto    => 'tcp');
63     die "$!" unless $sock;
64
65     $sock->autoflush();
66     print $sock "GET / HTTP/1.0" . "\015\012" x 2;
67     $document = join('', <$sock>);
68     print "DOC IS: $document\n";
69
70 Note the bizarrely hardcoded carriage return and newline in their octal
71 equivalents.  This is the ONLY way (currently) to assure a proper flush
72 on all platforms, including Macintosh.  That's the way things work in
73 network programming: you really should specify the exact bit pattern
74 on the network line terminator.  In practice, C<"\n\n"> often works,
75 but this is not portable.
76
77 See L<perlfaq9> for other examples of fetching URLs over the web.
78
79 =head2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
80
81 Those are operations of a text editor.  Perl is not a text editor.
82 Perl is a programming language.  You have to decompose the problem into
83 low-level calls to read, write, open, close, and seek.
84
85 Although humans have an easy time thinking of a text file as being a
86 sequence of lines that operates much like a stack of playing cards -- or
87 punch cards -- computers usually see the text file as a sequence of bytes.
88 In general, there's no direct way for Perl to seek to a particular line
89 of a file, insert text into a file, or remove text from a file.
90
91 (There are exceptions in special circumstances.  You can add or remove at
92 the very end of the file.  Another is replacing a sequence of bytes with
93 another sequence of the same length.  Another is using the C<$DB_RECNO>
94 array bindings as documented in L<DB_File>.  Yet another is manipulating
95 files with all lines the same length.)
96
97 The general solution is to create a temporary copy of the text file with
98 the changes you want, then copy that over the original.  This assumes
99 no locking.
100
101     $old = $file;
102     $new = "$file.tmp.$$";
103     $bak = "$file.orig";
104
105     open(OLD, "< $old")         or die "can't open $old: $!";
106     open(NEW, "> $new")         or die "can't open $new: $!";
107
108     # Correct typos, preserving case
109     while (<OLD>) {
110         s/\b(p)earl\b/${1}erl/i;
111         (print NEW $_)          or die "can't write to $new: $!";
112     }
113
114     close(OLD)                  or die "can't close $old: $!";
115     close(NEW)                  or die "can't close $new: $!";
116
117     rename($old, $bak)          or die "can't rename $old to $bak: $!";
118     rename($new, $old)          or die "can't rename $new to $old: $!";
119
120 Perl can do this sort of thing for you automatically with the C<-i>
121 command-line switch or the closely-related C<$^I> variable (see
122 L<perlrun> for more details).  Note that
123 C<-i> may require a suffix on some non-Unix systems; see the
124 platform-specific documentation that came with your port.
125
126     # Renumber a series of tests from the command line
127     perl -pi -e 's/(^\s+test\s+)\d+/ $1 . ++$count /e' t/op/taint.t
128
129     # form a script
130     local($^I, @ARGV) = ('.orig', glob("*.c"));
131     while (<>) {
132         if ($. == 1) {
133             print "This line should appear at the top of each file\n";
134         }
135         s/\b(p)earl\b/${1}erl/i;        # Correct typos, preserving case
136         print;
137         close ARGV if eof;              # Reset $.
138     }
139
140 If you need to seek to an arbitrary line of a file that changes
141 infrequently, you could build up an index of byte positions of where
142 the line ends are in the file.  If the file is large, an index of
143 every tenth or hundredth line end would allow you to seek and read
144 fairly efficiently.  If the file is sorted, try the look.pl library
145 (part of the standard perl distribution).
146
147 In the unique case of deleting lines at the end of a file, you
148 can use tell() and truncate().  The following code snippet deletes
149 the last line of a file without making a copy or reading the
150 whole file into memory:
151
152         open (FH, "+< $file");
153         while ( <FH> ) { $addr = tell(FH) unless eof(FH) }
154         truncate(FH, $addr);
155
156 Error checking is left as an exercise for the reader.
157
158 =head2 How do I count the number of lines in a file?
159
160 One fairly efficient way is to count newlines in the file. The
161 following program uses a feature of tr///, as documented in L<perlop>.
162 If your text file doesn't end with a newline, then it's not really a
163 proper text file, so this may report one fewer line than you expect.
164
165     $lines = 0;
166     open(FILE, $filename) or die "Can't open `$filename': $!";
167     while (sysread FILE, $buffer, 4096) {
168         $lines += ($buffer =~ tr/\n//);
169     }
170     close FILE;
171
172 This assumes no funny games with newline translations.
173
174 =head2 How do I make a temporary file name?
175
176 Use the C<new_tmpfile> class method from the IO::File module to get a
177 filehandle opened for reading and writing.  Use this if you don't
178 need to know the file's name.
179
180     use IO::File;
181     $fh = IO::File->new_tmpfile()
182         or die "Unable to make new temporary file: $!";
183
184 Or you can use the C<tmpnam> function from the POSIX module to get a
185 filename that you then open yourself.  Use this if you do need to know
186 the file's name.
187
188     use Fcntl;
189     use POSIX qw(tmpnam);
190
191     # try new temporary filenames until we get one that didn't already
192     # exist;  the check should be unnecessary, but you can't be too careful
193     do { $name = tmpnam() }
194         until sysopen(FH, $name, O_RDWR|O_CREAT|O_EXCL);
195
196     # install atexit-style handler so that when we exit or die,
197     # we automatically delete this temporary file
198     END { unlink($name) or die "Couldn't unlink $name : $!" }
199
200     # now go on to use the file ...
201
202 If you're committed to doing this by hand, use the process ID and/or
203 the current time-value.  If you need to have many temporary files in
204 one process, use a counter:
205
206     BEGIN {
207         use Fcntl;
208         my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMP} || $ENV{TEMP};
209         my $base_name = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
210         sub temp_file {
211             local *FH;
212             my $count = 0;
213             until (defined(fileno(FH)) || $count++ > 100) {
214                 $base_name =~ s/-(\d+)$/"-" . (1 + $1)/e;
215                 sysopen(FH, $base_name, O_WRONLY|O_EXCL|O_CREAT);
216             }
217             if (defined(fileno(FH))
218                 return (*FH, $base_name);
219             } else {
220                 return ();
221             }
222         }
223     }
224
225 =head2 How can I manipulate fixed-record-length files?
226
227 The most efficient way is using pack() and unpack().  This is faster than
228 using substr() when taking many, many strings.  It is slower for just a few.
229
230 Here is a sample chunk of code to break up and put back together again
231 some fixed-format input lines, in this case from the output of a normal,
232 Berkeley-style ps:
233
234     # sample input line:
235     #   15158 p5  T      0:00 perl /home/tchrist/scripts/now-what
236     $PS_T = 'A6 A4 A7 A5 A*';
237     open(PS, "ps|");
238     print scalar <PS>; 
239     while (<PS>) {
240         ($pid, $tt, $stat, $time, $command) = unpack($PS_T, $_);
241         for $var (qw!pid tt stat time command!) {
242             print "$var: <$$var>\n";
243         }
244         print 'line=', pack($PS_T, $pid, $tt, $stat, $time, $command),
245                 "\n";
246     }
247
248 We've used C<$$var> in a way that forbidden by C<use strict 'refs'>.
249 That is, we've promoted a string to a scalar variable reference using
250 symbolic references.  This is ok in small programs, but doesn't scale
251 well.   It also only works on global variables, not lexicals.
252
253 =head2 How can I make a filehandle local to a subroutine?  How do I pass filehandles between subroutines?  How do I make an array of filehandles?
254
255 The fastest, simplest, and most direct way is to localize the typeglob
256 of the filehandle in question:
257
258     local *TmpHandle;
259
260 Typeglobs are fast (especially compared with the alternatives) and
261 reasonably easy to use, but they also have one subtle drawback.  If you
262 had, for example, a function named TmpHandle(), or a variable named
263 %TmpHandle, you just hid it from yourself.
264
265     sub findme {
266         local *HostFile;
267         open(HostFile, "</etc/hosts") or die "no /etc/hosts: $!";
268         local $_;               # <- VERY IMPORTANT
269         while (<HostFile>) {
270             print if /\b127\.(0\.0\.)?1\b/;
271         }
272         # *HostFile automatically closes/disappears here
273     }
274
275 Here's how to use this in a loop to open and store a bunch of
276 filehandles.  We'll use as values of the hash an ordered
277 pair to make it easy to sort the hash in insertion order.
278
279     @names = qw(motd termcap passwd hosts);
280     my $i = 0;
281     foreach $filename (@names) {
282         local *FH;
283         open(FH, "/etc/$filename") || die "$filename: $!";
284         $file{$filename} = [ $i++, *FH ];
285     }
286
287     # Using the filehandles in the array
288     foreach $name (sort { $file{$a}[0] <=> $file{$b}[0] } keys %file) {
289         my $fh = $file{$name}[1];
290         my $line = <$fh>;
291         print "$name $. $line";
292     }
293
294 For passing filehandles to functions, the easiest way is to 
295 preface them with a star, as in func(*STDIN).  See L<perlfaq7/"Passing
296 Filehandles"> for details.
297
298 If you want to create many anonymous handles, you should check out the
299 Symbol, FileHandle, or IO::Handle (etc.) modules.  Here's the equivalent
300 code with Symbol::gensym, which is reasonably light-weight:
301
302     foreach $filename (@names) {
303         use Symbol;
304         my $fh = gensym();
305         open($fh, "/etc/$filename") || die "open /etc/$filename: $!";
306         $file{$filename} = [ $i++, $fh ];
307     }
308
309 Or here using the semi-object-oriented FileHandle module, which certainly
310 isn't light-weight:
311
312     use FileHandle;
313
314     foreach $filename (@names) {
315         my $fh = FileHandle->new("/etc/$filename") or die "$filename: $!";
316         $file{$filename} = [ $i++, $fh ];
317     }
318
319 Please understand that whether the filehandle happens to be a (probably
320 localized) typeglob or an anonymous handle from one of the modules,
321 in no way affects the bizarre rules for managing indirect handles.
322 See the next question.
323
324 =head2 How can I use a filehandle indirectly?
325
326 An indirect filehandle is using something other than a symbol
327 in a place that a filehandle is expected.  Here are ways
328 to get those:
329
330     $fh =   SOME_FH;       # bareword is strict-subs hostile
331     $fh =  "SOME_FH";      # strict-refs hostile; same package only
332     $fh =  *SOME_FH;       # typeglob
333     $fh = \*SOME_FH;       # ref to typeglob (bless-able)
334     $fh =  *SOME_FH{IO};   # blessed IO::Handle from *SOME_FH typeglob
335
336 Or to use the C<new> method from the FileHandle or IO modules to
337 create an anonymous filehandle, store that in a scalar variable,
338 and use it as though it were a normal filehandle.
339
340     use FileHandle;
341     $fh = FileHandle->new();
342
343     use IO::Handle;                     # 5.004 or higher
344     $fh = IO::Handle->new();
345
346 Then use any of those as you would a normal filehandle.  Anywhere that
347 Perl is expecting a filehandle, an indirect filehandle may be used
348 instead. An indirect filehandle is just a scalar variable that contains
349 a filehandle.  Functions like C<print>, C<open>, C<seek>, or
350 the C<E<lt>FHE<gt>> diamond operator will accept either a read filehandle
351 or a scalar variable containing one:
352
353     ($ifh, $ofh, $efh) = (*STDIN, *STDOUT, *STDERR);
354     print $ofh "Type it: ";
355     $got = <$ifh>
356     print $efh "What was that: $got";
357
358 If you're passing a filehandle to a function, you can write
359 the function in two ways:
360
361     sub accept_fh {
362         my $fh = shift;
363         print $fh "Sending to indirect filehandle\n";
364     }
365
366 Or it can localize a typeglob and use the filehandle directly:
367
368     sub accept_fh {
369         local *FH = shift;
370         print  FH "Sending to localized filehandle\n";
371     }
372
373 Both styles work with either objects or typeglobs of real filehandles.
374 (They might also work with strings under some circumstances, but this
375 is risky.)
376
377     accept_fh(*STDOUT);
378     accept_fh($handle);
379
380 In the examples above, we assigned the filehandle to a scalar variable
381 before using it.  That is because only simple scalar variables,
382 not expressions or subscripts into hashes or arrays, can be used with
383 built-ins like C<print>, C<printf>, or the diamond operator.  These are
384 illegal and won't even compile:
385
386     @fd = (*STDIN, *STDOUT, *STDERR);
387     print $fd[1] "Type it: ";                           # WRONG
388     $got = <$fd[0]>                                     # WRONG
389     print $fd[2] "What was that: $got";                 # WRONG
390
391 With C<print> and C<printf>, you get around this by using a block and
392 an expression where you would place the filehandle:
393
394     print  { $fd[1] } "funny stuff\n";
395     printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
396     # Pity the poor deadbeef.
397
398 That block is a proper block like any other, so you can put more
399 complicated code there.  This sends the message out to one of two places:
400
401     $ok = -x "/bin/cat";                
402     print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
403     print { $fd[ 1+ ($ok || 0) ]  } "cat stat $ok\n";           
404
405 This approach of treating C<print> and C<printf> like object methods
406 calls doesn't work for the diamond operator.  That's because it's a
407 real operator, not just a function with a comma-less argument.  Assuming
408 you've been storing typeglobs in your structure as we did above, you
409 can use the built-in function named C<readline> to reads a record just
410 as C<E<lt>E<gt>> does.  Given the initialization shown above for @fd, this
411 would work, but only because readline() require a typeglob.  It doesn't
412 work with objects or strings, which might be a bug we haven't fixed yet.
413
414     $got = readline($fd[0]);
415
416 Let it be noted that the flakiness of indirect filehandles is not
417 related to whether they're strings, typeglobs, objects, or anything else.
418 It's the syntax of the fundamental operators.  Playing the object
419 game doesn't help you at all here.
420
421 =head2 How can I set up a footer format to be used with write()?
422
423 There's no builtin way to do this, but L<perlform> has a couple of
424 techniques to make it possible for the intrepid hacker.
425
426 =head2 How can I write() into a string?
427
428 See L<perlform/"Accessing Formatting Internals"> for an swrite() function.
429
430 =head2 How can I output my numbers with commas added?
431
432 This one will do it for you:
433
434     sub commify {
435         local $_  = shift;
436         1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
437         return $_;
438     }
439
440     $n = 23659019423.2331;
441     print "GOT: ", commify($n), "\n";
442
443     GOT: 23,659,019,423.2331
444
445 You can't just:
446
447     s/^([-+]?\d+)(\d{3})/$1,$2/g;
448
449 because you have to put the comma in and then recalculate your
450 position.
451
452 Alternatively, this commifies all numbers in a line regardless of
453 whether they have decimal portions, are preceded by + or -, or
454 whatever:
455
456     # from Andrew Johnson <ajohnson@gpu.srv.ualberta.ca>
457     sub commify {
458        my $input = shift;
459         $input = reverse $input;
460         $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;
461         return scalar reverse $input;
462     }
463
464 =head2 How can I translate tildes (~) in a filename?
465
466 Use the E<lt>E<gt> (glob()) operator, documented in L<perlfunc>.  This
467 requires that you have a shell installed that groks tildes, meaning
468 csh or tcsh or (some versions of) ksh, and thus may have portability
469 problems.  The Glob::KGlob module (available from CPAN) gives more
470 portable glob functionality.
471
472 Within Perl, you may use this directly:
473
474         $filename =~ s{
475           ^ ~             # find a leading tilde
476           (               # save this in $1
477               [^/]        # a non-slash character
478                     *     # repeated 0 or more times (0 means me)
479           )
480         }{
481           $1
482               ? (getpwnam($1))[7]
483               : ( $ENV{HOME} || $ENV{LOGDIR} )
484         }ex;
485
486 =head2 How come when I open a file read-write it wipes it out?
487
488 Because you're using something like this, which truncates the file and
489 I<then> gives you read-write access:
490
491     open(FH, "+> /path/name");          # WRONG (almost always)
492
493 Whoops.  You should instead use this, which will fail if the file
494 doesn't exist.  
495
496     open(FH, "+< /path/name");          # open for update
497
498 Using "E<gt>" always clobbers or creates.  Using "E<lt>" never does
499 either.  The "+" doesn't change this.
500
501 Here are examples of many kinds of file opens.  Those using sysopen()
502 all assume
503
504     use Fcntl;
505
506 To open file for reading:
507
508     open(FH, "< $path")                                 || die $!;
509     sysopen(FH, $path, O_RDONLY)                        || die $!;
510
511 To open file for writing, create new file if needed or else truncate old file:
512
513     open(FH, "> $path") || die $!;
514     sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT)        || die $!;
515     sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT, 0666)  || die $!;
516
517 To open file for writing, create new file, file must not exist:
518
519     sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT)         || die $!;
520     sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT, 0666)   || die $!;
521
522 To open file for appending, create if necessary:
523
524     open(FH, ">> $path") || die $!;
525     sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT)       || die $!;
526     sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT, 0666) || die $!;
527
528 To open file for appending, file must exist:
529
530     sysopen(FH, $path, O_WRONLY|O_APPEND)               || die $!;
531
532 To open file for update, file must exist:
533
534     open(FH, "+< $path")                                || die $!;
535     sysopen(FH, $path, O_RDWR)                          || die $!;
536
537 To open file for update, create file if necessary:
538
539     sysopen(FH, $path, O_RDWR|O_CREAT)                  || die $!;
540     sysopen(FH, $path, O_RDWR|O_CREAT, 0666)            || die $!;
541
542 To open file for update, file must not exist:
543
544     sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT)           || die $!;
545     sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT, 0666)     || die $!;
546
547 To open a file without blocking, creating if necessary:
548
549     sysopen(FH, "/tmp/somefile", O_WRONLY|O_NDELAY|O_CREAT)
550             or die "can't open /tmp/somefile: $!":
551
552 Be warned that neither creation nor deletion of files is guaranteed to
553 be an atomic operation over NFS.  That is, two processes might both
554 successful create or unlink the same file!  Therefore O_EXCL
555 isn't so exclusive as you might wish.
556
557 See also the new L<perlopentut> if you have it (new for 5.006).
558
559 =head2 Why do I sometimes get an "Argument list too long" when I use E<lt>*E<gt>?
560
561 The C<E<lt>E<gt>> operator performs a globbing operation (see above).
562 By default glob() forks csh(1) to do the actual glob expansion, but
563 csh can't handle more than 127 items and so gives the error message
564 C<Argument list too long>.  People who installed tcsh as csh won't
565 have this problem, but their users may be surprised by it.
566
567 To get around this, either do the glob yourself with readdir() and
568 patterns, or use a module like Glob::KGlob, one that doesn't use the
569 shell to do globbing.  This is expected to be fixed soon.
570
571 =head2 Is there a leak/bug in glob()?
572
573 Due to the current implementation on some operating systems, when you
574 use the glob() function or its angle-bracket alias in a scalar
575 context, you may cause a leak and/or unpredictable behavior.  It's
576 best therefore to use glob() only in list context.
577
578 =head2 How can I open a file with a leading "E<gt>" or trailing blanks?
579
580 Normally perl ignores trailing blanks in filenames, and interprets
581 certain leading characters (or a trailing "|") to mean something
582 special.  To avoid this, you might want to use a routine like this.
583 It makes incomplete pathnames into explicit relative ones, and tacks a
584 trailing null byte on the name to make perl leave it alone:
585
586     sub safe_filename {
587         local $_  = shift;
588         s#^([^./])#./$1#;
589         $_ .= "\0";
590         return $_;
591     }
592
593     $badpath = "<<<something really wicked   ";
594     $fn = safe_filename($badpath");
595     open(FH, "> $fn") or "couldn't open $badpath: $!";
596
597 This assumes that you are using POSIX (portable operating systems
598 interface) paths.  If you are on a closed, non-portable, proprietary
599 system, you may have to adjust the C<"./"> above.
600
601 It would be a lot clearer to use sysopen(), though:
602
603     use Fcntl;
604     $badpath = "<<<something really wicked   ";
605     open (FH, $badpath, O_WRONLY | O_CREAT | O_TRUNC)
606         or die "can't open $badpath: $!";
607
608 For more information, see also the new L<perlopentut> if you have it
609 (new for 5.006).
610
611 =head2 How can I reliably rename a file?
612
613 Well, usually you just use Perl's rename() function.  But that may not
614 work everywhere, in particular, renaming files across file systems.
615 Some sub-Unix systems have broken ports that corrupt the semantics of
616 rename() -- for example, WinNT does this right, but Win95 and Win98
617 are broken.  (The last two parts are not surprising, but the first is. :-)
618
619 If your operating system supports a proper mv(1) program or its moral
620 equivalent, this works:
621
622     rename($old, $new) or system("mv", $old, $new);
623
624 It may be more compelling to use the File::Copy module instead.  You
625 just copy to the new file to the new name (checking return values),
626 then delete the old one.  This isn't really the same semantics as a
627 real rename(), though, which preserves metainformation like
628 permissions, timestamps, inode info, etc.
629
630 The newer version of File::Copy exports a move() function.
631
632 =head2 How can I lock a file?
633
634 Perl's builtin flock() function (see L<perlfunc> for details) will call
635 flock(2) if that exists, fcntl(2) if it doesn't (on perl version 5.004 and
636 later), and lockf(3) if neither of the two previous system calls exists.
637 On some systems, it may even use a different form of native locking.
638 Here are some gotchas with Perl's flock():
639
640 =over 4
641
642 =item 1
643
644 Produces a fatal error if none of the three system calls (or their
645 close equivalent) exists.
646
647 =item 2
648
649 lockf(3) does not provide shared locking, and requires that the
650 filehandle be open for writing (or appending, or read/writing).
651
652 =item 3
653
654 Some versions of flock() can't lock files over a network (e.g. on NFS file
655 systems), so you'd need to force the use of fcntl(2) when you build Perl.
656 But even this is dubious at best.  See the flock entry of L<perlfunc>,
657 and the F<INSTALL> file in the source distribution for information on
658 building Perl to do this.
659
660 Two potentially non-obvious but traditional flock semantics are that
661 it waits indefinitely until the lock is granted, and that its locks
662 I<merely advisory>.  Such discretionary locks are more flexible, but
663 offer fewer guarantees.  This means that files locked with flock() may
664 be modified by programs that do not also use flock().  Cars that stop
665 for red lights get on well with each other, but not with cars that don't
666 stop for red lights.  See the perlport manpage, your port's specific
667 documentation, or your system-specific local manpages for details.  It's
668 best to assume traditional behavior if you're writing portable programs.
669 (But if you're not, you should as always feel perfectly free to write
670 for your own system's idiosyncrasies (sometimes called "features").
671 Slavish adherence to portability concerns shouldn't get in the way of
672 your getting your job done.)
673
674 For more information on file locking, see also L<perlopentut/"File
675 Locking"> if you have it (new for 5.006).
676
677 =back
678
679 =head2 Why can't I just open(FH, ">file.lock")?
680
681 A common bit of code B<NOT TO USE> is this:
682
683     sleep(3) while -e "file.lock";      # PLEASE DO NOT USE
684     open(LCK, "> file.lock");           # THIS BROKEN CODE
685
686 This is a classic race condition: you take two steps to do something
687 which must be done in one.  That's why computer hardware provides an
688 atomic test-and-set instruction.   In theory, this "ought" to work:
689
690     sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT)
691                 or die "can't open  file.lock: $!":
692
693 except that lamentably, file creation (and deletion) is not atomic
694 over NFS, so this won't work (at least, not every time) over the net.
695 Various schemes involving link() have been suggested, but
696 these tend to involve busy-wait, which is also subdesirable.
697
698 =head2 I still don't get locking.  I just want to increment the number in the file.  How can I do this?
699
700 Didn't anyone ever tell you web-page hit counters were useless?
701 They don't count number of hits, they're a waste of time, and they serve
702 only to stroke the writer's vanity.  Better to pick a random number.
703 It's more realistic.
704
705 Anyway, this is what you can do if you can't help yourself.
706
707     use Fcntl ':flock';
708     sysopen(FH, "numfile", O_RDWR|O_CREAT)       or die "can't open numfile: $!";
709     flock(FH, LOCK_EX)                           or die "can't flock numfile: $!";
710     $num = <FH> || 0;
711     seek(FH, 0, 0)                               or die "can't rewind numfile: $!";
712     truncate(FH, 0)                              or die "can't truncate numfile: $!";
713     (print FH $num+1, "\n")                      or die "can't write numfile: $!";
714     # Perl as of 5.004 automatically flushes before unlocking
715     flock(FH, LOCK_UN)                           or die "can't flock numfile: $!";
716     close FH                                     or die "can't close numfile: $!";
717
718 Here's a much better web-page hit counter:
719
720     $hits = int( (time() - 850_000_000) / rand(1_000) );
721
722 If the count doesn't impress your friends, then the code might.  :-)
723
724 =head2 How do I randomly update a binary file?
725
726 If you're just trying to patch a binary, in many cases something as
727 simple as this works:
728
729     perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs
730
731 However, if you have fixed sized records, then you might do something more
732 like this:
733
734     $RECSIZE = 220; # size of record, in bytes
735     $recno   = 37;  # which record to update
736     open(FH, "+<somewhere") || die "can't update somewhere: $!";
737     seek(FH, $recno * $RECSIZE, 0);
738     read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
739     # munge the record
740     seek(FH, -$RECSIZE, 1);
741     print FH $record;
742     close FH;
743
744 Locking and error checking are left as an exercise for the reader.
745 Don't forget them, or you'll be quite sorry.
746
747 =head2 How do I get a file's timestamp in perl?
748
749 If you want to retrieve the time at which the file was last read,
750 written, or had its meta-data (owner, etc) changed, you use the B<-M>,
751 B<-A>, or B<-C> filetest operations as documented in L<perlfunc>.  These
752 retrieve the age of the file (measured against the start-time of your
753 program) in days as a floating point number.  To retrieve the "raw"
754 time in seconds since the epoch, you would call the stat function,
755 then use localtime(), gmtime(), or POSIX::strftime() to convert this
756 into human-readable form.
757
758 Here's an example:
759
760     $write_secs = (stat($file))[9];
761     printf "file %s updated at %s\n", $file,
762         scalar localtime($write_secs);
763
764 If you prefer something more legible, use the File::stat module
765 (part of the standard distribution in version 5.004 and later):
766
767     # error checking left as an exercise for reader.
768     use File::stat;
769     use Time::localtime;
770     $date_string = ctime(stat($file)->mtime);
771     print "file $file updated at $date_string\n";
772
773 The POSIX::strftime() approach has the benefit of being,
774 in theory, independent of the current locale.  See L<perllocale>
775 for details.
776
777 =head2 How do I set a file's timestamp in perl?
778
779 You use the utime() function documented in L<perlfunc/utime>.
780 By way of example, here's a little program that copies the
781 read and write times from its first argument to all the rest
782 of them.
783
784     if (@ARGV < 2) {
785         die "usage: cptimes timestamp_file other_files ...\n";
786     }
787     $timestamp = shift;
788     ($atime, $mtime) = (stat($timestamp))[8,9];
789     utime $atime, $mtime, @ARGV;
790
791 Error checking is, as usual, left as an exercise for the reader.
792
793 Note that utime() currently doesn't work correctly with Win95/NT
794 ports.  A bug has been reported.  Check it carefully before using
795 it on those platforms.
796
797 =head2 How do I print to more than one file at once?
798
799 If you only have to do this once, you can do this:
800
801     for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
802
803 To connect up to one filehandle to several output filehandles, it's
804 easiest to use the tee(1) program if you have it, and let it take care
805 of the multiplexing:
806
807     open (FH, "| tee file1 file2 file3");
808
809 Or even:
810
811     # make STDOUT go to three files, plus original STDOUT
812     open (STDOUT, "| tee file1 file2 file3") or die "Teeing off: $!\n";
813     print "whatever\n"                       or die "Writing: $!\n";
814     close(STDOUT)                            or die "Closing: $!\n";
815
816 Otherwise you'll have to write your own multiplexing print
817 function -- or your own tee program -- or use Tom Christiansen's,
818 at http://www.perl.com/CPAN/authors/id/TOMC/scripts/tct.gz, which is
819 written in Perl and offers much greater functionality
820 than the stock version.
821
822 =head2 How can I read in an entire file all at once?
823
824 The customary Perl approach for processing all the lines in a file is to
825 do so one line at a time:
826
827     open (INPUT, $file)         || die "can't open $file: $!";
828     while (<INPUT>) {
829         chomp;
830         # do something with $_
831     } 
832     close(INPUT)                || die "can't close $file: $!";
833
834 This is tremendously more efficient than reading the entire file into
835 memory as an array of lines and then processing it one element at a time,
836 which is often -- if not almost always -- the wrong approach.  Whenever
837 you see someone do this:
838
839     @lines = <INPUT>;
840
841 You should think long and hard about why you need everything loaded
842 at once.  It's just not a scalable solution.  You might also find it
843 more fun to use the the standard DB_File module's $DB_RECNO bindings,
844 which allow you to tie an array to a file so that accessing an element
845 the array actually accesses the corresponding line in the file.
846
847 On very rare occasion, you may have an algorithm that demands that
848 the entire file be in memory at once as one scalar.  The simplest solution
849 to that is:
850
851     $var = `cat $file`;
852
853 Being in scalar context, you get the whole thing.  In list context,
854 you'd get a list of all the lines:
855
856     @lines = `cat $file`;
857
858 This tiny but expedient solution is neat, clean, and portable to all
859 systems that you've bothered to install decent tools on, even if you are
860 a Prisoner of Bill.  For those die-hards PoBs who've paid their billtax
861 and refuse to use the toolbox, or who like writing complicated code for
862 job security, you can of course read the file manually.
863
864     {
865         local(*INPUT, $/);
866         open (INPUT, $file)     || die "can't open $file: $!";
867         $var = <INPUT>;
868     }
869
870 That temporarily undefs your record separator, and will automatically 
871 close the file at block exit.  If the file is already open, just use this:
872
873     $var = do { local $/; <INPUT> };
874
875 =head2 How can I read in a file by paragraphs?
876
877 Use the C<$/> variable (see L<perlvar> for details).  You can either
878 set it to C<""> to eliminate empty paragraphs (C<"abc\n\n\n\ndef">,
879 for instance, gets treated as two paragraphs and not three), or
880 C<"\n\n"> to accept empty paragraphs.
881
882 Note that a blank line must have no blanks in it.  Thus C<"fred\n
883 \nstuff\n\n"> is one paragraph, but C<"fred\n\nstuff\n\n"> is two.
884
885 =head2 How can I read a single character from a file?  From the keyboard?
886
887 You can use the builtin C<getc()> function for most filehandles, but
888 it won't (easily) work on a terminal device.  For STDIN, either use
889 the Term::ReadKey module from CPAN, or use the sample code in
890 L<perlfunc/getc>.
891
892 If your system supports the portable operating system programming
893 interface (POSIX), you can use the following code, which you'll note
894 turns off echo processing as well.
895
896     #!/usr/bin/perl -w
897     use strict;
898     $| = 1;
899     for (1..4) {
900         my $got;
901         print "gimme: ";
902         $got = getone();
903         print "--> $got\n";
904     }
905     exit;
906
907     BEGIN {
908         use POSIX qw(:termios_h);
909
910         my ($term, $oterm, $echo, $noecho, $fd_stdin);
911
912         $fd_stdin = fileno(STDIN);
913
914         $term     = POSIX::Termios->new();
915         $term->getattr($fd_stdin);
916         $oterm     = $term->getlflag();
917
918         $echo     = ECHO | ECHOK | ICANON;
919         $noecho   = $oterm & ~$echo;
920
921         sub cbreak {
922             $term->setlflag($noecho);
923             $term->setcc(VTIME, 1);
924             $term->setattr($fd_stdin, TCSANOW);
925         }
926
927         sub cooked {
928             $term->setlflag($oterm);
929             $term->setcc(VTIME, 0);
930             $term->setattr($fd_stdin, TCSANOW);
931         }
932
933         sub getone {
934             my $key = '';
935             cbreak();
936             sysread(STDIN, $key, 1);
937             cooked();
938             return $key;
939         }
940
941     }
942
943     END { cooked() }
944
945 The Term::ReadKey module from CPAN may be easier to use.  Recent version
946 include also support for non-portable systems as well.
947
948     use Term::ReadKey;
949     open(TTY, "</dev/tty");
950     print "Gimme a char: ";
951     ReadMode "raw";
952     $key = ReadKey 0, *TTY;
953     ReadMode "normal";
954     printf "\nYou said %s, char number %03d\n",
955         $key, ord $key;
956
957 For legacy DOS systems, Dan Carson <dbc@tc.fluke.COM> reports the following:
958
959 To put the PC in "raw" mode, use ioctl with some magic numbers gleaned
960 from msdos.c (Perl source file) and Ralf Brown's interrupt list (comes
961 across the net every so often):
962
963     $old_ioctl = ioctl(STDIN,0,0);     # Gets device info
964     $old_ioctl &= 0xff;
965     ioctl(STDIN,1,$old_ioctl | 32);    # Writes it back, setting bit 5
966
967 Then to read a single character:
968
969     sysread(STDIN,$c,1);               # Read a single character
970
971 And to put the PC back to "cooked" mode:
972
973     ioctl(STDIN,1,$old_ioctl);         # Sets it back to cooked mode.
974
975 So now you have $c.  If C<ord($c) == 0>, you have a two byte code, which
976 means you hit a special key.  Read another byte with C<sysread(STDIN,$c,1)>,
977 and that value tells you what combination it was according to this
978 table:
979
980     # PC 2-byte keycodes = ^@ + the following:
981
982     # HEX     KEYS
983     # ---     ----
984     # 0F      SHF TAB
985     # 10-19   ALT QWERTYUIOP
986     # 1E-26   ALT ASDFGHJKL
987     # 2C-32   ALT ZXCVBNM
988     # 3B-44   F1-F10
989     # 47-49   HOME,UP,PgUp
990     # 4B      LEFT
991     # 4D      RIGHT
992     # 4F-53   END,DOWN,PgDn,Ins,Del
993     # 54-5D   SHF F1-F10
994     # 5E-67   CTR F1-F10
995     # 68-71   ALT F1-F10
996     # 73-77   CTR LEFT,RIGHT,END,PgDn,HOME
997     # 78-83   ALT 1234567890-=
998     # 84      CTR PgUp
999
1000 This is all trial and error I did a long time ago, I hope I'm reading the
1001 file that worked.
1002
1003 =head2 How can I tell whether there's a character waiting on a filehandle?
1004
1005 The very first thing you should do is look into getting the Term::ReadKey
1006 extension from CPAN.  As we mentioned earlier, it now even has limited
1007 support for non-portable (read: not open systems, closed, proprietary,
1008 not POSIX, not Unix, etc) systems.
1009
1010 You should also check out the Frequently Asked Questions list in
1011 comp.unix.* for things like this: the answer is essentially the same.
1012 It's very system dependent.  Here's one solution that works on BSD
1013 systems:
1014
1015     sub key_ready {
1016         my($rin, $nfd);
1017         vec($rin, fileno(STDIN), 1) = 1;
1018         return $nfd = select($rin,undef,undef,0);
1019     }
1020
1021 If you want to find out how many characters are waiting, there's
1022 also the FIONREAD ioctl call to be looked at.  The I<h2ph> tool that
1023 comes with Perl tries to convert C include files to Perl code, which
1024 can be C<require>d.  FIONREAD ends up defined as a function in the
1025 I<sys/ioctl.ph> file:
1026
1027     require 'sys/ioctl.ph';
1028
1029     $size = pack("L", 0);
1030     ioctl(FH, FIONREAD(), $size)    or die "Couldn't call ioctl: $!\n";
1031     $size = unpack("L", $size);
1032
1033 If I<h2ph> wasn't installed or doesn't work for you, you can
1034 I<grep> the include files by hand:
1035
1036     % grep FIONREAD /usr/include/*/*
1037     /usr/include/asm/ioctls.h:#define FIONREAD      0x541B
1038
1039 Or write a small C program using the editor of champions:
1040
1041     % cat > fionread.c
1042     #include <sys/ioctl.h>
1043     main() {
1044         printf("%#08x\n", FIONREAD);
1045     }
1046     ^D
1047     % cc -o fionread fionread.c
1048     % ./fionread
1049     0x4004667f
1050
1051 And then hard-code it, leaving porting as an exercise to your successor.
1052
1053     $FIONREAD = 0x4004667f;         # XXX: opsys dependent
1054
1055     $size = pack("L", 0);
1056     ioctl(FH, $FIONREAD, $size)     or die "Couldn't call ioctl: $!\n";
1057     $size = unpack("L", $size);
1058
1059 FIONREAD requires a filehandle connected to a stream, meaning sockets,
1060 pipes, and tty devices work, but I<not> files.
1061
1062 =head2 How do I do a C<tail -f> in perl?
1063
1064 First try
1065
1066     seek(GWFILE, 0, 1);
1067
1068 The statement C<seek(GWFILE, 0, 1)> doesn't change the current position,
1069 but it does clear the end-of-file condition on the handle, so that the
1070 next <GWFILE> makes Perl try again to read something.
1071
1072 If that doesn't work (it relies on features of your stdio implementation),
1073 then you need something more like this:
1074
1075         for (;;) {
1076           for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {
1077             # search for some stuff and put it into files
1078           }
1079           # sleep for a while
1080           seek(GWFILE, $curpos, 0);  # seek to where we had been
1081         }
1082
1083 If this still doesn't work, look into the POSIX module.  POSIX defines
1084 the clearerr() method, which can remove the end of file condition on a
1085 filehandle.  The method: read until end of file, clearerr(), read some
1086 more.  Lather, rinse, repeat.
1087
1088 There's also a File::Tail module from CPAN.
1089
1090 =head2 How do I dup() a filehandle in Perl?
1091
1092 If you check L<perlfunc/open>, you'll see that several of the ways
1093 to call open() should do the trick.  For example:
1094
1095     open(LOG, ">>/tmp/logfile");
1096     open(STDERR, ">&LOG");
1097
1098 Or even with a literal numeric descriptor:
1099
1100    $fd = $ENV{MHCONTEXTFD};
1101    open(MHCONTEXT, "<&=$fd");   # like fdopen(3S)
1102
1103 Note that "E<lt>&STDIN" makes a copy, but "E<lt>&=STDIN" make
1104 an alias.  That means if you close an aliased handle, all
1105 aliases become inaccessible.  This is not true with 
1106 a copied one.
1107
1108 Error checking, as always, has been left as an exercise for the reader.
1109
1110 =head2 How do I close a file descriptor by number?
1111
1112 This should rarely be necessary, as the Perl close() function is to be
1113 used for things that Perl opened itself, even if it was a dup of a
1114 numeric descriptor, as with MHCONTEXT above.  But if you really have
1115 to, you may be able to do this:
1116
1117     require 'sys/syscall.ph';
1118     $rc = syscall(&SYS_close, $fd + 0);  # must force numeric
1119     die "can't sysclose $fd: $!" unless $rc == -1;
1120
1121 Or just use the fdopen(3S) feature of open():
1122
1123     { 
1124         local *F; 
1125         open F, "<&=$fd" or die "Cannot reopen fd=$fd: $!";
1126         close F;
1127     }
1128
1129 =head2 Why can't I use "C:\temp\foo" in DOS paths?  What doesn't `C:\temp\foo.exe` work?
1130
1131 Whoops!  You just put a tab and a formfeed into that filename!
1132 Remember that within double quoted strings ("like\this"), the
1133 backslash is an escape character.  The full list of these is in
1134 L<perlop/Quote and Quote-like Operators>.  Unsurprisingly, you don't
1135 have a file called "c:(tab)emp(formfeed)oo" or
1136 "c:(tab)emp(formfeed)oo.exe" on your legacy DOS filesystem.
1137
1138 Either single-quote your strings, or (preferably) use forward slashes.
1139 Since all DOS and Windows versions since something like MS-DOS 2.0 or so
1140 have treated C</> and C<\> the same in a path, you might as well use the
1141 one that doesn't clash with Perl -- or the POSIX shell, ANSI C and C++,
1142 awk, Tcl, Java, or Python, just to mention a few.  POSIX paths
1143 are more portable, too.
1144
1145 =head2 Why doesn't glob("*.*") get all the files?
1146
1147 Because even on non-Unix ports, Perl's glob function follows standard
1148 Unix globbing semantics.  You'll need C<glob("*")> to get all (non-hidden)
1149 files.  This makes glob() portable even to legacy systems.  Your
1150 port may include proprietary globbing functions as well.  Check its
1151 documentation for details.
1152
1153 =head2 Why does Perl let me delete read-only files?  Why does C<-i> clobber protected files?  Isn't this a bug in Perl?
1154
1155 This is elaborately and painstakingly described in the "Far More Than
1156 You Ever Wanted To Know" in
1157 http://www.perl.com/CPAN/doc/FMTEYEWTK/file-dir-perms .
1158
1159 The executive summary: learn how your filesystem works.  The
1160 permissions on a file say what can happen to the data in that file.
1161 The permissions on a directory say what can happen to the list of
1162 files in that directory.  If you delete a file, you're removing its
1163 name from the directory (so the operation depends on the permissions
1164 of the directory, not of the file).  If you try to write to the file,
1165 the permissions of the file govern whether you're allowed to.
1166
1167 =head2 How do I select a random line from a file?
1168
1169 Here's an algorithm from the Camel Book:
1170
1171     srand;
1172     rand($.) < 1 && ($line = $_) while <>;
1173
1174 This has a significant advantage in space over reading the whole
1175 file in.  A simple proof by induction is available upon 
1176 request if you doubt its correctness.
1177
1178 =head2 Why do I get weird spaces when I print an array of lines?
1179
1180 Saying
1181
1182     print "@lines\n";
1183
1184 joins together the elements of C<@lines> with a space between them.
1185 If C<@lines> were C<("little", "fluffy", "clouds")> then the above
1186 statement would print:
1187
1188     little fluffy clouds
1189
1190 but if each element of C<@lines> was a line of text, ending a newline
1191 character C<("little\n", "fluffy\n", "clouds\n")> then it would print:
1192
1193     little
1194      fluffy
1195      clouds
1196
1197 If your array contains lines, just print them:
1198
1199     print @lines;
1200
1201 =head1 AUTHOR AND COPYRIGHT
1202
1203 Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington.
1204 All rights reserved.
1205
1206 When included as an integrated part of the Standard Distribution
1207 of Perl or of its documentation (printed or otherwise), this works is
1208 covered under Perl's Artistic License.  For separate distributions of
1209 all or part of this FAQ outside of that, see L<perlfaq>.
1210
1211 Irrespective of its distribution, all code examples here are public
1212 domain.  You are permitted and encouraged to use this code and any
1213 derivatives thereof in your own programs for fun or for profit as you
1214 see fit.  A simple comment in the code giving credit to the FAQ would
1215 be courteous but is not required.