Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "Tie::File 3" | |
132 | .TH Tie::File 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Tie::File \- Access the lines of a disk file via a Perl array | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& # This file documents Tie::File version 0.93 | |
139 | .Ve | |
140 | .PP | |
141 | .Vb 1 | |
142 | \& tie @array, 'Tie::File', filename or die ...; | |
143 | .Ve | |
144 | .PP | |
145 | .Vb 2 | |
146 | \& $array[13] = 'blah'; # line 13 of the file is now 'blah' | |
147 | \& print $array[42]; # display line 42 of the file | |
148 | .Ve | |
149 | .PP | |
150 | .Vb 2 | |
151 | \& $n_recs = @array; # how many records are in the file? | |
152 | \& $#array -= 2; # chop two records off the end | |
153 | .Ve | |
154 | .PP | |
155 | .Vb 3 | |
156 | \& for (@array) { | |
157 | \& s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file | |
158 | \& } | |
159 | .Ve | |
160 | .PP | |
161 | .Vb 2 | |
162 | \& # These are just like regular push, pop, unshift, shift, and splice | |
163 | \& # Except that they modify the file in the way you would expect | |
164 | .Ve | |
165 | .PP | |
166 | .Vb 5 | |
167 | \& push @array, new recs...; | |
168 | \& my $r1 = pop @array; | |
169 | \& unshift @array, new recs...; | |
170 | \& my $r1 = shift @array; | |
171 | \& @old_recs = splice @array, 3, 7, new recs...; | |
172 | .Ve | |
173 | .PP | |
174 | .Vb 1 | |
175 | \& untie @array; # all finished | |
176 | .Ve | |
177 | .SH "DESCRIPTION" | |
178 | .IX Header "DESCRIPTION" | |
179 | \&\f(CW\*(C`Tie::File\*(C'\fR represents a regular text file as a Perl array. Each | |
180 | element in the array corresponds to a record in the file. The first | |
181 | line of the file is element 0 of the array; the second line is element | |
182 | 1, and so on. | |
183 | .PP | |
184 | The file is \fInot\fR loaded into memory, so this will work even for | |
185 | gigantic files. | |
186 | .PP | |
187 | Changes to the array are reflected in the file immediately. | |
188 | .PP | |
189 | Lazy people and beginners may now stop reading the manual. | |
190 | .ie n .Sh """recsep""" | |
191 | .el .Sh "\f(CWrecsep\fP" | |
192 | .IX Subsection "recsep" | |
193 | What is a 'record'? By default, the meaning is the same as for the | |
194 | \&\f(CW\*(C`<...>\*(C'\fR operator: It's a string terminated by \f(CW$/\fR, which is | |
195 | probably \f(CW"\en"\fR. (Minor exception: on dos and Win32 systems, a | |
196 | \&'record' is a string terminated by \f(CW"\er\en"\fR.) You may change the | |
197 | definition of \*(L"record\*(R" by supplying the \f(CW\*(C`recsep\*(C'\fR option in the \f(CW\*(C`tie\*(C'\fR | |
198 | call: | |
199 | .PP | |
200 | .Vb 1 | |
201 | \& tie @array, 'Tie::File', $file, recsep => 'es'; | |
202 | .Ve | |
203 | .PP | |
204 | This says that records are delimited by the string \f(CW\*(C`es\*(C'\fR. If the file | |
205 | contained the following data: | |
206 | .PP | |
207 | .Vb 1 | |
208 | \& Curse these pesky flies!\en | |
209 | .Ve | |
210 | .PP | |
211 | then the \f(CW@array\fR would appear to have four elements: | |
212 | .PP | |
213 | .Vb 4 | |
214 | \& "Curse th" | |
215 | \& "e p" | |
216 | \& "ky fli" | |
217 | \& "!\en" | |
218 | .Ve | |
219 | .PP | |
220 | An undefined value is not permitted as a record separator. Perl's | |
221 | special \*(L"paragraph mode\*(R" semantics (A\*` la \f(CW\*(C`$/ = ""\*(C'\fR) are not | |
222 | emulated. | |
223 | .PP | |
224 | Records read from the tied array do not have the record separator | |
225 | string on the end; this is to allow | |
226 | .PP | |
227 | .Vb 1 | |
228 | \& $array[17] .= "extra"; | |
229 | .Ve | |
230 | .PP | |
231 | to work as expected. | |
232 | .PP | |
233 | (See \*(L"autochomp\*(R", below.) Records stored into the array will have | |
234 | the record separator string appended before they are written to the | |
235 | file, if they don't have one already. For example, if the record | |
236 | separator string is \f(CW"\en"\fR, then the following two lines do exactly | |
237 | the same thing: | |
238 | .PP | |
239 | .Vb 2 | |
240 | \& $array[17] = "Cherry pie"; | |
241 | \& $array[17] = "Cherry pie\en"; | |
242 | .Ve | |
243 | .PP | |
244 | The result is that the contents of line 17 of the file will be | |
245 | replaced with \*(L"Cherry pie\*(R"; a newline character will separate line 17 | |
246 | from line 18. This means that this code will do nothing: | |
247 | .PP | |
248 | .Vb 1 | |
249 | \& chomp $array[17]; | |
250 | .Ve | |
251 | .PP | |
252 | Because the \f(CW\*(C`chomp\*(C'\fRed value will have the separator reattached when | |
253 | it is written back to the file. There is no way to create a file | |
254 | whose trailing record separator string is missing. | |
255 | .PP | |
256 | Inserting records that \fIcontain\fR the record separator string is not | |
257 | supported by this module. It will probably produce a reasonable | |
258 | result, but what this result will be may change in a future version. | |
259 | Use 'splice' to insert records or to replace one record with several. | |
260 | .ie n .Sh """autochomp""" | |
261 | .el .Sh "\f(CWautochomp\fP" | |
262 | .IX Subsection "autochomp" | |
263 | Normally, array elements have the record separator removed, so that if | |
264 | the file contains the text | |
265 | .PP | |
266 | .Vb 3 | |
267 | \& Gold | |
268 | \& Frankincense | |
269 | \& Myrrh | |
270 | .Ve | |
271 | .PP | |
272 | the tied array will appear to contain \f(CW\*(C`("Gold", "Frankincense", | |
273 | "Myrrh")\*(C'\fR. If you set \f(CW\*(C`autochomp\*(C'\fR to a false value, the record | |
274 | separator will not be removed. If the file above was tied with | |
275 | .PP | |
276 | .Vb 1 | |
277 | \& tie @gifts, "Tie::File", $gifts, autochomp => 0; | |
278 | .Ve | |
279 | .PP | |
280 | then the array \f(CW@gifts\fR would appear to contain \f(CW\*(C`("Gold\en", | |
281 | "Frankincense\en", "Myrrh\en")\*(C'\fR, or (on Win32 systems) \f(CW\*(C`("Gold\er\en", | |
282 | "Frankincense\er\en", "Myrrh\er\en")\*(C'\fR. | |
283 | .ie n .Sh """mode""" | |
284 | .el .Sh "\f(CWmode\fP" | |
285 | .IX Subsection "mode" | |
286 | Normally, the specified file will be opened for read and write access, | |
287 | and will be created if it does not exist. (That is, the flags | |
288 | \&\f(CW\*(C`O_RDWR | O_CREAT\*(C'\fR are supplied in the \f(CW\*(C`open\*(C'\fR call.) If you want to | |
289 | change this, you may supply alternative flags in the \f(CW\*(C`mode\*(C'\fR option. | |
290 | See Fcntl for a listing of available flags. | |
291 | For example: | |
292 | .PP | |
293 | .Vb 3 | |
294 | \& # open the file if it exists, but fail if it does not exist | |
295 | \& use Fcntl 'O_RDWR'; | |
296 | \& tie @array, 'Tie::File', $file, mode => O_RDWR; | |
297 | .Ve | |
298 | .PP | |
299 | .Vb 3 | |
300 | \& # create the file if it does not exist | |
301 | \& use Fcntl 'O_RDWR', 'O_CREAT'; | |
302 | \& tie @array, 'Tie::File', $file, mode => O_RDWR | O_CREAT; | |
303 | .Ve | |
304 | .PP | |
305 | .Vb 3 | |
306 | \& # open an existing file in read-only mode | |
307 | \& use Fcntl 'O_RDONLY'; | |
308 | \& tie @array, 'Tie::File', $file, mode => O_RDONLY; | |
309 | .Ve | |
310 | .PP | |
311 | Opening the data file in write-only or append mode is not supported. | |
312 | .ie n .Sh """memory""" | |
313 | .el .Sh "\f(CWmemory\fP" | |
314 | .IX Subsection "memory" | |
315 | This is an upper limit on the amount of memory that \f(CW\*(C`Tie::File\*(C'\fR will | |
316 | consume at any time while managing the file. This is used for two | |
317 | things: managing the \fIread cache\fR and managing the \fIdeferred write | |
318 | buffer\fR. | |
319 | .PP | |
320 | Records read in from the file are cached, to avoid having to re-read | |
321 | them repeatedly. If you read the same record twice, the first time it | |
322 | will be stored in memory, and the second time it will be fetched from | |
323 | the \fIread cache\fR. The amount of data in the read cache will not | |
324 | exceed the value you specified for \f(CW\*(C`memory\*(C'\fR. If \f(CW\*(C`Tie::File\*(C'\fR wants | |
325 | to cache a new record, but the read cache is full, it will make room | |
326 | by expiring the least-recently visited records from the read cache. | |
327 | .PP | |
328 | The default memory limit is 2Mib. You can adjust the maximum read | |
329 | cache size by supplying the \f(CW\*(C`memory\*(C'\fR option. The argument is the | |
330 | desired cache size, in bytes. | |
331 | .PP | |
332 | .Vb 2 | |
333 | \& # I have a lot of memory, so use a large cache to speed up access | |
334 | \& tie @array, 'Tie::File', $file, memory => 20_000_000; | |
335 | .Ve | |
336 | .PP | |
337 | Setting the memory limit to 0 will inhibit caching; records will be | |
338 | fetched from disk every time you examine them. | |
339 | .PP | |
340 | The \f(CW\*(C`memory\*(C'\fR value is not an absolute or exact limit on the memory | |
341 | used. \f(CW\*(C`Tie::File\*(C'\fR objects contains some structures besides the read | |
342 | cache and the deferred write buffer, whose sizes are not charged | |
343 | against \f(CW\*(C`memory\*(C'\fR. | |
344 | .ie n .Sh """dw_size""" | |
345 | .el .Sh "\f(CWdw_size\fP" | |
346 | .IX Subsection "dw_size" | |
347 | (This is an advanced feature. Skip this section on first reading.) | |
348 | .PP | |
349 | If you use deferred writing (See \*(L"Deferred Writing\*(R", below) then | |
350 | data you write into the array will not be written directly to the | |
351 | file; instead, it will be saved in the \fIdeferred write buffer\fR to be | |
352 | written out later. Data in the deferred write buffer is also charged | |
353 | against the memory limit you set with the \f(CW\*(C`memory\*(C'\fR option. | |
354 | .PP | |
355 | You may set the \f(CW\*(C`dw_size\*(C'\fR option to limit the amount of data that can | |
356 | be saved in the deferred write buffer. This limit may not exceed the | |
357 | total memory limit. For example, if you set \f(CW\*(C`dw_size\*(C'\fR to 1000 and | |
358 | \&\f(CW\*(C`memory\*(C'\fR to 2500, that means that no more than 1000 bytes of deferred | |
359 | writes will be saved up. The space available for the read cache will | |
360 | vary, but it will always be at least 1500 bytes (if the deferred write | |
361 | buffer is full) and it could grow as large as 2500 bytes (if the | |
362 | deferred write buffer is empty.) | |
363 | .PP | |
364 | If you don't specify a \f(CW\*(C`dw_size\*(C'\fR, it defaults to the entire memory | |
365 | limit. | |
366 | .Sh "Option Format" | |
367 | .IX Subsection "Option Format" | |
368 | \&\f(CW\*(C`\-mode\*(C'\fR is a synonym for \f(CW\*(C`mode\*(C'\fR. \f(CW\*(C`\-recsep\*(C'\fR is a synonym for | |
369 | \&\f(CW\*(C`recsep\*(C'\fR. \f(CW\*(C`\-memory\*(C'\fR is a synonym for \f(CW\*(C`memory\*(C'\fR. You get the | |
370 | idea. | |
371 | .SH "Public Methods" | |
372 | .IX Header "Public Methods" | |
373 | The \f(CW\*(C`tie\*(C'\fR call returns an object, say \f(CW$o\fR. You may call | |
374 | .PP | |
375 | .Vb 2 | |
376 | \& $rec = $o->FETCH($n); | |
377 | \& $o->STORE($n, $rec); | |
378 | .Ve | |
379 | .PP | |
380 | to fetch or store the record at line \f(CW$n\fR, respectively; similarly | |
381 | the other tied array methods. (See perltie for details.) You may | |
382 | also call the following methods on this object: | |
383 | .ie n .Sh """flock""" | |
384 | .el .Sh "\f(CWflock\fP" | |
385 | .IX Subsection "flock" | |
386 | .Vb 1 | |
387 | \& $o->flock(MODE) | |
388 | .Ve | |
389 | .PP | |
390 | will lock the tied file. \f(CW\*(C`MODE\*(C'\fR has the same meaning as the second | |
391 | argument to the Perl built-in \f(CW\*(C`flock\*(C'\fR function; for example | |
392 | \&\f(CW\*(C`LOCK_SH\*(C'\fR or \f(CW\*(C`LOCK_EX | LOCK_NB\*(C'\fR. (These constants are provided by | |
393 | the \f(CW\*(C`use Fcntl ':flock'\*(C'\fR declaration.) | |
394 | .PP | |
395 | \&\f(CW\*(C`MODE\*(C'\fR is optional; the default is \f(CW\*(C`LOCK_EX\*(C'\fR. | |
396 | .PP | |
397 | \&\f(CW\*(C`Tie::File\*(C'\fR promises that the following sequence of operations will | |
398 | be safe: | |
399 | .PP | |
400 | .Vb 2 | |
401 | \& my $o = tie @array, "Tie::File", $filename; | |
402 | \& $o->flock; | |
403 | .Ve | |
404 | .PP | |
405 | In particular, \f(CW\*(C`Tie::File\*(C'\fR will \fInot\fR read or write the file during | |
406 | the \f(CW\*(C`tie\*(C'\fR call. (Exception: Using \f(CW\*(C`mode => O_TRUNC\*(C'\fR will, of | |
407 | course, erase the file during the \f(CW\*(C`tie\*(C'\fR call. If you want to do this | |
408 | safely, then open the file without \f(CW\*(C`O_TRUNC\*(C'\fR, lock the file, and use | |
409 | \&\f(CW\*(C`@array = ()\*(C'\fR.) | |
410 | .PP | |
411 | The best way to unlock a file is to discard the object and untie the | |
412 | array. It is probably unsafe to unlock the file without also untying | |
413 | it, because if you do, changes may remain unwritten inside the object. | |
414 | That is why there is no shortcut for unlocking. If you really want to | |
415 | unlock the file prematurely, you know what to do; if you don't know | |
416 | what to do, then don't do it. | |
417 | .PP | |
418 | All the usual warnings about file locking apply here. In particular, | |
419 | note that file locking in Perl is \fBadvisory\fR, which means that | |
420 | holding a lock will not prevent anyone else from reading, writing, or | |
421 | erasing the file; it only prevents them from getting another lock at | |
422 | the same time. Locks are analogous to green traffic lights: If you | |
423 | have a green light, that does not prevent the idiot coming the other | |
424 | way from plowing into you sideways; it merely guarantees to you that | |
425 | the idiot does not also have a green light at the same time. | |
426 | .ie n .Sh """autochomp""" | |
427 | .el .Sh "\f(CWautochomp\fP" | |
428 | .IX Subsection "autochomp" | |
429 | .Vb 2 | |
430 | \& my $old_value = $o->autochomp(0); # disable autochomp option | |
431 | \& my $old_value = $o->autochomp(1); # enable autochomp option | |
432 | .Ve | |
433 | .PP | |
434 | .Vb 1 | |
435 | \& my $ac = $o->autochomp(); # recover current value | |
436 | .Ve | |
437 | .PP | |
438 | See \*(L"autochomp\*(R", above. | |
439 | .ie n .Sh """defer""\fP, \f(CW""flush""\fP, \f(CW""discard""\fP, and \f(CW""autodefer""" | |
440 | .el .Sh "\f(CWdefer\fP, \f(CWflush\fP, \f(CWdiscard\fP, and \f(CWautodefer\fP" | |
441 | .IX Subsection "defer, flush, discard, and autodefer" | |
442 | See \*(L"Deferred Writing\*(R", below. | |
443 | .SH "Tying to an already-opened filehandle" | |
444 | .IX Header "Tying to an already-opened filehandle" | |
445 | If \f(CW$fh\fR is a filehandle, such as is returned by \f(CW\*(C`IO::File\*(C'\fR or one | |
446 | of the other \f(CW\*(C`IO\*(C'\fR modules, you may use: | |
447 | .PP | |
448 | .Vb 1 | |
449 | \& tie @array, 'Tie::File', $fh, ...; | |
450 | .Ve | |
451 | .PP | |
452 | Similarly if you opened that handle \f(CW\*(C`FH\*(C'\fR with regular \f(CW\*(C`open\*(C'\fR or | |
453 | \&\f(CW\*(C`sysopen\*(C'\fR, you may use: | |
454 | .PP | |
455 | .Vb 1 | |
456 | \& tie @array, 'Tie::File', \e*FH, ...; | |
457 | .Ve | |
458 | .PP | |
459 | Handles that were opened write-only won't work. Handles that were | |
460 | opened read-only will work as long as you don't try to modify the | |
461 | array. Handles must be attached to seekable sources of data\-\-\-that | |
462 | means no pipes or sockets. If \f(CW\*(C`Tie::File\*(C'\fR can detect that you | |
463 | supplied a non-seekable handle, the \f(CW\*(C`tie\*(C'\fR call will throw an | |
464 | exception. (On Unix systems, it can detect this.) | |
465 | .SH "Deferred Writing" | |
466 | .IX Header "Deferred Writing" | |
467 | (This is an advanced feature. Skip this section on first reading.) | |
468 | .PP | |
469 | Normally, modifying a \f(CW\*(C`Tie::File\*(C'\fR array writes to the underlying file | |
470 | immediately. Every assignment like \f(CW\*(C`$a[3] = ...\*(C'\fR rewrites as much of | |
471 | the file as is necessary; typically, everything from line 3 through | |
472 | the end will need to be rewritten. This is the simplest and most | |
473 | transparent behavior. Performance even for large files is reasonably | |
474 | good. | |
475 | .PP | |
476 | However, under some circumstances, this behavior may be excessively | |
477 | slow. For example, suppose you have a million-record file, and you | |
478 | want to do: | |
479 | .PP | |
480 | .Vb 3 | |
481 | \& for (@FILE) { | |
482 | \& $_ = "> $_"; | |
483 | \& } | |
484 | .Ve | |
485 | .PP | |
486 | The first time through the loop, you will rewrite the entire file, | |
487 | from line 0 through the end. The second time through the loop, you | |
488 | will rewrite the entire file from line 1 through the end. The third | |
489 | time through the loop, you will rewrite the entire file from line 2 to | |
490 | the end. And so on. | |
491 | .PP | |
492 | If the performance in such cases is unacceptable, you may defer the | |
493 | actual writing, and then have it done all at once. The following loop | |
494 | will perform much better for large files: | |
495 | .PP | |
496 | .Vb 5 | |
497 | \& (tied @a)->defer; | |
498 | \& for (@a) { | |
499 | \& $_ = "> $_"; | |
500 | \& } | |
501 | \& (tied @a)->flush; | |
502 | .Ve | |
503 | .PP | |
504 | If \f(CW\*(C`Tie::File\*(C'\fR's memory limit is large enough, all the writing will | |
505 | done in memory. Then, when you call \f(CW\*(C`\->flush\*(C'\fR, the entire file | |
506 | will be rewritten in a single pass. | |
507 | .PP | |
508 | (Actually, the preceding discussion is something of a fib. You don't | |
509 | need to enable deferred writing to get good performance for this | |
510 | common case, because \f(CW\*(C`Tie::File\*(C'\fR will do it for you automatically | |
511 | unless you specifically tell it not to. See \*(L"autodeferring\*(R", | |
512 | below.) | |
513 | .PP | |
514 | Calling \f(CW\*(C`\->flush\*(C'\fR returns the array to immediate-write mode. If | |
515 | you wish to discard the deferred writes, you may call \f(CW\*(C`\->discard\*(C'\fR | |
516 | instead of \f(CW\*(C`\->flush\*(C'\fR. Note that in some cases, some of the data | |
517 | will have been written already, and it will be too late for | |
518 | \&\f(CW\*(C`\->discard\*(C'\fR to discard all the changes. Support for | |
519 | \&\f(CW\*(C`\->discard\*(C'\fR may be withdrawn in a future version of \f(CW\*(C`Tie::File\*(C'\fR. | |
520 | .PP | |
521 | Deferred writes are cached in memory up to the limit specified by the | |
522 | \&\f(CW\*(C`dw_size\*(C'\fR option (see above). If the deferred-write buffer is full | |
523 | and you try to write still more deferred data, the buffer will be | |
524 | flushed. All buffered data will be written immediately, the buffer | |
525 | will be emptied, and the now-empty space will be used for future | |
526 | deferred writes. | |
527 | .PP | |
528 | If the deferred-write buffer isn't yet full, but the total size of the | |
529 | buffer and the read cache would exceed the \f(CW\*(C`memory\*(C'\fR limit, the oldest | |
530 | records will be expired from the read cache until the total size is | |
531 | under the limit. | |
532 | .PP | |
533 | \&\f(CW\*(C`push\*(C'\fR, \f(CW\*(C`pop\*(C'\fR, \f(CW\*(C`shift\*(C'\fR, \f(CW\*(C`unshift\*(C'\fR, and \f(CW\*(C`splice\*(C'\fR cannot be | |
534 | deferred. When you perform one of these operations, any deferred data | |
535 | is written to the file and the operation is performed immediately. | |
536 | This may change in a future version. | |
537 | .PP | |
538 | If you resize the array with deferred writing enabled, the file will | |
539 | be resized immediately, but deferred records will not be written. | |
540 | This has a surprising consequence: \f(CW\*(C`@a = (...)\*(C'\fR erases the file | |
541 | immediately, but the writing of the actual data is deferred. This | |
542 | might be a bug. If it is a bug, it will be fixed in a future version. | |
543 | .Sh "Autodeferring" | |
544 | .IX Subsection "Autodeferring" | |
545 | \&\f(CW\*(C`Tie::File\*(C'\fR tries to guess when deferred writing might be helpful, | |
546 | and to turn it on and off automatically. | |
547 | .PP | |
548 | .Vb 3 | |
549 | \& for (@a) { | |
550 | \& $_ = "> $_"; | |
551 | \& } | |
552 | .Ve | |
553 | .PP | |
554 | In this example, only the first two assignments will be done | |
555 | immediately; after this, all the changes to the file will be deferred | |
556 | up to the user-specified memory limit. | |
557 | .PP | |
558 | You should usually be able to ignore this and just use the module | |
559 | without thinking about deferring. However, special applications may | |
560 | require fine control over which writes are deferred, or may require | |
561 | that all writes be immediate. To disable the autodeferment feature, | |
562 | use | |
563 | .PP | |
564 | .Vb 1 | |
565 | \& (tied @o)->autodefer(0); | |
566 | .Ve | |
567 | .PP | |
568 | or | |
569 | .PP | |
570 | .Vb 1 | |
571 | \& tie @array, 'Tie::File', $file, autodefer => 0; | |
572 | .Ve | |
573 | .PP | |
574 | Similarly, \f(CW\*(C`\->autodefer(1)\*(C'\fR re-enables autodeferment, and | |
575 | \&\f(CW\*(C`\->autodefer()\*(C'\fR recovers the current value of the autodefer setting. | |
576 | .SH "CAVEATS" | |
577 | .IX Header "CAVEATS" | |
578 | (That's Latin for 'warnings'.) | |
579 | .IP "\(bu" 4 | |
580 | This is \s-1BETA\s0 \s-1RELEASE\s0 \s-1SOFTWARE\s0. It may have bugs. See the discussion | |
581 | below about the (lack of any) warranty. | |
582 | .Sp | |
583 | In particular, this means that the interface may change in | |
584 | incompatible ways from one version to the next, without warning. That | |
585 | has happened at least once already. The interface will freeze before | |
586 | Perl 5.8 is released, probably sometime in April 2002. | |
587 | .IP "\(bu" 4 | |
588 | Reasonable effort was made to make this module efficient. Nevertheless, | |
589 | changing the size of a record in the middle of a large file will | |
590 | always be fairly slow, because everything after the new record must be | |
591 | moved. | |
592 | .IP "\(bu" 4 | |
593 | The behavior of tied arrays is not precisely the same as for regular | |
594 | arrays. For example: | |
595 | .Sp | |
596 | .Vb 2 | |
597 | \& # This DOES print "How unusual!" | |
598 | \& undef $a[10]; print "How unusual!\en" if defined $a[10]; | |
599 | .Ve | |
600 | .Sp | |
601 | \&\f(CW\*(C`undef\*(C'\fR\-ing a \f(CW\*(C`Tie::File\*(C'\fR array element just blanks out the | |
602 | corresponding record in the file. When you read it back again, you'll | |
603 | get the empty string, so the supposedly\-\f(CW\*(C`undef\*(C'\fR'ed value will be | |
604 | defined. Similarly, if you have \f(CW\*(C`autochomp\*(C'\fR disabled, then | |
605 | .Sp | |
606 | .Vb 3 | |
607 | \& # This DOES print "How unusual!" if 'autochomp' is disabled | |
608 | \& undef $a[10]; | |
609 | \& print "How unusual!\en" if $a[10]; | |
610 | .Ve | |
611 | .Sp | |
612 | Because when \f(CW\*(C`autochomp\*(C'\fR is disabled, \f(CW$a[10]\fR will read back as | |
613 | \&\f(CW"\en"\fR (or whatever the record separator string is.) | |
614 | .Sp | |
615 | There are other minor differences, particularly regarding \f(CW\*(C`exists\*(C'\fR | |
616 | and \f(CW\*(C`delete\*(C'\fR, but in general, the correspondence is extremely close. | |
617 | .IP "\(bu" 4 | |
618 | Not quite every effort was made to make this module as efficient as | |
619 | possible. \f(CW\*(C`FETCHSIZE\*(C'\fR should use binary search instead of linear | |
620 | search. | |
621 | .Sp | |
622 | The performance of the \f(CW\*(C`flush\*(C'\fR method could be improved. At present, | |
623 | it still rewrites the tail of the file once for each block of | |
624 | contiguous lines to be changed. In the typical case, this will result | |
625 | in only one rewrite, but in peculiar cases it might be bad. It should | |
626 | be possible to perform \fIall\fR deferred writing with a single rewrite. | |
627 | .Sp | |
628 | Profiling suggests that these defects are probably minor; in any | |
629 | event, they will be fixed in a future version of the module. | |
630 | .IP "\(bu" 4 | |
631 | I have supposed that since this module is concerned with file I/O, | |
632 | almost all normal use of it will be heavily I/O bound. This means | |
633 | that the time to maintain complicated data structures inside the | |
634 | module will be dominated by the time to actually perform the I/O. | |
635 | When there was an opportunity to spend \s-1CPU\s0 time to avoid doing I/O, I | |
636 | tried to take it. | |
637 | .IP "\(bu" 4 | |
638 | You might be tempted to think that deferred writing is like | |
639 | transactions, with \f(CW\*(C`flush\*(C'\fR as \f(CW\*(C`commit\*(C'\fR and \f(CW\*(C`discard\*(C'\fR as | |
640 | \&\f(CW\*(C`rollback\*(C'\fR, but it isn't, so don't. | |
641 | .SH "SUBCLASSING" | |
642 | .IX Header "SUBCLASSING" | |
643 | This version promises absolutely nothing about the internals, which | |
644 | may change without notice. A future version of the module will have a | |
645 | well-defined and stable subclassing \s-1API\s0. | |
646 | .ie n .SH "WHAT ABOUT ""DB_File""?" | |
647 | .el .SH "WHAT ABOUT \f(CWDB_File\fP?" | |
648 | .IX Header "WHAT ABOUT DB_File?" | |
649 | People sometimes point out that DB_File will do something similar, | |
650 | and ask why \f(CW\*(C`Tie::File\*(C'\fR module is necessary. | |
651 | .PP | |
652 | There are a number of reasons that you might prefer \f(CW\*(C`Tie::File\*(C'\fR. | |
653 | A list is available at \f(CW\*(C`http://perl.plover.com/TieFile/why\-not\-DB_File\*(C'\fR. | |
654 | .SH "AUTHOR" | |
655 | .IX Header "AUTHOR" | |
656 | Mark Jason Dominus | |
657 | .PP | |
658 | To contact the author, send email to: \f(CW\*(C`mjd\-perl\-tiefile+@plover.com\*(C'\fR | |
659 | .PP | |
660 | To receive an announcement whenever a new version of this module is | |
661 | released, send a blank email message to | |
662 | \&\f(CW\*(C`mjd\-perl\-tiefile\-subscribe@plover.com\*(C'\fR. | |
663 | .PP | |
664 | The most recent version of this module, including documentation and | |
665 | any news of importance, will be available at | |
666 | .PP | |
667 | .Vb 1 | |
668 | \& http://perl.plover.com/TieFile/ | |
669 | .Ve | |
670 | .SH "LICENSE" | |
671 | .IX Header "LICENSE" | |
672 | \&\f(CW\*(C`Tie::File\*(C'\fR version 0.93 is copyright (C) 2002 Mark Jason Dominus. | |
673 | .PP | |
674 | This library is free software; you may redistribute it and/or modify | |
675 | it under the same terms as Perl itself. | |
676 | .PP | |
677 | These terms are your choice of any of (1) the Perl Artistic Licence, | |
678 | or (2) version 2 of the \s-1GNU\s0 General Public License as published by the | |
679 | Free Software Foundation, or (3) any later version of the \s-1GNU\s0 General | |
680 | Public License. | |
681 | .PP | |
682 | This library is distributed in the hope that it will be useful, | |
683 | but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of | |
684 | \&\s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the | |
685 | \&\s-1GNU\s0 General Public License for more details. | |
686 | .PP | |
687 | You should have received a copy of the \s-1GNU\s0 General Public License | |
688 | along with this library program; it should be in the file \f(CW\*(C`COPYING\*(C'\fR. | |
689 | If not, write to the Free Software Foundation, Inc., 59 Temple Place, | |
690 | Suite 330, Boston, \s-1MA\s0 02111 \s-1USA\s0 | |
691 | .PP | |
692 | For licensing inquiries, contact the author at: | |
693 | .PP | |
694 | .Vb 3 | |
695 | \& Mark Jason Dominus | |
696 | \& 255 S. Warnock St. | |
697 | \& Philadelphia, PA 19107 | |
698 | .Ve | |
699 | .SH "WARRANTY" | |
700 | .IX Header "WARRANTY" | |
701 | \&\f(CW\*(C`Tie::File\*(C'\fR version 0.93 comes with \s-1ABSOLUTELY\s0 \s-1NO\s0 \s-1WARRANTY\s0. | |
702 | For details, see the license. | |
703 | .SH "THANKS" | |
704 | .IX Header "THANKS" | |
705 | Gigantic thanks to Jarkko Hietaniemi, for agreeing to put this in the | |
706 | core when I hadn't written it yet, and for generally being helpful, | |
707 | supportive, and competent. (Usually the rule is \*(L"choose any one.\*(R") | |
708 | Also big thanks to Abhijit Menon-Sen for all of the same things. | |
709 | .PP | |
710 | Special thanks to Craig Berry and Peter Prymmer (for \s-1VMS\s0 portability | |
711 | help), Randy Kobes (for Win32 portability help), Clinton Pierce and | |
712 | Autrijus Tang (for heroic eleventh-hour Win32 testing above and beyond | |
713 | the call of duty), Michael G Schwern (for testing advice), and the | |
714 | rest of the \s-1CPAN\s0 testers (for testing generally). | |
715 | .PP | |
716 | Additional thanks to: | |
717 | Edward Avis / | |
718 | Gerrit Haase / | |
719 | Nikola Knezevic / | |
720 | Nick Ing-Simmons / | |
721 | Tassilo von Parseval / | |
722 | H. Dieter Pearcey / | |
723 | Slaven Rezic / | |
724 | Peter Scott / | |
725 | Peter Somu / | |
726 | Autrijus Tang (again) / | |
727 | Tels / | |
728 | Juerd Wallboer | |
729 | .SH "TODO" | |
730 | .IX Header "TODO" | |
731 | More tests. (The cache and heap modules need more unit tests.) | |
732 | .PP | |
733 | Improve \s-1SPLICE\s0 algorithm to use deferred writing machinery. | |
734 | .PP | |
735 | Cleverer strategy for flushing deferred writes. | |
736 | .PP | |
737 | More tests. (Stuff I didn't think of yet.) | |
738 | .PP | |
739 | Paragraph mode? | |
740 | .PP | |
741 | Fixed-length mode. Leave-blanks mode. | |
742 | .PP | |
743 | Maybe an autolocking mode? | |
744 | .PP | |
745 | Record locking with \fIfcntl()\fR? Then the module might support an undo | |
746 | log and get real transactions. What a tour de force that would be. | |
747 | .PP | |
748 | More tests. |