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