This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Two doublewords less
[perl5.git] / pod / perlfaq5.pod
CommitLineData
68dc0745
PP
1=head1 NAME
2
3fe9a6f1 3perlfaq5 - Files and Formats ($Revision: 1.20 $, $Date: 1997/03/19 17:24:51 $)
68dc0745
PP
4
5=head1 DESCRIPTION
6
7This section deals with I/O and the "f" issues: filehandles, flushing,
8formats, and footers.
9
10=head2 How do I flush/unbuffer a filehandle? Why must I do this?
11
12The C standard I/O library (stdio) normally buffers characters sent to
13devices. This is done for efficiency reasons, so that there isn't a
14system call for each byte. Any time you use print() or write() in
15Perl, you go though this buffering. syswrite() circumvents stdio and
16buffering.
17
18In most stdio implementations, the type of buffering and the size of
19the buffer varies according to the type of device. Disk files are block
20buffered, often with a buffer size of more than 2k. Pipes and sockets
21are 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
23the entire line when it gets the newline.
24
25Perl does not support truly unbuffered output (except insofar as you can
26C<syswrite(OUT, $char, 1)>). What it does instead support is "command
27buffering", in which a physical write is performed after every output
28command. This isn't as hard on your system as unbuffering, but does
29get the output where you want it when you want it.
30
31If you expect characters to get to your device when you print them there,
32you'll want to autoflush its handle, as in the older:
33
34 use FileHandle;
35 open(DEV, "<+/dev/tty"); # ceci n'est pas une pipe
36 DEV->autoflush(1);
37
38or the newer IO::* modules:
39
40 use IO::Handle;
41 open(DEV, ">/dev/printer"); # but is this?
42 DEV->autoflush(1);
43
44or even this:
45
46 use IO::Socket; # this one is kinda a pipe?
47 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.com',
48 PeerPort => 'http(80)',
49 Proto => 'tcp');
50 die "$!" unless $sock;
51
52 $sock->autoflush();
53 $sock->print("GET /\015\012");
54 $document = join('', $sock->getlines());
55 print "DOC IS: $document\n";
56
57Note the hardcoded carriage return and newline in their octal
58equivalents. This is the ONLY way (currently) to assure a proper
59flush on all platforms, including Macintosh.
60
61You can use select() and the C<$|> variable to control autoflushing
62(see L<perlvar/$|> and L<perlfunc/select>):
63
64 $oldh = select(DEV);
65 $| = 1;
66 select($oldh);
67
68You'll also see code that does this without a temporary variable, as in
69
70 select((select(DEV), $| = 1)[0]);
71
72=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?
73
74Although humans have an easy time thinking of a text file as being a
75sequence of lines that operates much like a stack of playing cards --
76or punch cards -- computers usually see the text file as a sequence of
77bytes. In general, there's no direct way for Perl to seek to a
78particular line of a file, insert text into a file, or remove text
79from a file.
80
81(There are exceptions in special circumstances. Replacing a sequence
82of bytes with another sequence of the same length is one. Another is
83using the C<$DB_RECNO> array bindings as documented in L<DB_File>.
84Yet another is manipulating files with all lines the same length.)
85
86The general solution is to create a temporary copy of the text file with
87the changes you want, then copy that over the original.
88
89 $old = $file;
90 $new = "$file.tmp.$$";
91 $bak = "$file.bak";
92
93 open(OLD, "< $old") or die "can't open $old: $!";
94 open(NEW, "> $new") or die "can't open $new: $!";
95
96 # Correct typos, preserving case
97 while (<OLD>) {
98 s/\b(p)earl\b/${1}erl/i;
99 (print NEW $_) or die "can't write to $new: $!";
100 }
101
102 close(OLD) or die "can't close $old: $!";
103 close(NEW) or die "can't close $new: $!";
104
105 rename($old, $bak) or die "can't rename $old to $bak: $!";
106 rename($new, $old) or die "can't rename $new to $old: $!";
107
108Perl can do this sort of thing for you automatically with the C<-i>
54310121 109command line switch or the closely-related C<$^I> variable (see
68dc0745
PP
110L<perlrun> for more details). Note that
111C<-i> may require a suffix on some non-Unix systems; see the
112platform-specific documentation that came with your port.
113
114 # Renumber a series of tests from the command line
115 perl -pi -e 's/(^\s+test\s+)\d+/ $1 . ++$count /e' t/op/taint.t
116
117 # form a script
118 local($^I, @ARGV) = ('.bak', glob("*.c"));
119 while (<>) {
120 if ($. == 1) {
121 print "This line should appear at the top of each file\n";
122 }
123 s/\b(p)earl\b/${1}erl/i; # Correct typos, preserving case
124 print;
125 close ARGV if eof; # Reset $.
126 }
127
128If you need to seek to an arbitrary line of a file that changes
129infrequently, you could build up an index of byte positions of where
130the line ends are in the file. If the file is large, an index of
131every tenth or hundredth line end would allow you to seek and read
132fairly efficiently. If the file is sorted, try the look.pl library
133(part of the standard perl distribution).
134
135In the unique case of deleting lines at the end of a file, you
136can use tell() and truncate(). The following code snippet deletes
137the last line of a file without making a copy or reading the
138whole file into memory:
139
140 open (FH, "+< $file");
54310121 141 while ( <FH> ) { $addr = tell(FH) unless eof(FH) }
68dc0745
PP
142 truncate(FH, $addr);
143
144Error checking is left as an exercise for the reader.
145
146=head2 How do I count the number of lines in a file?
147
148One fairly efficient way is to count newlines in the file. The
149following program uses a feature of tr///, as documented in L<perlop>.
150If your text file doesn't end with a newline, then it's not really a
151proper text file, so this may report one fewer line than you expect.
152
153 $lines = 0;
154 open(FILE, $filename) or die "Can't open `$filename': $!";
155 while (sysread FILE, $buffer, 4096) {
156 $lines += ($buffer =~ tr/\n//);
157 }
158 close FILE;
159
160=head2 How do I make a temporary file name?
161
162Use the process ID and/or the current time-value. If you need to have
163many temporary files in one process, use a counter:
164
165 BEGIN {
166 use IO::File;
167 use Fcntl;
168 my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMP} || $ENV{TEMP};
169 my $base_name = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
170 sub temp_file {
171 my $fh = undef;
172 my $count = 0;
173 until (defined($fh) || $count > 100) {
174 $base_name =~ s/-(\d+)$/"-" . (1 + $1)/e;
175 $fh = IO::File->new($base_name, O_WRONLY|O_EXCL|O_CREAT, 0644)
176 }
177 if (defined($fh)) {
178 return ($fh, $base_name);
179 } else {
180 return ();
181 }
182 }
183 }
184
185Or you could simply use IO::Handle::new_tmpfile.
186
187=head2 How can I manipulate fixed-record-length files?
188
189The most efficient way is using pack() and unpack(). This is faster
190than using substr(). Here is a sample chunk of code to break up and
191put back together again some fixed-format input lines, in this case
192from the output of a normal, Berkeley-style ps:
193
194 # sample input line:
195 # 15158 p5 T 0:00 perl /home/tchrist/scripts/now-what
196 $PS_T = 'A6 A4 A7 A5 A*';
197 open(PS, "ps|");
198 $_ = <PS>; print;
199 while (<PS>) {
200 ($pid, $tt, $stat, $time, $command) = unpack($PS_T, $_);
201 for $var (qw!pid tt stat time command!) {
202 print "$var: <$$var>\n";
203 }
204 print 'line=', pack($PS_T, $pid, $tt, $stat, $time, $command),
205 "\n";
206 }
207
208=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?
209
210You may have some success with typeglobs, as we always had to use
211in days of old:
212
213 local(*FH);
214
215But while still supported, that isn't the best to go about getting
216local filehandles. Typeglobs have their drawbacks. You may well want
217to use the C<FileHandle> module, which creates new filehandles for you
218(see L<FileHandle>):
219
220 use FileHandle;
221 sub findme {
222 my $fh = FileHandle->new();
223 open($fh, "</etc/hosts") or die "no /etc/hosts: $!";
224 while (<$fh>) {
225 print if /\b127\.(0\.0\.)?1\b/;
226 }
227 # $fh automatically closes/disappears here
228 }
229
230Internally, Perl believes filehandles to be of class IO::Handle. You
231may use that module directly if you'd like (see L<IO::Handle>), or
232one of its more specific derived classes.
233
234=head2 How can I set up a footer format to be used with write()?
235
54310121 236There's no builtin way to do this, but L<perlform> has a couple of
68dc0745
PP
237techniques to make it possible for the intrepid hacker.
238
239=head2 How can I write() into a string?
240
241See L<perlform> for an swrite() function.
242
243=head2 How can I output my numbers with commas added?
244
245This one will do it for you:
246
247 sub commify {
248 local $_ = shift;
249 1 while s/^(-?\d+)(\d{3})/$1,$2/;
250 return $_;
251 }
252
253 $n = 23659019423.2331;
254 print "GOT: ", commify($n), "\n";
255
256 GOT: 23,659,019,423.2331
257
258You can't just:
259
260 s/^(-?\d+)(\d{3})/$1,$2/g;
261
262because you have to put the comma in and then recalculate your
263position.
264
265=head2 How can I translate tildes (~) in a filename?
266
267Use the E<lt>E<gt> (glob()) operator, documented in L<perlfunc>. This
268requires that you have a shell installed that groks tildes, meaning
269csh or tcsh or (some versions of) ksh, and thus may have portability
270problems. The Glob::KGlob module (available from CPAN) gives more
271portable glob functionality.
272
273Within Perl, you may use this directly:
274
275 $filename =~ s{
276 ^ ~ # find a leading tilde
277 ( # save this in $1
278 [^/] # a non-slash character
279 * # repeated 0 or more times (0 means me)
280 )
281 }{
282 $1
283 ? (getpwnam($1))[7]
284 : ( $ENV{HOME} || $ENV{LOGDIR} )
285 }ex;
286
287=head2 How come when I open the file read-write it wipes it out?
288
289Because you're using something like this, which truncates the file and
290I<then> gives you read-write access:
291
292 open(FH, "+> /path/name"); # WRONG
293
294Whoops. You should instead use this, which will fail if the file
295doesn't exist.
296
297 open(FH, "+< /path/name"); # open for update
298
299If this is an issue, try:
300
301 sysopen(FH, "/path/name", O_RDWR|O_CREAT, 0644);
302
303Error checking is left as an exercise for the reader.
304
305=head2 Why do I sometimes get an "Argument list too long" when I use <*>?
306
307The C<E<lt>E<gt>> operator performs a globbing operation (see above).
308By default glob() forks csh(1) to do the actual glob expansion, but
309csh can't handle more than 127 items and so gives the error message
310C<Argument list too long>. People who installed tcsh as csh won't
311have this problem, but their users may be surprised by it.
312
313To get around this, either do the glob yourself with C<Dirhandle>s and
314patterns, or use a module like Glob::KGlob, one that doesn't use the
315shell to do globbing.
316
317=head2 Is there a leak/bug in glob()?
318
319Due to the current implementation on some operating systems, when you
320use the glob() function or its angle-bracket alias in a scalar
321context, you may cause a leak and/or unpredictable behavior. It's
322best therefore to use glob() only in list context.
323
324=head2 How can I open a file with a leading "E<gt>" or trailing blanks?
325
326Normally perl ignores trailing blanks in filenames, and interprets
327certain leading characters (or a trailing "|") to mean something
328special. To avoid this, you might want to use a routine like this.
329It makes incomplete pathnames into explicit relative ones, and tacks a
330trailing null byte on the name to make perl leave it alone:
331
332 sub safe_filename {
333 local $_ = shift;
334 return m#^/#
335 ? "$_\0"
336 : "./$_\0";
337 }
338
339 $fn = safe_filename("<<<something really wicked ");
340 open(FH, "> $fn") or "couldn't open $fn: $!";
341
342You could also use the sysopen() function (see L<perlfunc/sysopen>).
343
344=head2 How can I reliably rename a file?
345
346Well, usually you just use Perl's rename() function. But that may
347not work everywhere, in particular, renaming files across file systems.
348If your operating system supports a mv(1) program or its moral equivalent,
349this works:
350
351 rename($old, $new) or system("mv", $old, $new);
352
353It may be more compelling to use the File::Copy module instead. You
354just copy to the new file to the new name (checking return values),
355then delete the old one. This isn't really the same semantics as a
356real rename(), though, which preserves metainformation like
357permissions, timestamps, inode info, etc.
358
359=head2 How can I lock a file?
360
54310121 361Perl's builtin flock() function (see L<perlfunc> for details) will call
68dc0745
PP
362flock(2) if that exists, fcntl(2) if it doesn't (on perl version 5.004 and
363later), and lockf(3) if neither of the two previous system calls exists.
364On some systems, it may even use a different form of native locking.
365Here are some gotchas with Perl's flock():
366
367=over 4
368
369=item 1
370
371Produces a fatal error if none of the three system calls (or their
372close equivalent) exists.
373
374=item 2
375
376lockf(3) does not provide shared locking, and requires that the
377filehandle be open for writing (or appending, or read/writing).
378
379=item 3
380
381Some versions of flock() can't lock files over a network (e.g. on NFS
382file systems), so you'd need to force the use of fcntl(2) when you
383build Perl. See the flock entry of L<perlfunc>, and the F<INSTALL>
384file in the source distribution for information on building Perl to do
385this.
386
387=back
388
389The CPAN module File::Lock offers similar functionality and (if you
390have dynamic loading) won't require you to rebuild perl if your
391flock() can't lock network files.
392
393=head2 What can't I just open(FH, ">file.lock")?
394
395A common bit of code B<NOT TO USE> is this:
396
397 sleep(3) while -e "file.lock"; # PLEASE DO NOT USE
398 open(LCK, "> file.lock"); # THIS BROKEN CODE
399
400This is a classic race condition: you take two steps to do something
401which must be done in one. That's why computer hardware provides an
402atomic test-and-set instruction. In theory, this "ought" to work:
403
404 sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT, 0644)
405 or die "can't open file.lock: $!":
406
407except that lamentably, file creation (and deletion) is not atomic
408over NFS, so this won't work (at least, not every time) over the net.
7a2e2cd6
PP
409Various schemes involving link() have been suggested, but these tend
410to involve busy-wait, which is also subdesirable.
68dc0745 411
54310121 412=head2 I still don't get locking. I just want to increment the number
68dc0745
PP
413in the file. How can I do this?
414
54310121 415Didn't anyone ever tell you web page hit counters were useless?
68dc0745
PP
416
417Anyway, this is what to do:
418
419 use Fcntl;
420 sysopen(FH, "numfile", O_RDWR|O_CREAT, 0644) or die "can't open numfile: $!";
421 flock(FH, 2) or die "can't flock numfile: $!";
422 $num = <FH> || 0;
423 seek(FH, 0, 0) or die "can't rewind numfile: $!";
424 truncate(FH, 0) or die "can't truncate numfile: $!";
425 (print FH $num+1, "\n") or die "can't write numfile: $!";
426 # DO NOT UNLOCK THIS UNTIL YOU CLOSE
427 close FH or die "can't close numfile: $!";
428
54310121 429Here's a much better web page hit counter:
68dc0745
PP
430
431 $hits = int( (time() - 850_000_000) / rand(1_000) );
432
433If the count doesn't impress your friends, then the code might. :-)
434
435=head2 How do I randomly update a binary file?
436
437If you're just trying to patch a binary, in many cases something as
438simple as this works:
439
440 perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs
441
442However, if you have fixed sized records, then you might do something more
443like this:
444
445 $RECSIZE = 220; # size of record, in bytes
446 $recno = 37; # which record to update
447 open(FH, "+<somewhere") || die "can't update somewhere: $!";
448 seek(FH, $recno * $RECSIZE, 0);
449 read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
450 # munge the record
451 seek(FH, $recno * $RECSIZE, 0);
452 print FH $record;
453 close FH;
454
455Locking and error checking are left as an exercise for the reader.
456Don't forget them, or you'll be quite sorry.
457
54310121 458Don't forget to set binmode() under MS-DOS-like platforms when operating
68dc0745
PP
459on files that have anything other than straight text in them. See the
460docs on open() and on binmode() for more details.
461
462=head2 How do I get a file's timestamp in perl?
463
464If you want to retrieve the time at which the file was last read,
54310121 465written, or had its metadata (owner, etc) changed, you use the B<-M>,
68dc0745
PP
466B<-A>, or B<-C> filetest operations as documented in L<perlfunc>. These
467retrieve the age of the file (measured against the start-time of your
468program) in days as a floating point number. To retrieve the "raw"
469time in seconds since the epoch, you would call the stat function,
470then use localtime(), gmtime(), or POSIX::strftime() to convert this
471into human-readable form.
472
473Here's an example:
474
475 $write_secs = (stat($file))[9];
476 print "file $file updated at ", scalar(localtime($file)), "\n";
477
478If you prefer something more legible, use the File::stat module
479(part of the standard distribution in version 5.004 and later):
480
481 use File::stat;
482 use Time::localtime;
483 $date_string = ctime(stat($file)->mtime);
484 print "file $file updated at $date_string\n";
485
486Error checking is left as an exercise for the reader.
487
488=head2 How do I set a file's timestamp in perl?
489
490You use the utime() function documented in L<perlfunc/utime>.
491By way of example, here's a little program that copies the
492read and write times from its first argument to all the rest
493of them.
494
495 if (@ARGV < 2) {
496 die "usage: cptimes timestamp_file other_files ...\n";
497 }
498 $timestamp = shift;
499 ($atime, $mtime) = (stat($timestamp))[8,9];
500 utime $atime, $mtime, @ARGV;
501
502Error checking is left as an exercise for the reader.
503
504Note that utime() currently doesn't work correctly with Win95/NT
505ports. A bug has been reported. Check it carefully before using
506it on those platforms.
507
508=head2 How do I print to more than one file at once?
509
510If you only have to do this once, you can do this:
511
512 for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
513
514To connect up to one filehandle to several output filehandles, it's
515easiest to use the tee(1) program if you have it, and let it take care
516of the multiplexing:
517
518 open (FH, "| tee file1 file2 file3");
519
520Otherwise you'll have to write your own multiplexing print function --
521or your own tee program -- or use Tom Christiansen's, at
522http://www.perl.com/CPAN/authors/id/TOMC/scripts/tct.gz, which is
523written in Perl.
524
525In theory a IO::Tee class could be written, but to date we haven't
526seen such.
527
528=head2 How can I read in a file by paragraphs?
529
530Use the C<$\> variable (see L<perlvar> for details). You can either
531set it to C<""> to eliminate empty paragraphs (C<"abc\n\n\n\ndef">,
532for instance, gets treated as two paragraphs and not three), or
533C<"\n\n"> to accept empty paragraphs.
534
535=head2 How can I read a single character from a file? From the keyboard?
536
537You can use the builtin C<getc()> function for most filehandles, but
538it won't (easily) work on a terminal device. For STDIN, either use
539the Term::ReadKey module from CPAN, or use the sample code in
540L<perlfunc/getc>.
541
542If your system supports POSIX, you can use the following code, which
543you'll note turns off echo processing as well.
544
545 #!/usr/bin/perl -w
546 use strict;
547 $| = 1;
548 for (1..4) {
549 my $got;
550 print "gimme: ";
551 $got = getone();
552 print "--> $got\n";
553 }
554 exit;
555
556 BEGIN {
557 use POSIX qw(:termios_h);
558
559 my ($term, $oterm, $echo, $noecho, $fd_stdin);
560
561 $fd_stdin = fileno(STDIN);
562
563 $term = POSIX::Termios->new();
564 $term->getattr($fd_stdin);
565 $oterm = $term->getlflag();
566
567 $echo = ECHO | ECHOK | ICANON;
568 $noecho = $oterm & ~$echo;
569
570 sub cbreak {
571 $term->setlflag($noecho);
572 $term->setcc(VTIME, 1);
573 $term->setattr($fd_stdin, TCSANOW);
574 }
575
576 sub cooked {
577 $term->setlflag($oterm);
578 $term->setcc(VTIME, 0);
579 $term->setattr($fd_stdin, TCSANOW);
580 }
581
582 sub getone {
583 my $key = '';
584 cbreak();
585 sysread(STDIN, $key, 1);
586 cooked();
587 return $key;
588 }
589
590 }
591
592 END { cooked() }
593
594The Term::ReadKey module from CPAN may be easier to use:
595
596 use Term::ReadKey;
597 open(TTY, "</dev/tty");
598 print "Gimme a char: ";
599 ReadMode "raw";
600 $key = ReadKey 0, *TTY;
601 ReadMode "normal";
602 printf "\nYou said %s, char number %03d\n",
603 $key, ord $key;
604
54310121 605For MS-DOS systems, Dan Carson <dbc@tc.fluke.COM> reports the following:
68dc0745
PP
606
607To put the PC in "raw" mode, use ioctl with some magic numbers gleaned
608from msdos.c (Perl source file) and Ralf Brown's interrupt list (comes
609across the net every so often):
610
611 $old_ioctl = ioctl(STDIN,0,0); # Gets device info
612 $old_ioctl &= 0xff;
613 ioctl(STDIN,1,$old_ioctl | 32); # Writes it back, setting bit 5
614
615Then to read a single character:
616
617 sysread(STDIN,$c,1); # Read a single character
618
619And to put the PC back to "cooked" mode:
620
621 ioctl(STDIN,1,$old_ioctl); # Sets it back to cooked mode.
622
623So now you have $c. If C<ord($c) == 0>, you have a two byte code, which
624means you hit a special key. Read another byte with C<sysread(STDIN,$c,1)>,
625and that value tells you what combination it was according to this
626table:
627
628 # PC 2-byte keycodes = ^@ + the following:
629
630 # HEX KEYS
631 # --- ----
632 # 0F SHF TAB
633 # 10-19 ALT QWERTYUIOP
634 # 1E-26 ALT ASDFGHJKL
635 # 2C-32 ALT ZXCVBNM
636 # 3B-44 F1-F10
637 # 47-49 HOME,UP,PgUp
638 # 4B LEFT
639 # 4D RIGHT
640 # 4F-53 END,DOWN,PgDn,Ins,Del
641 # 54-5D SHF F1-F10
642 # 5E-67 CTR F1-F10
643 # 68-71 ALT F1-F10
644 # 73-77 CTR LEFT,RIGHT,END,PgDn,HOME
645 # 78-83 ALT 1234567890-=
646 # 84 CTR PgUp
647
648This is all trial and error I did a long time ago, I hope I'm reading the
649file that worked.
650
651=head2 How can I tell if there's a character waiting on a filehandle?
652
653You should check out the Frequently Asked Questions list in
654comp.unix.* for things like this: the answer is essentially the same.
655It's very system dependent. Here's one solution that works on BSD
656systems:
657
658 sub key_ready {
659 my($rin, $nfd);
660 vec($rin, fileno(STDIN), 1) = 1;
661 return $nfd = select($rin,undef,undef,0);
662 }
663
664You should look into getting the Term::ReadKey extension from CPAN.
665
666=head2 How do I open a file without blocking?
667
668You need to use the O_NDELAY or O_NONBLOCK flag from the Fcntl module
669in conjunction with sysopen():
670
671 use Fcntl;
672 sysopen(FH, "/tmp/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)
673 or die "can't open /tmp/somefile: $!":
674
675=head2 How do I create a file only if it doesn't exist?
676
677You need to use the O_CREAT and O_EXCL flags from the Fcntl module in
678conjunction with sysopen():
679
680 use Fcntl;
681 sysopen(FH, "/tmp/somefile", O_WRONLY|O_EXCL|O_CREAT, 0644)
682 or die "can't open /tmp/somefile: $!":
683
684Be warned that neither creation nor deletion of files is guaranteed to
685be an atomic operation over NFS. That is, two processes might both
686successful create or unlink the same file!
687
688=head2 How do I do a C<tail -f> in perl?
689
690First try
691
692 seek(GWFILE, 0, 1);
693
694The statement C<seek(GWFILE, 0, 1)> doesn't change the current position,
695but it does clear the end-of-file condition on the handle, so that the
696next <GWFILE> makes Perl try again to read something.
697
698If that doesn't work (it relies on features of your stdio implementation),
699then you need something more like this:
700
701 for (;;) {
702 for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {
703 # search for some stuff and put it into files
704 }
705 # sleep for a while
706 seek(GWFILE, $curpos, 0); # seek to where we had been
707 }
708
709If this still doesn't work, look into the POSIX module. POSIX defines
710the clearerr() method, which can remove the end of file condition on a
711filehandle. The method: read until end of file, clearerr(), read some
712more. Lather, rinse, repeat.
713
714=head2 How do I dup() a filehandle in Perl?
715
716If you check L<perlfunc/open>, you'll see that several of the ways
717to call open() should do the trick. For example:
718
719 open(LOG, ">>/tmp/logfile");
720 open(STDERR, ">&LOG");
721
722Or even with a literal numeric descriptor:
723
724 $fd = $ENV{MHCONTEXTFD};
725 open(MHCONTEXT, "<&=$fd"); # like fdopen(3S)
726
727Error checking has been left as an exercise for the reader.
728
729=head2 How do I close a file descriptor by number?
730
731This should rarely be necessary, as the Perl close() function is to be
732used for things that Perl opened itself, even if it was a dup of a
733numeric descriptor, as with MHCONTEXT above. But if you really have
734to, you may be able to do this:
735
736 require 'sys/syscall.ph';
737 $rc = syscall(&SYS_close, $fd + 0); # must force numeric
738 die "can't sysclose $fd: $!" unless $rc == -1;
739
54310121 740=head2 Why can't I use "C:\temp\foo" in MS-DOS paths? What doesn't `C:\temp\foo.exe` work?
68dc0745
PP
741
742Whoops! You just put a tab and a formfeed into that filename!
743Remember that within double quoted strings ("like\this"), the
744backslash is an escape character. The full list of these is in
745L<perlop/Quote and Quote-like Operators>. Unsurprisingly, you don't
746have a file called "c:(tab)emp(formfeed)oo" or
54310121 747"c:(tab)emp(formfeed)oo.exe" on your MS-DOS filesystem.
68dc0745
PP
748
749Either single-quote your strings, or (preferably) use forward slashes.
54310121 750Since all MS-DOS and Windows versions since something like MS-DOS 2.0 or so
68dc0745
PP
751have treated C</> and C<\> the same in a path, you might as well use the
752one that doesn't clash with Perl -- or the POSIX shell, ANSI C and C++,
753awk, Tcl, Java, or Python, just to mention a few.
754
755=head2 Why doesn't glob("*.*") get all the files?
756
757Because even on non-Unix ports, Perl's glob function follows standard
54310121 758Unix globbing semantics. You'll need C<glob("*")> to get all (nonhidden)
68dc0745
PP
759files.
760
761=head2 Why does Perl let me delete read-only files? Why does C<-i> clobber protected files? Isn't this a bug in Perl?
762
763This is elaborately and painstakingly described in the "Far More Than
764You Every Wanted To Know" in
765http://www.perl.com/CPAN/doc/FMTEYEWTK/file-dir-perms .
766
767The executive summary: learn how your filesystem works. The
768permissions on a file say what can happen to the data in that file.
769The permissions on a directory say what can happen to the list of
770files in that directory. If you delete a file, you're removing its
771name from the directory (so the operation depends on the permissions
772of the directory, not of the file). If you try to write to the file,
773the permissions of the file govern whether you're allowed to.
774
775=head2 How do I select a random line from a file?
776
777Here's an algorithm from the Camel Book:
778
779 srand;
780 rand($.) < 1 && ($line = $_) while <>;
781
782This has a significant advantage in space over reading the whole
783file in.
784
785=head1 AUTHOR AND COPYRIGHT
786
787Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
788All rights reserved. See L<perlfaq> for distribution information.