Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
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 "2001-09-21" "perl v5.8.8" "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 2 | |
138 | \& # This file documents Tie::File version 0.97 | |
139 | \& use Tie::File; | |
140 | .Ve | |
141 | .PP | |
142 | .Vb 1 | |
143 | \& tie @array, 'Tie::File', filename or die ...; | |
144 | .Ve | |
145 | .PP | |
146 | .Vb 2 | |
147 | \& $array[13] = 'blah'; # line 13 of the file is now 'blah' | |
148 | \& print $array[42]; # display line 42 of the file | |
149 | .Ve | |
150 | .PP | |
151 | .Vb 2 | |
152 | \& $n_recs = @array; # how many records are in the file? | |
153 | \& $#array -= 2; # chop two records off the end | |
154 | .Ve | |
155 | .PP | |
156 | .Vb 3 | |
157 | \& for (@array) { | |
158 | \& s/PERL/Perl/g; # Replace PERL with Perl everywhere in the file | |
159 | \& } | |
160 | .Ve | |
161 | .PP | |
162 | .Vb 2 | |
163 | \& # These are just like regular push, pop, unshift, shift, and splice | |
164 | \& # Except that they modify the file in the way you would expect | |
165 | .Ve | |
166 | .PP | |
167 | .Vb 5 | |
168 | \& push @array, new recs...; | |
169 | \& my $r1 = pop @array; | |
170 | \& unshift @array, new recs...; | |
171 | \& my $r2 = shift @array; | |
172 | \& @old_recs = splice @array, 3, 7, new recs...; | |
173 | .Ve | |
174 | .PP | |
175 | .Vb 1 | |
176 | \& untie @array; # all finished | |
177 | .Ve | |
178 | .SH "DESCRIPTION" | |
179 | .IX Header "DESCRIPTION" | |
180 | \&\f(CW\*(C`Tie::File\*(C'\fR represents a regular text file as a Perl array. Each | |
181 | element in the array corresponds to a record in the file. The first | |
182 | line of the file is element 0 of the array; the second line is element | |
183 | 1, and so on. | |
184 | .PP | |
185 | The file is \fInot\fR loaded into memory, so this will work even for | |
186 | gigantic files. | |
187 | .PP | |
188 | Changes to the array are reflected in the file immediately. | |
189 | .PP | |
190 | Lazy people and beginners may now stop reading the manual. | |
191 | .ie n .Sh """recsep""" | |
192 | .el .Sh "\f(CWrecsep\fP" | |
193 | .IX Subsection "recsep" | |
194 | What is a 'record'? By default, the meaning is the same as for the | |
195 | \&\f(CW\*(C`<...>\*(C'\fR operator: It's a string terminated by \f(CW$/\fR, which is | |
196 | probably \f(CW"\en"\fR. (Minor exception: on \s-1DOS\s0 and Win32 systems, a | |
197 | \&'record' is a string terminated by \f(CW"\er\en"\fR.) You may change the | |
198 | definition of \*(L"record\*(R" by supplying the \f(CW\*(C`recsep\*(C'\fR option in the \f(CW\*(C`tie\*(C'\fR | |
199 | call: | |
200 | .PP | |
201 | .Vb 1 | |
202 | \& tie @array, 'Tie::File', $file, recsep => 'es'; | |
203 | .Ve | |
204 | .PP | |
205 | This says that records are delimited by the string \f(CW\*(C`es\*(C'\fR. If the file | |
206 | contained the following data: | |
207 | .PP | |
208 | .Vb 1 | |
209 | \& Curse these pesky flies!\en | |
210 | .Ve | |
211 | .PP | |
212 | then the \f(CW@array\fR would appear to have four elements: | |
213 | .PP | |
214 | .Vb 4 | |
215 | \& "Curse th" | |
216 | \& "e p" | |
217 | \& "ky fli" | |
218 | \& "!\en" | |
219 | .Ve | |
220 | .PP | |
221 | An undefined value is not permitted as a record separator. Perl's | |
222 | special \*(L"paragraph mode\*(R" semantics (A\*` la \f(CW\*(C`$/ = ""\*(C'\fR) are not | |
223 | emulated. | |
224 | .PP | |
225 | Records read from the tied array do not have the record separator | |
226 | string on the end; this is to allow | |
227 | .PP | |
228 | .Vb 1 | |
229 | \& $array[17] .= "extra"; | |
230 | .Ve | |
231 | .PP | |
232 | to work as expected. | |
233 | .PP | |
234 | (See \*(L"autochomp\*(R", below.) Records stored into the array will have | |
235 | the record separator string appended before they are written to the | |
236 | file, if they don't have one already. For example, if the record | |
237 | separator string is \f(CW"\en"\fR, then the following two lines do exactly | |
238 | the same thing: | |
239 | .PP | |
240 | .Vb 2 | |
241 | \& $array[17] = "Cherry pie"; | |
242 | \& $array[17] = "Cherry pie\en"; | |
243 | .Ve | |
244 | .PP | |
245 | The result is that the contents of line 17 of the file will be | |
246 | replaced with \*(L"Cherry pie\*(R"; a newline character will separate line 17 | |
247 | from line 18. This means that this code will do nothing: | |
248 | .PP | |
249 | .Vb 1 | |
250 | \& chomp $array[17]; | |
251 | .Ve | |
252 | .PP | |
253 | Because the \f(CW\*(C`chomp\*(C'\fRed value will have the separator reattached when | |
254 | it is written back to the file. There is no way to create a file | |
255 | whose trailing record separator string is missing. | |
256 | .PP | |
257 | Inserting records that \fIcontain\fR the record separator string is not | |
258 | supported by this module. It will probably produce a reasonable | |
259 | result, but what this result will be may change in a future version. | |
260 | Use 'splice' to insert records or to replace one record with several. | |
261 | .ie n .Sh """autochomp""" | |
262 | .el .Sh "\f(CWautochomp\fP" | |
263 | .IX Subsection "autochomp" | |
264 | Normally, array elements have the record separator removed, so that if | |
265 | the file contains the text | |
266 | .PP | |
267 | .Vb 3 | |
268 | \& Gold | |
269 | \& Frankincense | |
270 | \& Myrrh | |
271 | .Ve | |
272 | .PP | |
273 | the tied array will appear to contain \f(CW\*(C`("Gold", "Frankincense", | |
274 | "Myrrh")\*(C'\fR. If you set \f(CW\*(C`autochomp\*(C'\fR to a false value, the record | |
275 | separator will not be removed. If the file above was tied with | |
276 | .PP | |
277 | .Vb 1 | |
278 | \& tie @gifts, "Tie::File", $gifts, autochomp => 0; | |
279 | .Ve | |
280 | .PP | |
281 | then the array \f(CW@gifts\fR would appear to contain \f(CW\*(C`("Gold\en", | |
282 | "Frankincense\en", "Myrrh\en")\*(C'\fR, or (on Win32 systems) \f(CW\*(C`("Gold\er\en", | |
283 | "Frankincense\er\en", "Myrrh\er\en")\*(C'\fR. | |
284 | .ie n .Sh """mode""" | |
285 | .el .Sh "\f(CWmode\fP" | |
286 | .IX Subsection "mode" | |
287 | Normally, the specified file will be opened for read and write access, | |
288 | and will be created if it does not exist. (That is, the flags | |
289 | \&\f(CW\*(C`O_RDWR | O_CREAT\*(C'\fR are supplied in the \f(CW\*(C`open\*(C'\fR call.) If you want to | |
290 | change this, you may supply alternative flags in the \f(CW\*(C`mode\*(C'\fR option. | |
291 | See Fcntl for a listing of available flags. | |
292 | For example: | |
293 | .PP | |
294 | .Vb 3 | |
295 | \& # open the file if it exists, but fail if it does not exist | |
296 | \& use Fcntl 'O_RDWR'; | |
297 | \& tie @array, 'Tie::File', $file, mode => O_RDWR; | |
298 | .Ve | |
299 | .PP | |
300 | .Vb 3 | |
301 | \& # create the file if it does not exist | |
302 | \& use Fcntl 'O_RDWR', 'O_CREAT'; | |
303 | \& tie @array, 'Tie::File', $file, mode => O_RDWR | O_CREAT; | |
304 | .Ve | |
305 | .PP | |
306 | .Vb 3 | |
307 | \& # open an existing file in read-only mode | |
308 | \& use Fcntl 'O_RDONLY'; | |
309 | \& tie @array, 'Tie::File', $file, mode => O_RDONLY; | |
310 | .Ve | |
311 | .PP | |
312 | Opening the data file in write-only or append mode is not supported. | |
313 | .ie n .Sh """memory""" | |
314 | .el .Sh "\f(CWmemory\fP" | |
315 | .IX Subsection "memory" | |
316 | This is an upper limit on the amount of memory that \f(CW\*(C`Tie::File\*(C'\fR will | |
317 | consume at any time while managing the file. This is used for two | |
318 | things: managing the \fIread cache\fR and managing the \fIdeferred write | |
319 | buffer\fR. | |
320 | .PP | |
321 | Records read in from the file are cached, to avoid having to re-read | |
322 | them repeatedly. If you read the same record twice, the first time it | |
323 | will be stored in memory, and the second time it will be fetched from | |
324 | the \fIread cache\fR. The amount of data in the read cache will not | |
325 | exceed the value you specified for \f(CW\*(C`memory\*(C'\fR. If \f(CW\*(C`Tie::File\*(C'\fR wants | |
326 | to cache a new record, but the read cache is full, it will make room | |
327 | by expiring the least-recently visited records from the read cache. | |
328 | .PP | |
329 | The default memory limit is 2Mib. You can adjust the maximum read | |
330 | cache size by supplying the \f(CW\*(C`memory\*(C'\fR option. The argument is the | |
331 | desired cache size, in bytes. | |
332 | .PP | |
333 | .Vb 2 | |
334 | \& # I have a lot of memory, so use a large cache to speed up access | |
335 | \& tie @array, 'Tie::File', $file, memory => 20_000_000; | |
336 | .Ve | |
337 | .PP | |
338 | Setting the memory limit to 0 will inhibit caching; records will be | |
339 | fetched from disk every time you examine them. | |
340 | .PP | |
341 | The \f(CW\*(C`memory\*(C'\fR value is not an absolute or exact limit on the memory | |
342 | used. \f(CW\*(C`Tie::File\*(C'\fR objects contains some structures besides the read | |
343 | cache and the deferred write buffer, whose sizes are not charged | |
344 | against \f(CW\*(C`memory\*(C'\fR. | |
345 | .PP | |
346 | The cache itself consumes about 310 bytes per cached record, so if | |
347 | your file has many short records, you may want to decrease the cache | |
348 | memory limit, or else the cache overhead may exceed the size of the | |
349 | cached data. | |
350 | .ie n .Sh """dw_size""" | |
351 | .el .Sh "\f(CWdw_size\fP" | |
352 | .IX Subsection "dw_size" | |
353 | (This is an advanced feature. Skip this section on first reading.) | |
354 | .PP | |
355 | If you use deferred writing (See \*(L"Deferred Writing\*(R", below) then | |
356 | data you write into the array will not be written directly to the | |
357 | file; instead, it will be saved in the \fIdeferred write buffer\fR to be | |
358 | written out later. Data in the deferred write buffer is also charged | |
359 | against the memory limit you set with the \f(CW\*(C`memory\*(C'\fR option. | |
360 | .PP | |
361 | You may set the \f(CW\*(C`dw_size\*(C'\fR option to limit the amount of data that can | |
362 | be saved in the deferred write buffer. This limit may not exceed the | |
363 | total memory limit. For example, if you set \f(CW\*(C`dw_size\*(C'\fR to 1000 and | |
364 | \&\f(CW\*(C`memory\*(C'\fR to 2500, that means that no more than 1000 bytes of deferred | |
365 | writes will be saved up. The space available for the read cache will | |
366 | vary, but it will always be at least 1500 bytes (if the deferred write | |
367 | buffer is full) and it could grow as large as 2500 bytes (if the | |
368 | deferred write buffer is empty.) | |
369 | .PP | |
370 | If you don't specify a \f(CW\*(C`dw_size\*(C'\fR, it defaults to the entire memory | |
371 | limit. | |
372 | .Sh "Option Format" | |
373 | .IX Subsection "Option Format" | |
374 | \&\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 | |
375 | \&\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 | |
376 | idea. | |
377 | .SH "Public Methods" | |
378 | .IX Header "Public Methods" | |
379 | The \f(CW\*(C`tie\*(C'\fR call returns an object, say \f(CW$o\fR. You may call | |
380 | .PP | |
381 | .Vb 2 | |
382 | \& $rec = $o->FETCH($n); | |
383 | \& $o->STORE($n, $rec); | |
384 | .Ve | |
385 | .PP | |
386 | to fetch or store the record at line \f(CW$n\fR, respectively; similarly | |
387 | the other tied array methods. (See perltie for details.) You may | |
388 | also call the following methods on this object: | |
389 | .ie n .Sh """flock""" | |
390 | .el .Sh "\f(CWflock\fP" | |
391 | .IX Subsection "flock" | |
392 | .Vb 1 | |
393 | \& $o->flock(MODE) | |
394 | .Ve | |
395 | .PP | |
396 | will lock the tied file. \f(CW\*(C`MODE\*(C'\fR has the same meaning as the second | |
397 | argument to the Perl built-in \f(CW\*(C`flock\*(C'\fR function; for example | |
398 | \&\f(CW\*(C`LOCK_SH\*(C'\fR or \f(CW\*(C`LOCK_EX | LOCK_NB\*(C'\fR. (These constants are provided by | |
399 | the \f(CW\*(C`use Fcntl ':flock'\*(C'\fR declaration.) | |
400 | .PP | |
401 | \&\f(CW\*(C`MODE\*(C'\fR is optional; the default is \f(CW\*(C`LOCK_EX\*(C'\fR. | |
402 | .PP | |
403 | \&\f(CW\*(C`Tie::File\*(C'\fR maintains an internal table of the byte offset of each | |
404 | record it has seen in the file. | |
405 | .PP | |
406 | When you use \f(CW\*(C`flock\*(C'\fR to lock the file, \f(CW\*(C`Tie::File\*(C'\fR assumes that the | |
407 | read cache is no longer trustworthy, because another process might | |
408 | have modified the file since the last time it was read. Therefore, a | |
409 | successful call to \f(CW\*(C`flock\*(C'\fR discards the contents of the read cache | |
410 | and the internal record offset table. | |
411 | .PP | |
412 | \&\f(CW\*(C`Tie::File\*(C'\fR promises that the following sequence of operations will | |
413 | be safe: | |
414 | .PP | |
415 | .Vb 2 | |
416 | \& my $o = tie @array, "Tie::File", $filename; | |
417 | \& $o->flock; | |
418 | .Ve | |
419 | .PP | |
420 | In particular, \f(CW\*(C`Tie::File\*(C'\fR will \fInot\fR read or write the file during | |
421 | the \f(CW\*(C`tie\*(C'\fR call. (Exception: Using \f(CW\*(C`mode => O_TRUNC\*(C'\fR will, of | |
422 | course, erase the file during the \f(CW\*(C`tie\*(C'\fR call. If you want to do this | |
423 | safely, then open the file without \f(CW\*(C`O_TRUNC\*(C'\fR, lock the file, and use | |
424 | \&\f(CW\*(C`@array = ()\*(C'\fR.) | |
425 | .PP | |
426 | The best way to unlock a file is to discard the object and untie the | |
427 | array. It is probably unsafe to unlock the file without also untying | |
428 | it, because if you do, changes may remain unwritten inside the object. | |
429 | That is why there is no shortcut for unlocking. If you really want to | |
430 | unlock the file prematurely, you know what to do; if you don't know | |
431 | what to do, then don't do it. | |
432 | .PP | |
433 | All the usual warnings about file locking apply here. In particular, | |
434 | note that file locking in Perl is \fBadvisory\fR, which means that | |
435 | holding a lock will not prevent anyone else from reading, writing, or | |
436 | erasing the file; it only prevents them from getting another lock at | |
437 | the same time. Locks are analogous to green traffic lights: If you | |
438 | have a green light, that does not prevent the idiot coming the other | |
439 | way from plowing into you sideways; it merely guarantees to you that | |
440 | the idiot does not also have a green light at the same time. | |
441 | .ie n .Sh """autochomp""" | |
442 | .el .Sh "\f(CWautochomp\fP" | |
443 | .IX Subsection "autochomp" | |
444 | .Vb 2 | |
445 | \& my $old_value = $o->autochomp(0); # disable autochomp option | |
446 | \& my $old_value = $o->autochomp(1); # enable autochomp option | |
447 | .Ve | |
448 | .PP | |
449 | .Vb 1 | |
450 | \& my $ac = $o->autochomp(); # recover current value | |
451 | .Ve | |
452 | .PP | |
453 | See \*(L"autochomp\*(R", above. | |
454 | .ie n .Sh """defer""\fP, \f(CW""flush""\fP, \f(CW""discard""\fP, and \f(CW""autodefer""" | |
455 | .el .Sh "\f(CWdefer\fP, \f(CWflush\fP, \f(CWdiscard\fP, and \f(CWautodefer\fP" | |
456 | .IX Subsection "defer, flush, discard, and autodefer" | |
457 | See \*(L"Deferred Writing\*(R", below. | |
458 | .ie n .Sh """offset""" | |
459 | .el .Sh "\f(CWoffset\fP" | |
460 | .IX Subsection "offset" | |
461 | .Vb 1 | |
462 | \& $off = $o->offset($n); | |
463 | .Ve | |
464 | .PP | |
465 | This method returns the byte offset of the start of the \f(CW$n\fRth record | |
466 | in the file. If there is no such record, it returns an undefined | |
467 | value. | |
468 | .SH "Tying to an already-opened filehandle" | |
469 | .IX Header "Tying to an already-opened filehandle" | |
470 | If \f(CW$fh\fR is a filehandle, such as is returned by \f(CW\*(C`IO::File\*(C'\fR or one | |
471 | of the other \f(CW\*(C`IO\*(C'\fR modules, you may use: | |
472 | .PP | |
473 | .Vb 1 | |
474 | \& tie @array, 'Tie::File', $fh, ...; | |
475 | .Ve | |
476 | .PP | |
477 | Similarly if you opened that handle \f(CW\*(C`FH\*(C'\fR with regular \f(CW\*(C`open\*(C'\fR or | |
478 | \&\f(CW\*(C`sysopen\*(C'\fR, you may use: | |
479 | .PP | |
480 | .Vb 1 | |
481 | \& tie @array, 'Tie::File', \e*FH, ...; | |
482 | .Ve | |
483 | .PP | |
484 | Handles that were opened write-only won't work. Handles that were | |
485 | opened read-only will work as long as you don't try to modify the | |
486 | array. Handles must be attached to seekable sources of data\-\-\-that | |
487 | means no pipes or sockets. If \f(CW\*(C`Tie::File\*(C'\fR can detect that you | |
488 | supplied a non-seekable handle, the \f(CW\*(C`tie\*(C'\fR call will throw an | |
489 | exception. (On Unix systems, it can detect this.) | |
490 | .PP | |
491 | Note that Tie::File will only close any filehandles that it opened | |
492 | internally. If you passed it a filehandle as above, you \*(L"own\*(R" the | |
493 | filehandle, and are responsible for closing it after you have untied | |
494 | the \f(CW@array\fR. | |
495 | .SH "Deferred Writing" | |
496 | .IX Header "Deferred Writing" | |
497 | (This is an advanced feature. Skip this section on first reading.) | |
498 | .PP | |
499 | Normally, modifying a \f(CW\*(C`Tie::File\*(C'\fR array writes to the underlying file | |
500 | immediately. Every assignment like \f(CW\*(C`$a[3] = ...\*(C'\fR rewrites as much of | |
501 | the file as is necessary; typically, everything from line 3 through | |
502 | the end will need to be rewritten. This is the simplest and most | |
503 | transparent behavior. Performance even for large files is reasonably | |
504 | good. | |
505 | .PP | |
506 | However, under some circumstances, this behavior may be excessively | |
507 | slow. For example, suppose you have a million-record file, and you | |
508 | want to do: | |
509 | .PP | |
510 | .Vb 3 | |
511 | \& for (@FILE) { | |
512 | \& $_ = "> $_"; | |
513 | \& } | |
514 | .Ve | |
515 | .PP | |
516 | The first time through the loop, you will rewrite the entire file, | |
517 | from line 0 through the end. The second time through the loop, you | |
518 | will rewrite the entire file from line 1 through the end. The third | |
519 | time through the loop, you will rewrite the entire file from line 2 to | |
520 | the end. And so on. | |
521 | .PP | |
522 | If the performance in such cases is unacceptable, you may defer the | |
523 | actual writing, and then have it done all at once. The following loop | |
524 | will perform much better for large files: | |
525 | .PP | |
526 | .Vb 5 | |
527 | \& (tied @a)->defer; | |
528 | \& for (@a) { | |
529 | \& $_ = "> $_"; | |
530 | \& } | |
531 | \& (tied @a)->flush; | |
532 | .Ve | |
533 | .PP | |
534 | If \f(CW\*(C`Tie::File\*(C'\fR's memory limit is large enough, all the writing will | |
535 | done in memory. Then, when you call \f(CW\*(C`\->flush\*(C'\fR, the entire file | |
536 | will be rewritten in a single pass. | |
537 | .PP | |
538 | (Actually, the preceding discussion is something of a fib. You don't | |
539 | need to enable deferred writing to get good performance for this | |
540 | common case, because \f(CW\*(C`Tie::File\*(C'\fR will do it for you automatically | |
541 | unless you specifically tell it not to. See \*(L"autodeferring\*(R", | |
542 | below.) | |
543 | .PP | |
544 | Calling \f(CW\*(C`\->flush\*(C'\fR returns the array to immediate-write mode. If | |
545 | you wish to discard the deferred writes, you may call \f(CW\*(C`\->discard\*(C'\fR | |
546 | instead of \f(CW\*(C`\->flush\*(C'\fR. Note that in some cases, some of the data | |
547 | will have been written already, and it will be too late for | |
548 | \&\f(CW\*(C`\->discard\*(C'\fR to discard all the changes. Support for | |
549 | \&\f(CW\*(C`\->discard\*(C'\fR may be withdrawn in a future version of \f(CW\*(C`Tie::File\*(C'\fR. | |
550 | .PP | |
551 | Deferred writes are cached in memory up to the limit specified by the | |
552 | \&\f(CW\*(C`dw_size\*(C'\fR option (see above). If the deferred-write buffer is full | |
553 | and you try to write still more deferred data, the buffer will be | |
554 | flushed. All buffered data will be written immediately, the buffer | |
555 | will be emptied, and the now-empty space will be used for future | |
556 | deferred writes. | |
557 | .PP | |
558 | If the deferred-write buffer isn't yet full, but the total size of the | |
559 | buffer and the read cache would exceed the \f(CW\*(C`memory\*(C'\fR limit, the oldest | |
560 | records will be expired from the read cache until the total size is | |
561 | under the limit. | |
562 | .PP | |
563 | \&\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 | |
564 | deferred. When you perform one of these operations, any deferred data | |
565 | is written to the file and the operation is performed immediately. | |
566 | This may change in a future version. | |
567 | .PP | |
568 | If you resize the array with deferred writing enabled, the file will | |
569 | be resized immediately, but deferred records will not be written. | |
570 | This has a surprising consequence: \f(CW\*(C`@a = (...)\*(C'\fR erases the file | |
571 | immediately, but the writing of the actual data is deferred. This | |
572 | might be a bug. If it is a bug, it will be fixed in a future version. | |
573 | .Sh "Autodeferring" | |
574 | .IX Subsection "Autodeferring" | |
575 | \&\f(CW\*(C`Tie::File\*(C'\fR tries to guess when deferred writing might be helpful, | |
576 | and to turn it on and off automatically. | |
577 | .PP | |
578 | .Vb 3 | |
579 | \& for (@a) { | |
580 | \& $_ = "> $_"; | |
581 | \& } | |
582 | .Ve | |
583 | .PP | |
584 | In this example, only the first two assignments will be done | |
585 | immediately; after this, all the changes to the file will be deferred | |
586 | up to the user-specified memory limit. | |
587 | .PP | |
588 | You should usually be able to ignore this and just use the module | |
589 | without thinking about deferring. However, special applications may | |
590 | require fine control over which writes are deferred, or may require | |
591 | that all writes be immediate. To disable the autodeferment feature, | |
592 | use | |
593 | .PP | |
594 | .Vb 1 | |
595 | \& (tied @o)->autodefer(0); | |
596 | .Ve | |
597 | .PP | |
598 | or | |
599 | .PP | |
600 | .Vb 1 | |
601 | \& tie @array, 'Tie::File', $file, autodefer => 0; | |
602 | .Ve | |
603 | .PP | |
604 | Similarly, \f(CW\*(C`\->autodefer(1)\*(C'\fR re-enables autodeferment, and | |
605 | \&\f(CW\*(C`\->autodefer()\*(C'\fR recovers the current value of the autodefer setting. | |
606 | .SH "CONCURRENT ACCESS TO FILES" | |
607 | .IX Header "CONCURRENT ACCESS TO FILES" | |
608 | Caching and deferred writing are inappropriate if you want the same | |
609 | file to be accessed simultaneously from more than one process. Other | |
610 | optimizations performed internally by this module are also | |
611 | incompatible with concurrent access. A future version of this module will | |
612 | support a \f(CW\*(C`concurrent => 1\*(C'\fR option that enables safe concurrent access. | |
613 | .PP | |
614 | Previous versions of this documentation suggested using \f(CW\*(C`memory | |
615 | => 0\*(C'\fR for safe concurrent access. This was mistaken. Tie::File | |
616 | will not support safe concurrent access before version 0.98. | |
617 | .SH "CAVEATS" | |
618 | .IX Header "CAVEATS" | |
619 | (That's Latin for 'warnings'.) | |
620 | .IP "\(bu" 4 | |
621 | Reasonable effort was made to make this module efficient. Nevertheless, | |
622 | changing the size of a record in the middle of a large file will | |
623 | always be fairly slow, because everything after the new record must be | |
624 | moved. | |
625 | .IP "\(bu" 4 | |
626 | The behavior of tied arrays is not precisely the same as for regular | |
627 | arrays. For example: | |
628 | .Sp | |
629 | .Vb 2 | |
630 | \& # This DOES print "How unusual!" | |
631 | \& undef $a[10]; print "How unusual!\en" if defined $a[10]; | |
632 | .Ve | |
633 | .Sp | |
634 | \&\f(CW\*(C`undef\*(C'\fR\-ing a \f(CW\*(C`Tie::File\*(C'\fR array element just blanks out the | |
635 | corresponding record in the file. When you read it back again, you'll | |
636 | get the empty string, so the supposedly\-\f(CW\*(C`undef\*(C'\fR'ed value will be | |
637 | defined. Similarly, if you have \f(CW\*(C`autochomp\*(C'\fR disabled, then | |
638 | .Sp | |
639 | .Vb 3 | |
640 | \& # This DOES print "How unusual!" if 'autochomp' is disabled | |
641 | \& undef $a[10]; | |
642 | \& print "How unusual!\en" if $a[10]; | |
643 | .Ve | |
644 | .Sp | |
645 | Because when \f(CW\*(C`autochomp\*(C'\fR is disabled, \f(CW$a[10]\fR will read back as | |
646 | \&\f(CW"\en"\fR (or whatever the record separator string is.) | |
647 | .Sp | |
648 | There are other minor differences, particularly regarding \f(CW\*(C`exists\*(C'\fR | |
649 | and \f(CW\*(C`delete\*(C'\fR, but in general, the correspondence is extremely close. | |
650 | .IP "\(bu" 4 | |
651 | I have supposed that since this module is concerned with file I/O, | |
652 | almost all normal use of it will be heavily I/O bound. This means | |
653 | that the time to maintain complicated data structures inside the | |
654 | module will be dominated by the time to actually perform the I/O. | |
655 | When there was an opportunity to spend \s-1CPU\s0 time to avoid doing I/O, I | |
656 | usually tried to take it. | |
657 | .IP "\(bu" 4 | |
658 | You might be tempted to think that deferred writing is like | |
659 | transactions, with \f(CW\*(C`flush\*(C'\fR as \f(CW\*(C`commit\*(C'\fR and \f(CW\*(C`discard\*(C'\fR as | |
660 | \&\f(CW\*(C`rollback\*(C'\fR, but it isn't, so don't. | |
661 | .IP "\(bu" 4 | |
662 | There is a large memory overhead for each record offset and for each | |
663 | cache entry: about 310 bytes per cached data record, and about 21 bytes per offset table entry. | |
664 | .Sp | |
665 | The per-record overhead will limit the maximum number of records you | |
666 | can access per file. Note that \fIaccessing\fR the length of the array | |
667 | via \f(CW\*(C`$x = scalar @tied_file\*(C'\fR accesses \fBall\fR records and stores their | |
668 | offsets. The same for \f(CW\*(C`foreach (@tied_file)\*(C'\fR, even if you exit the | |
669 | loop early. | |
670 | .SH "SUBCLASSING" | |
671 | .IX Header "SUBCLASSING" | |
672 | This version promises absolutely nothing about the internals, which | |
673 | may change without notice. A future version of the module will have a | |
674 | well-defined and stable subclassing \s-1API\s0. | |
675 | .ie n .SH "WHAT ABOUT ""DB_File""?" | |
676 | .el .SH "WHAT ABOUT \f(CWDB_File\fP?" | |
677 | .IX Header "WHAT ABOUT DB_File?" | |
678 | People sometimes point out that DB_File will do something similar, | |
679 | and ask why \f(CW\*(C`Tie::File\*(C'\fR module is necessary. | |
680 | .PP | |
681 | There are a number of reasons that you might prefer \f(CW\*(C`Tie::File\*(C'\fR. | |
682 | A list is available at \f(CW\*(C`http://perl.plover.com/TieFile/why\-not\-DB_File\*(C'\fR. | |
683 | .SH "AUTHOR" | |
684 | .IX Header "AUTHOR" | |
685 | Mark Jason Dominus | |
686 | .PP | |
687 | To contact the author, send email to: \f(CW\*(C`mjd\-perl\-tiefile+@plover.com\*(C'\fR | |
688 | .PP | |
689 | To receive an announcement whenever a new version of this module is | |
690 | released, send a blank email message to | |
691 | \&\f(CW\*(C`mjd\-perl\-tiefile\-subscribe@plover.com\*(C'\fR. | |
692 | .PP | |
693 | The most recent version of this module, including documentation and | |
694 | any news of importance, will be available at | |
695 | .PP | |
696 | .Vb 1 | |
697 | \& http://perl.plover.com/TieFile/ | |
698 | .Ve | |
699 | .SH "LICENSE" | |
700 | .IX Header "LICENSE" | |
701 | \&\f(CW\*(C`Tie::File\*(C'\fR version 0.97 is copyright (C) 2003 Mark Jason Dominus. | |
702 | .PP | |
703 | This library is free software; you may redistribute it and/or modify | |
704 | it under the same terms as Perl itself. | |
705 | .PP | |
706 | These terms are your choice of any of (1) the Perl Artistic Licence, | |
707 | or (2) version 2 of the \s-1GNU\s0 General Public License as published by the | |
708 | Free Software Foundation, or (3) any later version of the \s-1GNU\s0 General | |
709 | Public License. | |
710 | .PP | |
711 | This library is distributed in the hope that it will be useful, | |
712 | but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of | |
713 | \&\s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the | |
714 | \&\s-1GNU\s0 General Public License for more details. | |
715 | .PP | |
716 | You should have received a copy of the \s-1GNU\s0 General Public License | |
717 | along with this library program; it should be in the file \f(CW\*(C`COPYING\*(C'\fR. | |
718 | If not, write to the Free Software Foundation, Inc., 59 Temple Place, | |
719 | Suite 330, Boston, \s-1MA\s0 02111 \s-1USA\s0 | |
720 | .PP | |
721 | For licensing inquiries, contact the author at: | |
722 | .PP | |
723 | .Vb 3 | |
724 | \& Mark Jason Dominus | |
725 | \& 255 S. Warnock St. | |
726 | \& Philadelphia, PA 19107 | |
727 | .Ve | |
728 | .SH "WARRANTY" | |
729 | .IX Header "WARRANTY" | |
730 | \&\f(CW\*(C`Tie::File\*(C'\fR version 0.97 comes with \s-1ABSOLUTELY\s0 \s-1NO\s0 \s-1WARRANTY\s0. | |
731 | For details, see the license. | |
732 | .SH "THANKS" | |
733 | .IX Header "THANKS" | |
734 | Gigantic thanks to Jarkko Hietaniemi, for agreeing to put this in the | |
735 | core when I hadn't written it yet, and for generally being helpful, | |
736 | supportive, and competent. (Usually the rule is \*(L"choose any one.\*(R") | |
737 | Also big thanks to Abhijit Menon-Sen for all of the same things. | |
738 | .PP | |
739 | Special thanks to Craig Berry and Peter Prymmer (for \s-1VMS\s0 portability | |
740 | help), Randy Kobes (for Win32 portability help), Clinton Pierce and | |
741 | Autrijus Tang (for heroic eleventh-hour Win32 testing above and beyond | |
742 | the call of duty), Michael G Schwern (for testing advice), and the | |
743 | rest of the \s-1CPAN\s0 testers (for testing generally). | |
744 | .PP | |
745 | Special thanks to Tels for suggesting several speed and memory | |
746 | optimizations. | |
747 | .PP | |
748 | Additional thanks to: | |
749 | Edward Avis / | |
750 | Mattia Barbon / | |
751 | Tom Christiansen / | |
752 | Gerrit Haase / | |
753 | Gurusamy Sarathy / | |
754 | Jarkko Hietaniemi (again) / | |
755 | Nikola Knezevic / | |
756 | John Kominetz / | |
757 | Nick Ing-Simmons / | |
758 | Tassilo von Parseval / | |
759 | H. Dieter Pearcey / | |
760 | Slaven Rezic / | |
761 | Eric Roode / | |
762 | Peter Scott / | |
763 | Peter Somu / | |
764 | Autrijus Tang (again) / | |
765 | Tels (again) / | |
766 | Juerd Waalboer | |
767 | .SH "TODO" | |
768 | .IX Header "TODO" | |
769 | More tests. (Stuff I didn't think of yet.) | |
770 | .PP | |
771 | Paragraph mode? | |
772 | .PP | |
773 | Fixed-length mode. Leave-blanks mode. | |
774 | .PP | |
775 | Maybe an autolocking mode? | |
776 | .PP | |
777 | For many common uses of the module, the read cache is a liability. | |
778 | For example, a program that inserts a single record, or that scans the | |
779 | file once, will have a cache hit rate of zero. This suggests a major | |
780 | optimization: The cache should be initially disabled. Here's a hybrid | |
781 | approach: Initially, the cache is disabled, but the cache code | |
782 | maintains statistics about how high the hit rate would be *if* it were | |
783 | enabled. When it sees the hit rate get high enough, it enables | |
784 | itself. The \s-1STAT\s0 comments in this code are the beginning of an | |
785 | implementation of this. | |
786 | .PP | |
787 | Record locking with \fIfcntl()\fR? Then the module might support an undo | |
788 | log and get real transactions. What a tour de force that would be. | |
789 | .PP | |
790 | Keeping track of the highest cached record. This would allow reads-in-a-row | |
791 | to skip the cache lookup faster (if reading from 1..N with empty cache at | |
792 | start, the last cached value will be always N\-1). | |
793 | .PP | |
794 | More tests. |