1PERLFAQ5(1)            Perl Programmers Reference Guide            PERLFAQ5(1)
2
3
4

NAME

6       perlfaq5 - Files and Formats ($Revision: 1.42 $, $Date: 2005/12/31
7       00:54:37 $)
8

DESCRIPTION

10       This section deals with I/O and the "f" issues: filehandles, flushing,
11       formats, and footers.
12
13       How do I flush/unbuffer an output filehandle?  Why must I do this?
14
15       Perl does not support truly unbuffered output (except insofar as you
16       can "syswrite(OUT, $char, 1)"), although it does support is "command
17       buffering", in which a physical write is performed after every output
18       command.
19
20       The C standard I/O library (stdio) normally buffers characters sent to
21       devices so that there isn't a system call for each byte. In most stdio
22       implementations, the type of output buffering and the size of the buf‐
23       fer varies according to the type of device. Perl's print() and write()
24       functions normally buffer output, while syswrite() bypasses buffering
25       all together.
26
27       If you want your output to be sent immediately when you execute print()
28       or write() (for instance, for some network protocols), you must set the
29       handle's autoflush flag. This flag is the Perl variable $⎪ and when it
30       is set to a true value, Perl will flush the handle's buffer after each
31       print() or write(). Setting $⎪ affects buffering only for the currently
32       selected default file handle. You choose this handle with the one argu‐
33       ment select() call (see "$⎪" in perlvar and "select" in perlfunc).
34
35       Use select() to choose the desired handle, then set its per-filehandle
36       variables.
37
38           $old_fh = select(OUTPUT_HANDLE);
39           $⎪ = 1;
40           select($old_fh);
41
42       Some idioms can handle this in a single statement:
43
44           select((select(OUTPUT_HANDLE), $⎪ = 1)[0]);
45
46           $⎪ = 1, select $_ for select OUTPUT_HANDLE;
47
48       Some modules offer object-oriented access to handles and their vari‐
49       ables, although they may be overkill if this is the only thing you do
50       with them.  You can use IO::Handle:
51
52           use IO::Handle;
53           open(DEV, ">/dev/printer");   # but is this?
54           DEV->autoflush(1);
55
56       or IO::Socket:
57
58           use IO::Socket;               # this one is kinda a pipe?
59               my $sock = IO::Socket::INET->new( 'www.example.com:80' );
60
61           $sock->autoflush();
62
63       How do I change one line in a file/delete a line in a file/insert a
64       line in the middle of a file/append to the beginning of a file?
65
66       Use the Tie::File module, which is included in the standard distribu‐
67       tion since Perl 5.8.0.
68
69       How do I count the number of lines in a file?
70
71       One fairly efficient way is to count newlines in the file. The follow‐
72       ing program uses a feature of tr///, as documented in perlop.  If your
73       text file doesn't end with a newline, then it's not really a proper
74       text file, so this may report one fewer line than you expect.
75
76           $lines = 0;
77           open(FILE, $filename) or die "Can't open `$filename': $!";
78           while (sysread FILE, $buffer, 4096) {
79               $lines += ($buffer =~ tr/\n//);
80           }
81           close FILE;
82
83       This assumes no funny games with newline translations.
84
85       How can I use Perl's "-i" option from within a program?
86
87       "-i" sets the value of Perl's $^I variable, which in turn affects the
88       behavior of "<>"; see perlrun for more details.  By modifying the
89       appropriate variables directly, you can get the same behavior within a
90       larger program.  For example:
91
92            # ...
93            {
94               local($^I, @ARGV) = ('.orig', glob("*.c"));
95               while (<>) {
96                  if ($. == 1) {
97                      print "This line should appear at the top of each file\n";
98                  }
99                  s/\b(p)earl\b/${1}erl/i;        # Correct typos, preserving case
100                  print;
101                  close ARGV if eof;              # Reset $.
102               }
103            }
104            # $^I and @ARGV return to their old values here
105
106       This block modifies all the ".c" files in the current directory, leav‐
107       ing a backup of the original data from each file in a new ".c.orig"
108       file.
109
110       How can I copy a file?
111
112       (contributed by brian d foy)
113
114       Use the File::Copy module. It comes with Perl and can do a true copy
115       across file systems, and it does its magic in a portable fashion.
116
117               use File::Copy;
118
119               copy( $original, $new_copy ) or die "Copy failed: $!";
120
121       If you can't use File::Copy, you'll have to do the work yourself: open
122       the original file, open the destination file, then print to the desti‐
123       nation file as you read the original.
124
125       How do I make a temporary file name?
126
127       If you don't need to know the name of the file, you can use "open()"
128       with "undef" in place of the file name.  The "open()" function creates
129       an anonymous temporary file.
130
131               open my $tmp, '+>', undef or die $!;
132
133       Otherwise, you can use the File::Temp module.
134
135         use File::Temp qw/ tempfile tempdir /;
136
137         $dir = tempdir( CLEANUP => 1 );
138         ($fh, $filename) = tempfile( DIR => $dir );
139
140         # or if you don't need to know the filename
141
142         $fh = tempfile( DIR => $dir );
143
144       The File::Temp has been a standard module since Perl 5.6.1.  If you
145       don't have a modern enough Perl installed, use the "new_tmpfile" class
146       method from the IO::File module to get a filehandle opened for reading
147       and writing.  Use it if you don't need to know the file's name:
148
149           use IO::File;
150           $fh = IO::File->new_tmpfile()
151               or die "Unable to make new temporary file: $!";
152
153       If you're committed to creating a temporary file by hand, use the
154       process ID and/or the current time-value.  If you need to have many
155       temporary files in one process, use a counter:
156
157           BEGIN {
158               use Fcntl;
159               my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMPDIR} ⎪⎪ $ENV{TEMP};
160               my $base_name = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
161               sub temp_file {
162                   local *FH;
163                   my $count = 0;
164                   until (defined(fileno(FH)) ⎪⎪ $count++ > 100) {
165                       $base_name =~ s/-(\d+)$/"-" . (1 + $1)/e;
166                       # O_EXCL is required for security reasons.
167                       sysopen(FH, $base_name, O_WRONLY⎪O_EXCL⎪O_CREAT);
168                   }
169                   if (defined(fileno(FH))
170                       return (*FH, $base_name);
171                   } else {
172                       return ();
173                   }
174               }
175           }
176
177       How can I manipulate fixed-record-length files?
178
179       The most efficient way is using pack() and unpack().  This is faster
180       than using substr() when taking many, many strings.  It is slower for
181       just a few.
182
183       Here is a sample chunk of code to break up and put back together again
184       some fixed-format input lines, in this case from the output of a nor‐
185       mal, Berkeley-style ps:
186
187           # sample input line:
188           #   15158 p5  T      0:00 perl /home/tchrist/scripts/now-what
189           my $PS_T = 'A6 A4 A7 A5 A*';
190           open my $ps, '-⎪', 'ps';
191           print scalar <$ps>;
192           my @fields = qw( pid tt stat time command );
193           while (<$ps>) {
194               my %process;
195               @process{@fields} = unpack($PS_T, $_);
196               for my $field ( @fields ) {
197                   print "$field: <$process{$field}>\n";
198               }
199               print 'line=', pack($PS_T, @process{@fields} ), "\n";
200           }
201
202       We've used a hash slice in order to easily handle the fields of each
203       row.  Storing the keys in an array means it's easy to operate on them
204       as a group or loop over them with for. It also avoids polluting the
205       program with global variables and using symbolic references.
206
207       How can I make a filehandle local to a subroutine?  How do I pass file‐
208       handles between subroutines?  How do I make an array of filehandles?
209
210       As of perl5.6, open() autovivifies file and directory handles as refer‐
211       ences if you pass it an uninitialized scalar variable.  You can then
212       pass these references just like any other scalar, and use them in the
213       place of named handles.
214
215               open my    $fh, $file_name;
216
217               open local $fh, $file_name;
218
219               print $fh "Hello World!\n";
220
221               process_file( $fh );
222
223       Before perl5.6, you had to deal with various typeglob idioms which you
224       may see in older code.
225
226               open FILE, "> $filename";
227               process_typeglob(   *FILE );
228               process_reference( \*FILE );
229
230               sub process_typeglob  { local *FH = shift; print FH  "Typeglob!" }
231               sub process_reference { local $fh = shift; print $fh "Reference!" }
232
233       If you want to create many anonymous handles, you should check out the
234       Symbol or IO::Handle modules.
235
236       How can I use a filehandle indirectly?
237
238       An indirect filehandle is using something other than a symbol in a
239       place that a filehandle is expected.  Here are ways to get indirect
240       filehandles:
241
242           $fh =   SOME_FH;       # bareword is strict-subs hostile
243           $fh =  "SOME_FH";      # strict-refs hostile; same package only
244           $fh =  *SOME_FH;       # typeglob
245           $fh = \*SOME_FH;       # ref to typeglob (bless-able)
246           $fh =  *SOME_FH{IO};   # blessed IO::Handle from *SOME_FH typeglob
247
248       Or, you can use the "new" method from one of the IO::* modules to cre‐
249       ate an anonymous filehandle, store that in a scalar variable, and use
250       it as though it were a normal filehandle.
251
252           use IO::Handle;                     # 5.004 or higher
253           $fh = IO::Handle->new();
254
255       Then use any of those as you would a normal filehandle.  Anywhere that
256       Perl is expecting a filehandle, an indirect filehandle may be used
257       instead. An indirect filehandle is just a scalar variable that contains
258       a filehandle.  Functions like "print", "open", "seek", or the "<FH>"
259       diamond operator will accept either a named filehandle or a scalar
260       variable containing one:
261
262           ($ifh, $ofh, $efh) = (*STDIN, *STDOUT, *STDERR);
263           print $ofh "Type it: ";
264           $got = <$ifh>
265           print $efh "What was that: $got";
266
267       If you're passing a filehandle to a function, you can write the func‐
268       tion in two ways:
269
270           sub accept_fh {
271               my $fh = shift;
272               print $fh "Sending to indirect filehandle\n";
273           }
274
275       Or it can localize a typeglob and use the filehandle directly:
276
277           sub accept_fh {
278               local *FH = shift;
279               print  FH "Sending to localized filehandle\n";
280           }
281
282       Both styles work with either objects or typeglobs of real filehandles.
283       (They might also work with strings under some circumstances, but this
284       is risky.)
285
286           accept_fh(*STDOUT);
287           accept_fh($handle);
288
289       In the examples above, we assigned the filehandle to a scalar variable
290       before using it.  That is because only simple scalar variables, not
291       expressions or subscripts of hashes or arrays, can be used with built-
292       ins like "print", "printf", or the diamond operator.  Using something
293       other than a simple scalar variable as a filehandle is illegal and
294       won't even compile:
295
296           @fd = (*STDIN, *STDOUT, *STDERR);
297           print $fd[1] "Type it: ";                           # WRONG
298           $got = <$fd[0]>                                     # WRONG
299           print $fd[2] "What was that: $got";                 # WRONG
300
301       With "print" and "printf", you get around this by using a block and an
302       expression where you would place the filehandle:
303
304           print  { $fd[1] } "funny stuff\n";
305           printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
306           # Pity the poor deadbeef.
307
308       That block is a proper block like any other, so you can put more com‐
309       plicated code there.  This sends the message out to one of two places:
310
311           $ok = -x "/bin/cat";
312           print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
313           print { $fd[ 1+ ($ok ⎪⎪ 0) ]  } "cat stat $ok\n";
314
315       This approach of treating "print" and "printf" like object methods
316       calls doesn't work for the diamond operator.  That's because it's a
317       real operator, not just a function with a comma-less argument.  Assum‐
318       ing you've been storing typeglobs in your structure as we did above,
319       you can use the built-in function named "readline" to read a record
320       just as "<>" does.  Given the initialization shown above for @fd, this
321       would work, but only because readline() requires a typeglob.  It
322       doesn't work with objects or strings, which might be a bug we haven't
323       fixed yet.
324
325           $got = readline($fd[0]);
326
327       Let it be noted that the flakiness of indirect filehandles is not
328       related to whether they're strings, typeglobs, objects, or anything
329       else.  It's the syntax of the fundamental operators.  Playing the
330       object game doesn't help you at all here.
331
332       How can I set up a footer format to be used with write()?
333
334       There's no builtin way to do this, but perlform has a couple of tech‐
335       niques to make it possible for the intrepid hacker.
336
337       How can I write() into a string?
338
339       See "Accessing Formatting Internals" in perlform for an swrite() func‐
340       tion.
341
342       How can I output my numbers with commas added?
343
344       (contributed by brian d foy and Benjamin Goldberg)
345
346       You can use Number::Format to separate places in a number.  It handles
347       locale information for those of you who want to insert full stops
348       instead (or anything else that they want to use, really).
349
350       This subroutine will add commas to your number:
351
352               sub commify {
353                  local $_  = shift;
354                  1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
355                  return $_;
356                  }
357
358       This regex from Benjamin Goldberg will add commas to numbers:
359
360          s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))⎪\G\d{3}(?=\d))/$1,/g;
361
362       It is easier to see with comments:
363
364          s/(
365              ^[-+]?            # beginning of number.
366              \d+?              # first digits before first comma
367              (?=               # followed by, (but not included in the match) :
368                 (?>(?:\d{3})+) # some positive multiple of three digits.
369                 (?!\d)         # an *exact* multiple, not x * 3 + 1 or whatever.
370              )
371             ⎪                  # or:
372              \G\d{3}           # after the last group, get three digits
373              (?=\d)            # but they have to have more digits after them.
374          )/$1,/xg;
375
376       How can I translate tildes (~) in a filename?
377
378       Use the <> (glob()) operator, documented in perlfunc.  Older versions
379       of Perl require that you have a shell installed that groks tildes.
380       Recent perl versions have this feature built in. The File::KGlob module
381       (available from CPAN) gives more portable glob functionality.
382
383       Within Perl, you may use this directly:
384
385               $filename =~ s{
386                 ^ ~             # find a leading tilde
387                 (               # save this in $1
388                     [^/]        # a non-slash character
389                           *     # repeated 0 or more times (0 means me)
390                 )
391               }{
392                 $1
393                     ? (getpwnam($1))[7]
394                     : ( $ENV{HOME} ⎪⎪ $ENV{LOGDIR} )
395               }ex;
396
397       How come when I open a file read-write it wipes it out?
398
399       Because you're using something like this, which truncates the file and
400       then gives you read-write access:
401
402           open(FH, "+> /path/name");          # WRONG (almost always)
403
404       Whoops.  You should instead use this, which will fail if the file
405       doesn't exist.
406
407           open(FH, "+< /path/name");          # open for update
408
409       Using ">" always clobbers or creates.  Using "<" never does either.
410       The "+" doesn't change this.
411
412       Here are examples of many kinds of file opens.  Those using sysopen()
413       all assume
414
415           use Fcntl;
416
417       To open file for reading:
418
419           open(FH, "< $path")                                 ⎪⎪ die $!;
420           sysopen(FH, $path, O_RDONLY)                        ⎪⎪ die $!;
421
422       To open file for writing, create new file if needed or else truncate
423       old file:
424
425           open(FH, "> $path") ⎪⎪ die $!;
426           sysopen(FH, $path, O_WRONLY⎪O_TRUNC⎪O_CREAT)        ⎪⎪ die $!;
427           sysopen(FH, $path, O_WRONLY⎪O_TRUNC⎪O_CREAT, 0666)  ⎪⎪ die $!;
428
429       To open file for writing, create new file, file must not exist:
430
431           sysopen(FH, $path, O_WRONLY⎪O_EXCL⎪O_CREAT)         ⎪⎪ die $!;
432           sysopen(FH, $path, O_WRONLY⎪O_EXCL⎪O_CREAT, 0666)   ⎪⎪ die $!;
433
434       To open file for appending, create if necessary:
435
436           open(FH, ">> $path") ⎪⎪ die $!;
437           sysopen(FH, $path, O_WRONLY⎪O_APPEND⎪O_CREAT)       ⎪⎪ die $!;
438           sysopen(FH, $path, O_WRONLY⎪O_APPEND⎪O_CREAT, 0666) ⎪⎪ die $!;
439
440       To open file for appending, file must exist:
441
442           sysopen(FH, $path, O_WRONLY⎪O_APPEND)               ⎪⎪ die $!;
443
444       To open file for update, file must exist:
445
446           open(FH, "+< $path")                                ⎪⎪ die $!;
447           sysopen(FH, $path, O_RDWR)                          ⎪⎪ die $!;
448
449       To open file for update, create file if necessary:
450
451           sysopen(FH, $path, O_RDWR⎪O_CREAT)                  ⎪⎪ die $!;
452           sysopen(FH, $path, O_RDWR⎪O_CREAT, 0666)            ⎪⎪ die $!;
453
454       To open file for update, file must not exist:
455
456           sysopen(FH, $path, O_RDWR⎪O_EXCL⎪O_CREAT)           ⎪⎪ die $!;
457           sysopen(FH, $path, O_RDWR⎪O_EXCL⎪O_CREAT, 0666)     ⎪⎪ die $!;
458
459       To open a file without blocking, creating if necessary:
460
461           sysopen(FH, "/foo/somefile", O_WRONLY⎪O_NDELAY⎪O_CREAT)
462                   or die "can't open /foo/somefile: $!":
463
464       Be warned that neither creation nor deletion of files is guaranteed to
465       be an atomic operation over NFS.  That is, two processes might both
466       successfully create or unlink the same file!  Therefore O_EXCL isn't as
467       exclusive as you might wish.
468
469       See also the new perlopentut if you have it (new for 5.6).
470
471       Why do I sometimes get an "Argument list too long" when I use <*>?
472
473       The "<>" operator performs a globbing operation (see above).  In Perl
474       versions earlier than v5.6.0, the internal glob() operator forks csh(1)
475       to do the actual glob expansion, but csh can't handle more than 127
476       items and so gives the error message "Argument list too long".  People
477       who installed tcsh as csh won't have this problem, but their users may
478       be surprised by it.
479
480       To get around this, either upgrade to Perl v5.6.0 or later, do the glob
481       yourself with readdir() and patterns, or use a module like File::KGlob,
482       one that doesn't use the shell to do globbing.
483
484       Is there a leak/bug in glob()?
485
486       Due to the current implementation on some operating systems, when you
487       use the glob() function or its angle-bracket alias in a scalar context,
488       you may cause a memory leak and/or unpredictable behavior.  It's best
489       therefore to use glob() only in list context.
490
491       How can I open a file with a leading ">" or trailing blanks?
492
493       (contributed by Brian McCauley)
494
495       The special two argument form of Perl's open() function ignores trail‐
496       ing blanks in filenames and infers the mode from certain leading char‐
497       acters (or a trailing "⎪"). In older versions of Perl this was the only
498       version of open() and so it is prevalent in old code and books.
499
500       Unless you have a particular reason to use the two argument form you
501       should use the three argument form of open() which does not treat any
502       charcters in the filename as special.
503
504               open FILE, "<", "  file  ";  # filename is "   file   "
505               open FILE, ">", ">file";     # filename is ">file"
506
507       How can I reliably rename a file?
508
509       If your operating system supports a proper mv(1) utility or its func‐
510       tional equivalent, this works:
511
512           rename($old, $new) or system("mv", $old, $new);
513
514       It may be more portable to use the File::Copy module instead.  You just
515       copy to the new file to the new name (checking return values), then
516       delete the old one.  This isn't really the same semantically as a
517       rename(), which preserves meta-information like permissions, time‐
518       stamps, inode info, etc.
519
520       Newer versions of File::Copy export a move() function.
521
522       How can I lock a file?
523
524       Perl's builtin flock() function (see perlfunc for details) will call
525       flock(2) if that exists, fcntl(2) if it doesn't (on perl version 5.004
526       and later), and lockf(3) if neither of the two previous system calls
527       exists.  On some systems, it may even use a different form of native
528       locking.  Here are some gotchas with Perl's flock():
529
530       1   Produces a fatal error if none of the three system calls (or their
531           close equivalent) exists.
532
533       2   lockf(3) does not provide shared locking, and requires that the
534           filehandle be open for writing (or appending, or read/writing).
535
536       3   Some versions of flock() can't lock files over a network (e.g. on
537           NFS file systems), so you'd need to force the use of fcntl(2) when
538           you build Perl.  But even this is dubious at best.  See the flock
539           entry of perlfunc and the INSTALL file in the source distribution
540           for information on building Perl to do this.
541
542           Two potentially non-obvious but traditional flock semantics are
543           that it waits indefinitely until the lock is granted, and that its
544           locks are merely advisory.  Such discretionary locks are more flex‐
545           ible, but offer fewer guarantees.  This means that files locked
546           with flock() may be modified by programs that do not also use
547           flock().  Cars that stop for red lights get on well with each
548           other, but not with cars that don't stop for red lights.  See the
549           perlport manpage, your port's specific documentation, or your sys‐
550           tem-specific local manpages for details.  It's best to assume tra‐
551           ditional behavior if you're writing portable programs.  (If you're
552           not, you should as always feel perfectly free to write for your own
553           system's idiosyncrasies (sometimes called "features").  Slavish
554           adherence to portability concerns shouldn't get in the way of your
555           getting your job done.)
556
557           For more information on file locking, see also "File Locking" in
558           perlopentut if you have it (new for 5.6).
559
560       Why can't I just open(FH, ">file.lock")?
561
562       A common bit of code NOT TO USE is this:
563
564           sleep(3) while -e "file.lock";      # PLEASE DO NOT USE
565           open(LCK, "> file.lock");           # THIS BROKEN CODE
566
567       This is a classic race condition: you take two steps to do something
568       which must be done in one.  That's why computer hardware provides an
569       atomic test-and-set instruction.   In theory, this "ought" to work:
570
571           sysopen(FH, "file.lock", O_WRONLY⎪O_EXCL⎪O_CREAT)
572                       or die "can't open  file.lock: $!";
573
574       except that lamentably, file creation (and deletion) is not atomic over
575       NFS, so this won't work (at least, not every time) over the net.  Vari‐
576       ous schemes involving link() have been suggested, but these tend to
577       involve busy-wait, which is also subdesirable.
578
579       I still don't get locking.  I just want to increment the number in the
580       file.  How can I do this?
581
582       Didn't anyone ever tell you web-page hit counters were useless?  They
583       don't count number of hits, they're a waste of time, and they serve
584       only to stroke the writer's vanity.  It's better to pick a random num‐
585       ber; they're more realistic.
586
587       Anyway, this is what you can do if you can't help yourself.
588
589           use Fcntl qw(:DEFAULT :flock);
590           sysopen(FH, "numfile", O_RDWR⎪O_CREAT)       or die "can't open numfile: $!";
591           flock(FH, LOCK_EX)                           or die "can't flock numfile: $!";
592           $num = <FH> ⎪⎪ 0;
593           seek(FH, 0, 0)                               or die "can't rewind numfile: $!";
594           truncate(FH, 0)                              or die "can't truncate numfile: $!";
595           (print FH $num+1, "\n")                      or die "can't write numfile: $!";
596           close FH                                     or die "can't close numfile: $!";
597
598       Here's a much better web-page hit counter:
599
600           $hits = int( (time() - 850_000_000) / rand(1_000) );
601
602       If the count doesn't impress your friends, then the code might.  :-)
603
604       All I want to do is append a small amount of text to the end of a file.
605       Do I still have to use locking?
606
607       If you are on a system that correctly implements flock() and you use
608       the example appending code from "perldoc -f flock" everything will be
609       OK even if the OS you are on doesn't implement append mode correctly
610       (if such a system exists.) So if you are happy to restrict yourself to
611       OSs that implement flock() (and that's not really much of a restric‐
612       tion) then that is what you should do.
613
614       If you know you are only going to use a system that does correctly
615       implement appending (i.e. not Win32) then you can omit the seek() from
616       the above code.
617
618       If you know you are only writing code to run on an OS and filesystem
619       that does implement append mode correctly (a local filesystem on a mod‐
620       ern Unix for example), and you keep the file in block-buffered mode and
621       you write less than one buffer-full of output between each manual
622       flushing of the buffer then each bufferload is almost guaranteed to be
623       written to the end of the file in one chunk without getting intermin‐
624       gled with anyone else's output. You can also use the syswrite() func‐
625       tion which is simply a wrapper around your systems write(2) system
626       call.
627
628       There is still a small theoretical chance that a signal will interrupt
629       the system level write() operation before completion.  There is also a
630       possibility that some STDIO implementations may call multiple system
631       level write()s even if the buffer was empty to start.  There may be
632       some systems where this probability is reduced to zero.
633
634       How do I randomly update a binary file?
635
636       If you're just trying to patch a binary, in many cases something as
637       simple as this works:
638
639           perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs
640
641       However, if you have fixed sized records, then you might do something
642       more like this:
643
644           $RECSIZE = 220; # size of record, in bytes
645           $recno   = 37;  # which record to update
646           open(FH, "+<somewhere") ⎪⎪ die "can't update somewhere: $!";
647           seek(FH, $recno * $RECSIZE, 0);
648           read(FH, $record, $RECSIZE) == $RECSIZE ⎪⎪ die "can't read record $recno: $!";
649           # munge the record
650           seek(FH, -$RECSIZE, 1);
651           print FH $record;
652           close FH;
653
654       Locking and error checking are left as an exercise for the reader.
655       Don't forget them or you'll be quite sorry.
656
657       How do I get a file's timestamp in perl?
658
659       If you want to retrieve the time at which the file was last read, writ‐
660       ten, or had its meta-data (owner, etc) changed, you use the -A, -M, or
661       -C file test operations as documented in perlfunc.  These retrieve the
662       age of the file (measured against the start-time of your program) in
663       days as a floating point number. Some platforms may not have all of
664       these times.  See perlport for details. To retrieve the "raw" time in
665       seconds since the epoch, you would call the stat function, then use
666       localtime(), gmtime(), or POSIX::strftime() to convert this into human-
667       readable form.
668
669       Here's an example:
670
671           $write_secs = (stat($file))[9];
672           printf "file %s updated at %s\n", $file,
673               scalar localtime($write_secs);
674
675       If you prefer something more legible, use the File::stat module (part
676       of the standard distribution in version 5.004 and later):
677
678           # error checking left as an exercise for reader.
679           use File::stat;
680           use Time::localtime;
681           $date_string = ctime(stat($file)->mtime);
682           print "file $file updated at $date_string\n";
683
684       The POSIX::strftime() approach has the benefit of being, in theory,
685       independent of the current locale.  See perllocale for details.
686
687       How do I set a file's timestamp in perl?
688
689       You use the utime() function documented in "utime" in perlfunc.  By way
690       of example, here's a little program that copies the read and write
691       times from its first argument to all the rest of them.
692
693           if (@ARGV < 2) {
694               die "usage: cptimes timestamp_file other_files ...\n";
695           }
696           $timestamp = shift;
697           ($atime, $mtime) = (stat($timestamp))[8,9];
698           utime $atime, $mtime, @ARGV;
699
700       Error checking is, as usual, left as an exercise for the reader.
701
702       The perldoc for utime also has an example that has the same effect as
703       touch(1) on files that already exist.
704
705       Certain file systems have a limited ability to store the times on a
706       file at the expected level of precision.  For example, the FAT and HPFS
707       filesystem are unable to create dates on files with a finer granularity
708       than two seconds.  This is a limitation of the filesystems, not of
709       utime().
710
711       How do I print to more than one file at once?
712
713       To connect one filehandle to several output filehandles, you can use
714       the IO::Tee or Tie::FileHandle::Multiplex modules.
715
716       If you only have to do this once, you can print individually to each
717       filehandle.
718
719           for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
720
721       How can I read in an entire file all at once?
722
723       You can use the File::Slurp module to do it in one step.
724
725               use File::Slurp;
726
727               $all_of_it = read_file($filename); # entire file in scalar
728           @all_lines = read_file($filename); # one line perl element
729
730       The customary Perl approach for processing all the lines in a file is
731       to do so one line at a time:
732
733           open (INPUT, $file)         ⎪⎪ die "can't open $file: $!";
734           while (<INPUT>) {
735               chomp;
736               # do something with $_
737           }
738           close(INPUT)                ⎪⎪ die "can't close $file: $!";
739
740       This is tremendously more efficient than reading the entire file into
741       memory as an array of lines and then processing it one element at a
742       time, which is often--if not almost always--the wrong approach.  When‐
743       ever you see someone do this:
744
745           @lines = <INPUT>;
746
747       you should think long and hard about why you need everything loaded at
748       once.  It's just not a scalable solution.  You might also find it more
749       fun to use the standard Tie::File module, or the DB_File module's
750       $DB_RECNO bindings, which allow you to tie an array to a file so that
751       accessing an element the array actually accesses the corresponding line
752       in the file.
753
754       You can read the entire filehandle contents into a scalar.
755
756           {
757               local(*INPUT, $/);
758               open (INPUT, $file)     ⎪⎪ die "can't open $file: $!";
759               $var = <INPUT>;
760           }
761
762       That temporarily undefs your record separator, and will automatically
763       close the file at block exit.  If the file is already open, just use
764       this:
765
766           $var = do { local $/; <INPUT> };
767
768       For ordinary files you can also use the read function.
769
770               read( INPUT, $var, -s INPUT );
771
772       The third argument tests the byte size of the data on the INPUT file‐
773       handle and reads that many bytes into the buffer $var.
774
775       How can I read in a file by paragraphs?
776
777       Use the $/ variable (see perlvar for details).  You can either set it
778       to "" to eliminate empty paragraphs ("abc\n\n\n\ndef", for instance,
779       gets treated as two paragraphs and not three), or "\n\n" to accept
780       empty paragraphs.
781
782       Note that a blank line must have no blanks in it.  Thus
783       "fred\n \nstuff\n\n" is one paragraph, but "fred\n\nstuff\n\n" is two.
784
785       How can I read a single character from a file?  From the keyboard?
786
787       You can use the builtin "getc()" function for most filehandles, but it
788       won't (easily) work on a terminal device.  For STDIN, either use the
789       Term::ReadKey module from CPAN or use the sample code in "getc" in
790       perlfunc.
791
792       If your system supports the portable operating system programming
793       interface (POSIX), you can use the following code, which you'll note
794       turns off echo processing as well.
795
796           #!/usr/bin/perl -w
797           use strict;
798           $⎪ = 1;
799           for (1..4) {
800               my $got;
801               print "gimme: ";
802               $got = getone();
803               print "--> $got\n";
804           }
805           exit;
806
807           BEGIN {
808               use POSIX qw(:termios_h);
809
810               my ($term, $oterm, $echo, $noecho, $fd_stdin);
811
812               $fd_stdin = fileno(STDIN);
813
814               $term     = POSIX::Termios->new();
815               $term->getattr($fd_stdin);
816               $oterm     = $term->getlflag();
817
818               $echo     = ECHO ⎪ ECHOK ⎪ ICANON;
819               $noecho   = $oterm & ~$echo;
820
821               sub cbreak {
822                   $term->setlflag($noecho);
823                   $term->setcc(VTIME, 1);
824                   $term->setattr($fd_stdin, TCSANOW);
825               }
826
827               sub cooked {
828                   $term->setlflag($oterm);
829                   $term->setcc(VTIME, 0);
830                   $term->setattr($fd_stdin, TCSANOW);
831               }
832
833               sub getone {
834                   my $key = '';
835                   cbreak();
836                   sysread(STDIN, $key, 1);
837                   cooked();
838                   return $key;
839               }
840
841           }
842
843           END { cooked() }
844
845       The Term::ReadKey module from CPAN may be easier to use.  Recent ver‐
846       sions include also support for non-portable systems as well.
847
848           use Term::ReadKey;
849           open(TTY, "</dev/tty");
850           print "Gimme a char: ";
851           ReadMode "raw";
852           $key = ReadKey 0, *TTY;
853           ReadMode "normal";
854           printf "\nYou said %s, char number %03d\n",
855               $key, ord $key;
856
857       How can I tell whether there's a character waiting on a filehandle?
858
859       The very first thing you should do is look into getting the Term::Read‐
860       Key extension from CPAN.  As we mentioned earlier, it now even has lim‐
861       ited support for non-portable (read: not open systems, closed, propri‐
862       etary, not POSIX, not Unix, etc) systems.
863
864       You should also check out the Frequently Asked Questions list in
865       comp.unix.* for things like this: the answer is essentially the same.
866       It's very system dependent.  Here's one solution that works on BSD sys‐
867       tems:
868
869           sub key_ready {
870               my($rin, $nfd);
871               vec($rin, fileno(STDIN), 1) = 1;
872               return $nfd = select($rin,undef,undef,0);
873           }
874
875       If you want to find out how many characters are waiting, there's also
876       the FIONREAD ioctl call to be looked at.  The h2ph tool that comes with
877       Perl tries to convert C include files to Perl code, which can be
878       "require"d.  FIONREAD ends up defined as a function in the sys/ioctl.ph
879       file:
880
881           require 'sys/ioctl.ph';
882
883           $size = pack("L", 0);
884           ioctl(FH, FIONREAD(), $size)    or die "Couldn't call ioctl: $!\n";
885           $size = unpack("L", $size);
886
887       If h2ph wasn't installed or doesn't work for you, you can grep the
888       include files by hand:
889
890           % grep FIONREAD /usr/include/*/*
891           /usr/include/asm/ioctls.h:#define FIONREAD      0x541B
892
893       Or write a small C program using the editor of champions:
894
895           % cat > fionread.c
896           #include <sys/ioctl.h>
897           main() {
898               printf("%#08x\n", FIONREAD);
899           }
900           ^D
901           % cc -o fionread fionread.c
902           % ./fionread
903           0x4004667f
904
905       And then hard code it, leaving porting as an exercise to your succes‐
906       sor.
907
908           $FIONREAD = 0x4004667f;         # XXX: opsys dependent
909
910           $size = pack("L", 0);
911           ioctl(FH, $FIONREAD, $size)     or die "Couldn't call ioctl: $!\n";
912           $size = unpack("L", $size);
913
914       FIONREAD requires a filehandle connected to a stream, meaning that
915       sockets, pipes, and tty devices work, but not files.
916
917       How do I do a "tail -f" in perl?
918
919       First try
920
921           seek(GWFILE, 0, 1);
922
923       The statement "seek(GWFILE, 0, 1)" doesn't change the current position,
924       but it does clear the end-of-file condition on the handle, so that the
925       next <GWFILE> makes Perl try again to read something.
926
927       If that doesn't work (it relies on features of your stdio implementa‐
928       tion), then you need something more like this:
929
930               for (;;) {
931                 for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {
932                   # search for some stuff and put it into files
933                 }
934                 # sleep for a while
935                 seek(GWFILE, $curpos, 0);  # seek to where we had been
936               }
937
938       If this still doesn't work, look into the POSIX module.  POSIX defines
939       the clearerr() method, which can remove the end of file condition on a
940       filehandle.  The method: read until end of file, clearerr(), read some
941       more.  Lather, rinse, repeat.
942
943       There's also a File::Tail module from CPAN.
944
945       How do I dup() a filehandle in Perl?
946
947       If you check "open" in perlfunc, you'll see that several of the ways to
948       call open() should do the trick.  For example:
949
950           open(LOG, ">>/foo/logfile");
951           open(STDERR, ">&LOG");
952
953       Or even with a literal numeric descriptor:
954
955          $fd = $ENV{MHCONTEXTFD};
956          open(MHCONTEXT, "<&=$fd");   # like fdopen(3S)
957
958       Note that "<&STDIN" makes a copy, but "<&=STDIN" make an alias.  That
959       means if you close an aliased handle, all aliases become inaccessible.
960       This is not true with a copied one.
961
962       Error checking, as always, has been left as an exercise for the reader.
963
964       How do I close a file descriptor by number?
965
966       This should rarely be necessary, as the Perl close() function is to be
967       used for things that Perl opened itself, even if it was a dup of a
968       numeric descriptor as with MHCONTEXT above.  But if you really have to,
969       you may be able to do this:
970
971           require 'sys/syscall.ph';
972           $rc = syscall(&SYS_close, $fd + 0);  # must force numeric
973           die "can't sysclose $fd: $!" unless $rc == -1;
974
975       Or, just use the fdopen(3S) feature of open():
976
977           {
978               local *F;
979               open F, "<&=$fd" or die "Cannot reopen fd=$fd: $!";
980               close F;
981           }
982
983       Why can't I use "C:\temp\foo" in DOS paths?  Why doesn't
984       `C:\temp\foo.exe` work?
985
986       Whoops!  You just put a tab and a formfeed into that filename!  Remem‐
987       ber that within double quoted strings ("like\this"), the backslash is
988       an escape character.  The full list of these is in "Quote and Quote-
989       like Operators" in perlop.  Unsurprisingly, you don't have a file
990       called "c:(tab)emp(formfeed)oo" or "c:(tab)emp(formfeed)oo.exe" on your
991       legacy DOS filesystem.
992
993       Either single-quote your strings, or (preferably) use forward slashes.
994       Since all DOS and Windows versions since something like MS-DOS 2.0 or
995       so have treated "/" and "\" the same in a path, you might as well use
996       the one that doesn't clash with Perl--or the POSIX shell, ANSI C and
997       C++, awk, Tcl, Java, or Python, just to mention a few.  POSIX paths are
998       more portable, too.
999
1000       Why doesn't glob("*.*") get all the files?
1001
1002       Because even on non-Unix ports, Perl's glob function follows standard
1003       Unix globbing semantics.  You'll need "glob("*")" to get all (non-hid‐
1004       den) files.  This makes glob() portable even to legacy systems.  Your
1005       port may include proprietary globbing functions as well.  Check its
1006       documentation for details.
1007
1008       Why does Perl let me delete read-only files?  Why does "-i" clobber
1009       protected files?  Isn't this a bug in Perl?
1010
1011       This is elaborately and painstakingly described in the file-dir-perms
1012       article in the "Far More Than You Ever Wanted To Know" collection in
1013       http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz .
1014
1015       The executive summary: learn how your filesystem works.  The permis‐
1016       sions on a file say what can happen to the data in that file.  The per‐
1017       missions on a directory say what can happen to the list of files in
1018       that directory.  If you delete a file, you're removing its name from
1019       the directory (so the operation depends on the permissions of the
1020       directory, not of the file).  If you try to write to the file, the per‐
1021       missions of the file govern whether you're allowed to.
1022
1023       How do I select a random line from a file?
1024
1025       Here's an algorithm from the Camel Book:
1026
1027           srand;
1028           rand($.) < 1 && ($line = $_) while <>;
1029
1030       This has a significant advantage in space over reading the whole file
1031       in.  You can find a proof of this method in The Art of Computer Pro‐
1032       gramming, Volume 2, Section 3.4.2, by Donald E. Knuth.
1033
1034       You can use the File::Random module which provides a function for that
1035       algorithm:
1036
1037               use File::Random qw/random_line/;
1038               my $line = random_line($filename);
1039
1040       Another way is to use the Tie::File module, which treats the entire
1041       file as an array.  Simply access a random array element.
1042
1043       Why do I get weird spaces when I print an array of lines?
1044
1045       Saying
1046
1047           print "@lines\n";
1048
1049       joins together the elements of @lines with a space between them.  If
1050       @lines were "("little", "fluffy", "clouds")" then the above statement
1051       would print
1052
1053           little fluffy clouds
1054
1055       but if each element of @lines was a line of text, ending a newline
1056       character "("little\n", "fluffy\n", "clouds\n")" then it would print:
1057
1058           little
1059            fluffy
1060            clouds
1061
1062       If your array contains lines, just print them:
1063
1064           print @lines;
1065
1067       Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and other
1068       authors as noted. All rights reserved.
1069
1070       This documentation is free; you can redistribute it and/or modify it
1071       under the same terms as Perl itself.
1072
1073       Irrespective of its distribution, all code examples here are in the
1074       public domain.  You are permitted and encouraged to use this code and
1075       any derivatives thereof in your own programs for fun or for profit as
1076       you see fit.  A simple comment in the code giving credit to the FAQ
1077       would be courteous but is not required.
1078
1079
1080
1081perl v5.8.8                       2006-01-07                       PERLFAQ5(1)
Impressum