Commit | Line | Data |
---|---|---|
760ac839 LW |
1 | =head1 NAME |
2 | ||
28757baa | 3 | perlapio - perl's IO abstraction interface. |
760ac839 LW |
4 | |
5 | =head1 SYNOPSIS | |
6 | ||
f185f654 KW |
7 | #define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */ |
8 | #include <perlio.h> /* Usually via #include <perl.h> */ | |
9 | ||
10 | PerlIO *PerlIO_stdin(void); | |
11 | PerlIO *PerlIO_stdout(void); | |
12 | PerlIO *PerlIO_stderr(void); | |
13 | ||
14 | PerlIO *PerlIO_open(const char *path,const char *mode); | |
15 | PerlIO *PerlIO_fdopen(int fd, const char *mode); | |
16 | PerlIO *PerlIO_reopen(const char *path, /* deprecated */ | |
17 | const char *mode, PerlIO *old); | |
18 | int PerlIO_close(PerlIO *f); | |
19 | ||
20 | int PerlIO_stdoutf(const char *fmt,...) | |
21 | int PerlIO_puts(PerlIO *f,const char *string); | |
22 | int PerlIO_putc(PerlIO *f,int ch); | |
23 | SSize_t PerlIO_write(PerlIO *f,const void *buf,size_t numbytes); | |
24 | int PerlIO_printf(PerlIO *f, const char *fmt,...); | |
25 | int PerlIO_vprintf(PerlIO *f, const char *fmt, va_list args); | |
26 | int PerlIO_flush(PerlIO *f); | |
27 | ||
28 | int PerlIO_eof(PerlIO *f); | |
29 | int PerlIO_error(PerlIO *f); | |
30 | void PerlIO_clearerr(PerlIO *f); | |
31 | ||
32 | int PerlIO_getc(PerlIO *d); | |
33 | int PerlIO_ungetc(PerlIO *f,int ch); | |
34 | SSize_t PerlIO_read(PerlIO *f, void *buf, size_t numbytes); | |
35 | ||
36 | int PerlIO_fileno(PerlIO *f); | |
37 | ||
38 | void PerlIO_setlinebuf(PerlIO *f); | |
39 | ||
40 | Off_t PerlIO_tell(PerlIO *f); | |
41 | int PerlIO_seek(PerlIO *f, Off_t offset, int whence); | |
42 | void PerlIO_rewind(PerlIO *f); | |
43 | ||
44 | int PerlIO_getpos(PerlIO *f, SV *save); /* prototype changed */ | |
45 | int PerlIO_setpos(PerlIO *f, SV *saved); /* prototype changed */ | |
46 | ||
47 | int PerlIO_fast_gets(PerlIO *f); | |
48 | int PerlIO_has_cntptr(PerlIO *f); | |
49 | SSize_t PerlIO_get_cnt(PerlIO *f); | |
50 | char *PerlIO_get_ptr(PerlIO *f); | |
51 | void PerlIO_set_ptrcnt(PerlIO *f, char *ptr, SSize_t count); | |
52 | ||
53 | int PerlIO_canset_cnt(PerlIO *f); /* deprecated */ | |
54 | void PerlIO_set_cnt(PerlIO *f, int count); /* deprecated */ | |
55 | ||
56 | int PerlIO_has_base(PerlIO *f); | |
57 | char *PerlIO_get_base(PerlIO *f); | |
58 | SSize_t PerlIO_get_bufsiz(PerlIO *f); | |
59 | ||
60 | PerlIO *PerlIO_importFILE(FILE *stdio, const char *mode); | |
463029d7 | 61 | FILE *PerlIO_exportFILE(PerlIO *f, const char *mode); |
f185f654 KW |
62 | FILE *PerlIO_findFILE(PerlIO *f); |
63 | void PerlIO_releaseFILE(PerlIO *f,FILE *stdio); | |
64 | ||
3329a50e | 65 | int PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, |
f185f654 | 66 | const char *layers); |
3329a50e | 67 | int PerlIO_binmode(pTHX_ PerlIO *f, int ptype, int imode, |
f185f654 | 68 | const char *layers); |
678f21a2 KW |
69 | void PerlIO_debug(const char *fmt,...); |
70 | ||
89222628 | 71 | =for apidoc_section $io |
51b56f5c | 72 | |
678f21a2 KW |
73 | =for apidoc Amh|int |PerlIO_apply_layers|PerlIO *f|const char *mode|const char *layers |
74 | =for apidoc Amh|int |PerlIO_binmode|PerlIO *f|int ptype|int imode|const char *layers | |
75 | =for apidoc ATmh|int |PerlIO_canset_cnt|PerlIO *f | |
76 | =for apidoc Amh|void |PerlIO_debug|const char *fmt|... | |
77 | =for apidoc ATmh|FILE *|PerlIO_exportFILE|PerlIO *f|const char *mode | |
78 | =for apidoc ATmh|int |PerlIO_fast_gets|PerlIO *f | |
79 | =for apidoc ATmh|PerlIO*|PerlIO_fdopen|int fd|const char *mode | |
80 | =for apidoc ATmh|FILE *|PerlIO_findFILE|PerlIO *f | |
81 | =for apidoc ATmh|int |PerlIO_getc|PerlIO *d | |
82 | =for apidoc ATmh|int |PerlIO_getpos|PerlIO *f|SV *save | |
83 | =for apidoc ATmh|int |PerlIO_has_base|PerlIO *f | |
84 | =for apidoc ATmh|int |PerlIO_has_cntptr|PerlIO *f | |
85 | =for apidoc ATmh|PerlIO*|PerlIO_importFILE|FILE *stdio|const char *mode | |
86 | =for apidoc ATmh|PerlIO*|PerlIO_open|const char *path|const char *mode | |
87 | =for apidoc Amh|int |PerlIO_printf|PerlIO *f|const char *fmt|... | |
88 | =for apidoc ATmh|int |PerlIO_putc|PerlIO *f|int ch | |
89 | =for apidoc ATmh|int |PerlIO_puts|PerlIO *f|const char *string | |
90 | =for apidoc ATmh|void |PerlIO_releaseFILE|PerlIO *f|FILE *stdio | |
64defa04 | 91 | =for apidoc ATmh|PerlIO *|PerlIO_reopen|const char *path|const char *mode|PerlIO *old |
678f21a2 KW |
92 | =for apidoc ATmh|void |PerlIO_rewind|PerlIO *f |
93 | =for apidoc ATmh|int |PerlIO_setpos|PerlIO *f|SV *saved | |
94 | =for apidoc Amh|int |PerlIO_stdoutf|const char *fmt|... | |
95 | =for apidoc ATmh|int |PerlIO_ungetc|PerlIO *f|int ch | |
96 | =for apidoc ATmh|int |PerlIO_vprintf|PerlIO *f|const char *fmt|va_list args | |
97 | ||
98 | =for apidoc PerlIO_stdin | |
99 | =for apidoc PerlIO_stdout | |
100 | =for apidoc PerlIO_stderr | |
101 | =for apidoc PerlIO_close | |
102 | =for apidoc PerlIO_write | |
103 | =for apidoc PerlIO_flush | |
104 | =for apidoc PerlIO_eof | |
105 | =for apidoc PerlIO_error | |
106 | =for apidoc PerlIO_clearerr | |
107 | =for apidoc PerlIO_read | |
108 | =for apidoc PerlIO_fileno | |
109 | =for apidoc PerlIO_setlinebuf | |
110 | =for apidoc PerlIO_tell | |
111 | =for apidoc PerlIO_seek | |
112 | =for apidoc PerlIO_get_cnt | |
113 | =for apidoc PerlIO_get_ptr | |
114 | =for apidoc PerlIO_set_ptrcnt | |
115 | =for apidoc PerlIO_set_cnt | |
116 | =for apidoc PerlIO_get_base | |
117 | =for apidoc PerlIO_get_bufsiz | |
760ac839 LW |
118 | |
119 | =head1 DESCRIPTION | |
120 | ||
06936a3c PN |
121 | Perl's source code, and extensions that want maximum portability, |
122 | should use the above functions instead of those defined in ANSI C's | |
123 | I<stdio.h>. The perl headers (in particular "perlio.h") will | |
124 | C<#define> them to the I/O mechanism selected at Configure time. | |
760ac839 LW |
125 | |
126 | The functions are modeled on those in I<stdio.h>, but parameter order | |
127 | has been "tidied up a little". | |
128 | ||
06936a3c PN |
129 | C<PerlIO *> takes the place of FILE *. Like FILE * it should be |
130 | treated as opaque (it is probably safe to assume it is a pointer to | |
131 | something). | |
50b80e25 | 132 | |
1549f383 | 133 | There are currently two implementations: |
50b80e25 | 134 | |
760ac839 LW |
135 | =over 4 |
136 | ||
50b80e25 | 137 | =item 1. USE_STDIO |
760ac839 | 138 | |
06936a3c PN |
139 | All above are #define'd to stdio functions or are trivial wrapper |
140 | functions which call stdio. In this case I<only> PerlIO * is a FILE *. | |
141 | This has been the default implementation since the abstraction was | |
142 | introduced in perl5.003_02. | |
50b80e25 | 143 | |
97cb92d6 | 144 | =item 2. USE_PERLIO |
50b80e25 | 145 | |
06936a3c PN |
146 | Introduced just after perl5.7.0, this is a re-implementation of the |
147 | above abstraction which allows perl more control over how IO is done | |
148 | as it decouples IO from the way the operating system and C library | |
149 | choose to do things. For USE_PERLIO PerlIO * has an extra layer of | |
150 | indirection - it is a pointer-to-a-pointer. This allows the PerlIO * | |
210b36aa | 151 | to remain with a known value while swapping the implementation around |
06936a3c PN |
152 | underneath I<at run time>. In this case all the above are true (but |
153 | very simple) functions which call the underlying implementation. | |
50b80e25 | 154 | |
06936a3c PN |
155 | This is the only implementation for which C<PerlIO_apply_layers()> |
156 | does anything "interesting". | |
50b80e25 NIS |
157 | |
158 | The USE_PERLIO implementation is described in L<perliol>. | |
159 | ||
160 | =back | |
161 | ||
06936a3c | 162 | Because "perlio.h" is a thin layer (for efficiency) the semantics of |
39ac7f1b LS |
163 | these functions are somewhat dependent on the underlying implementation. |
164 | Where these variations are understood they are noted below. | |
50b80e25 | 165 | |
39ac7f1b LS |
166 | Unless otherwise noted, functions return 0 on success, or a negative |
167 | value (usually C<EOF> which is usually -1) and set C<errno> on error. | |
50b80e25 NIS |
168 | |
169 | =over 4 | |
760ac839 LW |
170 | |
171 | =item B<PerlIO_stdin()>, B<PerlIO_stdout()>, B<PerlIO_stderr()> | |
172 | ||
173 | Use these rather than C<stdin>, C<stdout>, C<stderr>. They are written | |
174 | to look like "function calls" rather than variables because this makes | |
54310121 | 175 | it easier to I<make them> function calls if platform cannot export data |
176 | to loaded modules, or if (say) different "threads" might have different | |
760ac839 LW |
177 | values. |
178 | ||
179 | =item B<PerlIO_open(path, mode)>, B<PerlIO_fdopen(fd,mode)> | |
180 | ||
3039a93d | 181 | These correspond to fopen()/fdopen() and the arguments are the same. |
06936a3c PN |
182 | Return C<NULL> and set C<errno> if there is an error. There may be an |
183 | implementation limit on the number of open handles, which may be lower | |
184 | than the limit on the number of open files - C<errno> may not be set | |
210b36aa | 185 | when C<NULL> is returned if this limit is exceeded. |
50b80e25 | 186 | |
11e1c8f2 | 187 | =item B<PerlIO_reopen(path,mode,f)> |
50b80e25 | 188 | |
8cc92bd4 | 189 | While this currently exists in both implementations, perl itself |
50b80e25 NIS |
190 | does not use it. I<As perl does not use it, it is not well tested.> |
191 | ||
06936a3c PN |
192 | Perl prefers to C<dup> the new low-level descriptor to the descriptor |
193 | used by the existing PerlIO. This may become the behaviour of this | |
194 | function in the future. | |
760ac839 LW |
195 | |
196 | =item B<PerlIO_printf(f,fmt,...)>, B<PerlIO_vprintf(f,fmt,a)> | |
197 | ||
7b8d334a | 198 | These are fprintf()/vfprintf() equivalents. |
760ac839 LW |
199 | |
200 | =item B<PerlIO_stdoutf(fmt,...)> | |
201 | ||
202 | This is printf() equivalent. printf is #defined to this function, | |
84dc3c4d | 203 | so it is (currently) legal to use C<printf(fmt,...)> in perl sources. |
760ac839 LW |
204 | |
205 | =item B<PerlIO_read(f,buf,count)>, B<PerlIO_write(f,buf,count)> | |
206 | ||
2c2cb412 GA |
207 | These correspond functionally to fread() and fwrite() but the |
208 | arguments and return values are different. The PerlIO_read() and | |
209 | PerlIO_write() signatures have been modeled on the more sane low level | |
210 | read() and write() functions instead: The "file" argument is passed | |
211 | first, there is only one "count", and the return value can distinguish | |
212 | between error and C<EOF>. | |
213 | ||
214 | Returns a byte count if successful (which may be zero or | |
c0a503cc JH |
215 | positive), returns negative value and sets C<errno> on error. |
216 | Depending on implementation C<errno> may be C<EINTR> if operation was | |
217 | interrupted by a signal. | |
760ac839 LW |
218 | |
219 | =item B<PerlIO_close(f)> | |
220 | ||
06936a3c PN |
221 | Depending on implementation C<errno> may be C<EINTR> if operation was |
222 | interrupted by a signal. | |
50b80e25 | 223 | |
21917246 | 224 | =item B<PerlIO_puts(f,s)>, B<PerlIO_putc(f,c)> |
760ac839 | 225 | |
54310121 | 226 | These correspond to fputs() and fputc(). |
760ac839 LW |
227 | Note that arguments have been revised to have "file" first. |
228 | ||
21917246 | 229 | =item B<PerlIO_ungetc(f,c)> |
760ac839 | 230 | |
06936a3c PN |
231 | This corresponds to ungetc(). Note that arguments have been revised |
232 | to have "file" first. Arranges that next read operation will return | |
233 | the byte B<c>. Despite the implied "character" in the name only | |
234 | values in the range 0..0xFF are defined. Returns the byte B<c> on | |
235 | success or -1 (C<EOF>) on error. The number of bytes that can be | |
236 | "pushed back" may vary, only 1 character is certain, and then only if | |
237 | it is the last character that was read from the handle. | |
760ac839 LW |
238 | |
239 | =item B<PerlIO_getc(f)> | |
240 | ||
241 | This corresponds to getc(). | |
50b80e25 | 242 | Despite the c in the name only byte range 0..0xFF is supported. |
3039a93d | 243 | Returns the character read or -1 (C<EOF>) on error. |
760ac839 LW |
244 | |
245 | =item B<PerlIO_eof(f)> | |
246 | ||
06936a3c PN |
247 | This corresponds to feof(). Returns a true/false indication of |
248 | whether the handle is at end of file. For terminal devices this may | |
249 | or may not be "sticky" depending on the implementation. The flag is | |
250 | cleared by PerlIO_seek(), or PerlIO_rewind(). | |
760ac839 LW |
251 | |
252 | =item B<PerlIO_error(f)> | |
253 | ||
06936a3c PN |
254 | This corresponds to ferror(). Returns a true/false indication of |
255 | whether there has been an IO error on the handle. | |
760ac839 LW |
256 | |
257 | =item B<PerlIO_fileno(f)> | |
258 | ||
06936a3c PN |
259 | This corresponds to fileno(), note that on some platforms, the meaning |
260 | of "fileno" may not match Unix. Returns -1 if the handle has no open | |
261 | descriptor associated with it. | |
760ac839 LW |
262 | |
263 | =item B<PerlIO_clearerr(f)> | |
264 | ||
06936a3c PN |
265 | This corresponds to clearerr(), i.e., clears 'error' and (usually) |
266 | 'eof' flags for the "stream". Does not return a value. | |
760ac839 LW |
267 | |
268 | =item B<PerlIO_flush(f)> | |
269 | ||
06936a3c PN |
270 | This corresponds to fflush(). Sends any buffered write data to the |
271 | underlying file. If called with C<NULL> this may flush all open | |
fa11829f | 272 | streams (or core dump with some USE_STDIO implementations). Calling |
c0a503cc JH |
273 | on a handle open for read only, or on which last operation was a read |
274 | of some kind may lead to undefined behaviour on some USE_STDIO | |
275 | implementations. The USE_PERLIO (layers) implementation tries to | |
276 | behave better: it flushes all open streams when passed C<NULL>, and | |
277 | attempts to retain data on read streams either in the buffer or by | |
278 | seeking the handle to the current logical position. | |
760ac839 | 279 | |
50b80e25 | 280 | =item B<PerlIO_seek(f,offset,whence)> |
760ac839 | 281 | |
06936a3c PN |
282 | This corresponds to fseek(). Sends buffered write data to the |
283 | underlying file, or discards any buffered read data, then positions | |
d7f8936a | 284 | the file descriptor as specified by B<offset> and B<whence> (sic). |
06936a3c PN |
285 | This is the correct thing to do when switching between read and write |
286 | on the same handle (see issues with PerlIO_flush() above). Offset is | |
287 | of type C<Off_t> which is a perl Configure value which may not be same | |
50b80e25 | 288 | as stdio's C<off_t>. |
760ac839 | 289 | |
50b80e25 | 290 | =item B<PerlIO_tell(f)> |
760ac839 | 291 | |
06936a3c PN |
292 | This corresponds to ftell(). Returns the current file position, or |
293 | (Off_t) -1 on error. May just return value system "knows" without | |
294 | making a system call or checking the underlying file descriptor (so | |
295 | use on shared file descriptors is not safe without a | |
296 | PerlIO_seek()). Return value is of type C<Off_t> which is a perl | |
297 | Configure value which may not be same as stdio's C<off_t>. | |
760ac839 LW |
298 | |
299 | =item B<PerlIO_getpos(f,p)>, B<PerlIO_setpos(f,p)> | |
300 | ||
06936a3c PN |
301 | These correspond (loosely) to fgetpos() and fsetpos(). Rather than |
302 | stdio's Fpos_t they expect a "Perl Scalar Value" to be passed. What is | |
303 | stored there should be considered opaque. The layout of the data may | |
304 | vary from handle to handle. When not using stdio or if platform does | |
305 | not have the stdio calls then they are implemented in terms of | |
306 | PerlIO_tell() and PerlIO_seek(). | |
760ac839 LW |
307 | |
308 | =item B<PerlIO_rewind(f)> | |
309 | ||
50b80e25 NIS |
310 | This corresponds to rewind(). It is usually defined as being |
311 | ||
312 | PerlIO_seek(f,(Off_t)0L, SEEK_SET); | |
313 | PerlIO_clearerr(f); | |
314 | ||
760ac839 LW |
315 | =item B<PerlIO_tmpfile()> |
316 | ||
06936a3c PN |
317 | This corresponds to tmpfile(), i.e., returns an anonymous PerlIO or |
318 | NULL on error. The system will attempt to automatically delete the | |
319 | file when closed. On Unix the file is usually C<unlink>-ed just after | |
320 | it is created so it does not matter how it gets closed. On other | |
321 | systems the file may only be deleted if closed via PerlIO_close() | |
322 | and/or the program exits via C<exit>. Depending on the implementation | |
323 | there may be "race conditions" which allow other processes access to | |
324 | the file, though in general it will be safer in this regard than | |
325 | ad. hoc. schemes. | |
50b80e25 NIS |
326 | |
327 | =item B<PerlIO_setlinebuf(f)> | |
328 | ||
06936a3c PN |
329 | This corresponds to setlinebuf(). Does not return a value. What |
330 | constitutes a "line" is implementation dependent but usually means | |
331 | that writing "\n" flushes the buffer. What happens with things like | |
332 | "this\nthat" is uncertain. (Perl core uses it I<only> when "dumping"; | |
333 | it has nothing to do with $| auto-flush.) | |
760ac839 | 334 | |
54310121 | 335 | =back |
760ac839 | 336 | |
510d21e9 | 337 | =head2 Co-existence with stdio |
760ac839 | 338 | |
510d21e9 | 339 | There is outline support for co-existence of PerlIO with stdio. |
06936a3c PN |
340 | Obviously if PerlIO is implemented in terms of stdio there is no |
341 | problem. However in other cases then mechanisms must exist to create a | |
342 | FILE * which can be passed to library code which is going to use stdio | |
343 | calls. | |
50b80e25 | 344 | |
210b36aa | 345 | The first step is to add this line: |
50b80e25 NIS |
346 | |
347 | #define PERLIO_NOT_STDIO 0 | |
348 | ||
06936a3c PN |
349 | I<before> including any perl header files. (This will probably become |
350 | the default at some point). That prevents "perlio.h" from attempting | |
351 | to #define stdio functions onto PerlIO functions. | |
50b80e25 | 352 | |
06936a3c PN |
353 | XS code is probably better using "typemap" if it expects FILE * |
354 | arguments. The standard typemap will be adjusted to comprehend any | |
355 | changes in this area. | |
760ac839 LW |
356 | |
357 | =over 4 | |
358 | ||
4b069b44 | 359 | =item B<PerlIO_importFILE(f,mode)> |
760ac839 | 360 | |
22569500 | 361 | Used to get a PerlIO * from a FILE *. |
760ac839 | 362 | |
c0a503cc JH |
363 | The mode argument should be a string as would be passed to |
364 | fopen/PerlIO_open. If it is NULL then - for legacy support - the code | |
365 | will (depending upon the platform and the implementation) either | |
366 | attempt to empirically determine the mode in which I<f> is open, or | |
367 | use "r+" to indicate a read/write stream. | |
22569500 NIS |
368 | |
369 | Once called the FILE * should I<ONLY> be closed by calling | |
370 | C<PerlIO_close()> on the returned PerlIO *. | |
371 | ||
f504ae08 LC |
372 | The PerlIO is set to textmode. Use PerlIO_binmode if this is |
373 | not the desired mode. | |
374 | ||
b9d6bf13 JH |
375 | This is B<not> the reverse of PerlIO_exportFILE(). |
376 | ||
4b069b44 | 377 | =item B<PerlIO_exportFILE(f,mode)> |
760ac839 | 378 | |
8dcb5783 | 379 | Given a PerlIO * create a 'native' FILE * suitable for passing to code |
c0a503cc JH |
380 | expecting to be compiled and linked with ANSI C I<stdio.h>. The mode |
381 | argument should be a string as would be passed to fopen/PerlIO_open. | |
382 | If it is NULL then - for legacy support - the FILE * is opened in same | |
383 | mode as the PerlIO *. | |
384 | ||
385 | The fact that such a FILE * has been 'exported' is recorded, (normally | |
386 | by pushing a new :stdio "layer" onto the PerlIO *), which may affect | |
387 | future PerlIO operations on the original PerlIO *. You should not | |
388 | call C<fclose()> on the file unless you call C<PerlIO_releaseFILE()> | |
389 | to disassociate it from the PerlIO *. (Do not use PerlIO_importFILE() | |
390 | for doing the disassociation.) | |
760ac839 | 391 | |
22569500 NIS |
392 | Calling this function repeatedly will create a FILE * on each call |
393 | (and will push an :stdio layer each time as well). | |
760ac839 LW |
394 | |
395 | =item B<PerlIO_releaseFILE(p,f)> | |
396 | ||
06936a3c | 397 | Calling PerlIO_releaseFILE informs PerlIO that all use of FILE * is |
65dabbe3 LC |
398 | complete. It is removed from the list of 'exported' FILE *s, and the |
399 | associated PerlIO * should revert to its original behaviour. | |
760ac839 | 400 | |
b9d6bf13 JH |
401 | Use this to disassociate a file from a PerlIO * that was associated |
402 | using PerlIO_exportFILE(). | |
403 | ||
22569500 NIS |
404 | =item B<PerlIO_findFILE(f)> |
405 | ||
406 | Returns a native FILE * used by a stdio layer. If there is none, it | |
407 | will create one with PerlIO_exportFILE. In either case the FILE * | |
65dabbe3 | 408 | should be considered as belonging to PerlIO subsystem and should |
22569500 NIS |
409 | only be closed by calling C<PerlIO_close()>. |
410 | ||
8dcb5783 | 411 | |
760ac839 LW |
412 | =back |
413 | ||
50b80e25 NIS |
414 | =head2 "Fast gets" Functions |
415 | ||
06936a3c PN |
416 | In addition to standard-like API defined so far above there is an |
417 | "implementation" interface which allows perl to get at internals of | |
418 | PerlIO. The following calls correspond to the various FILE_xxx macros | |
419 | determined by Configure - or their equivalent in other | |
420 | implementations. This section is really of interest to only those | |
421 | concerned with detailed perl-core behaviour, implementing a PerlIO | |
422 | mapping or writing code which can make use of the "read ahead" that | |
423 | has been done by the IO system in the same way perl does. Note that | |
424 | any code that uses these interfaces must be prepared to do things the | |
425 | traditional way if a handle does not support them. | |
760ac839 LW |
426 | |
427 | =over 4 | |
428 | ||
50b80e25 | 429 | =item B<PerlIO_fast_gets(f)> |
760ac839 | 430 | |
50b80e25 | 431 | Returns true if implementation has all the interfaces required to |
c0a503cc JH |
432 | allow perl's C<sv_gets> to "bypass" normal IO mechanism. This can |
433 | vary from handle to handle. | |
760ac839 | 434 | |
50b80e25 NIS |
435 | PerlIO_fast_gets(f) = PerlIO_has_cntptr(f) && \ |
436 | PerlIO_canset_cnt(f) && \ | |
ccf3535a | 437 | 'Can set pointer into buffer' |
760ac839 | 438 | |
50b80e25 | 439 | =item B<PerlIO_has_cntptr(f)> |
760ac839 | 440 | |
06936a3c PN |
441 | Implementation can return pointer to current position in the "buffer" |
442 | and a count of bytes available in the buffer. Do not use this - use | |
443 | PerlIO_fast_gets. | |
760ac839 | 444 | |
50b80e25 | 445 | =item B<PerlIO_get_cnt(f)> |
760ac839 | 446 | |
06936a3c PN |
447 | Return count of readable bytes in the buffer. Zero or negative return |
448 | means no more bytes available. | |
760ac839 | 449 | |
50b80e25 | 450 | =item B<PerlIO_get_ptr(f)> |
760ac839 | 451 | |
06936a3c PN |
452 | Return pointer to next readable byte in buffer, accessing via the |
453 | pointer (dereferencing) is only safe if PerlIO_get_cnt() has returned | |
454 | a positive value. Only positive offsets up to value returned by | |
455 | PerlIO_get_cnt() are allowed. | |
760ac839 LW |
456 | |
457 | =item B<PerlIO_set_ptrcnt(f,p,c)> | |
458 | ||
54310121 | 459 | Set pointer into buffer, and a count of bytes still in the |
06936a3c PN |
460 | buffer. Should be used only to set pointer to within range implied by |
461 | previous calls to C<PerlIO_get_ptr> and C<PerlIO_get_cnt>. The two | |
462 | values I<must> be consistent with each other (implementation may only | |
463 | use one or the other or may require both). | |
50b80e25 NIS |
464 | |
465 | =item B<PerlIO_canset_cnt(f)> | |
466 | ||
467 | Implementation can adjust its idea of number of bytes in the buffer. | |
468 | Do not use this - use PerlIO_fast_gets. | |
760ac839 LW |
469 | |
470 | =item B<PerlIO_set_cnt(f,c)> | |
471 | ||
06936a3c PN |
472 | Obscure - set count of bytes in the buffer. Deprecated. Only usable |
473 | if PerlIO_canset_cnt() returns true. Currently used in only doio.c to | |
474 | force count less than -1 to -1. Perhaps should be PerlIO_set_empty or | |
475 | similar. This call may actually do nothing if "count" is deduced from | |
476 | pointer and a "limit". Do not use this - use PerlIO_set_ptrcnt(). | |
760ac839 LW |
477 | |
478 | =item B<PerlIO_has_base(f)> | |
479 | ||
50b80e25 | 480 | Returns true if implementation has a buffer, and can return pointer |
760ac839 LW |
481 | to whole buffer and its size. Used by perl for B<-T> / B<-B> tests. |
482 | Other uses would be very obscure... | |
483 | ||
484 | =item B<PerlIO_get_base(f)> | |
485 | ||
50b80e25 NIS |
486 | Return I<start> of buffer. Access only positive offsets in the buffer |
487 | up to the value returned by PerlIO_get_bufsiz(). | |
760ac839 LW |
488 | |
489 | =item B<PerlIO_get_bufsiz(f)> | |
490 | ||
06936a3c PN |
491 | Return the I<total number of bytes> in the buffer, this is neither the |
492 | number that can be read, nor the amount of memory allocated to the | |
493 | buffer. Rather it is what the operating system and/or implementation | |
494 | happened to C<read()> (or whatever) last time IO was requested. | |
50b80e25 NIS |
495 | |
496 | =back | |
497 | ||
498 | =head2 Other Functions | |
499 | ||
500 | =over 4 | |
501 | ||
3329a50e | 502 | =item PerlIO_apply_layers(aTHX_ f,mode,layers) |
50b80e25 NIS |
503 | |
504 | The new interface to the USE_PERLIO implementation. The layers ":crlf" | |
505 | and ":raw" are only ones allowed for other implementations and those | |
c0a503cc JH |
506 | are silently ignored. (As of perl5.8 ":raw" is deprecated.) Use |
507 | PerlIO_binmode() below for the portable case. | |
50b80e25 | 508 | |
3329a50e | 509 | =item PerlIO_binmode(aTHX_ f,ptype,imode,layers) |
50b80e25 NIS |
510 | |
511 | The hook used by perl's C<binmode> operator. | |
210b36aa | 512 | B<ptype> is perl's character for the kind of IO: |
50b80e25 NIS |
513 | |
514 | =over 8 | |
515 | ||
11e1c8f2 | 516 | =item 'E<lt>' read |
50b80e25 | 517 | |
11e1c8f2 | 518 | =item 'E<gt>' write |
50b80e25 NIS |
519 | |
520 | =item '+' read/write | |
521 | ||
522 | =back | |
523 | ||
524 | B<imode> is C<O_BINARY> or C<O_TEXT>. | |
525 | ||
c0a503cc JH |
526 | B<layers> is a string of layers to apply, only ":crlf" makes sense in |
527 | the non USE_PERLIO case. (As of perl5.8 ":raw" is deprecated in favour | |
528 | of passing NULL.) | |
50b80e25 NIS |
529 | |
530 | Portable cases are: | |
531 | ||
3329a50e | 532 | PerlIO_binmode(aTHX_ f,ptype,O_BINARY,NULL); |
50b80e25 | 533 | and |
3329a50e | 534 | PerlIO_binmode(aTHX_ f,ptype,O_TEXT,":crlf"); |
50b80e25 | 535 | |
06936a3c PN |
536 | On Unix these calls probably have no effect whatsoever. Elsewhere |
537 | they alter "\n" to CR,LF translation and possibly cause a special text | |
538 | "end of file" indicator to be written or honoured on read. The effect | |
539 | of making the call after doing any IO to the handle depends on the | |
540 | implementation. (It may be ignored, affect any data which is already | |
541 | buffered as well, or only apply to subsequent data.) | |
50b80e25 NIS |
542 | |
543 | =item PerlIO_debug(fmt,...) | |
544 | ||
06936a3c PN |
545 | PerlIO_debug is a printf()-like function which can be used for |
546 | debugging. No return value. Its main use is inside PerlIO where using | |
547 | real printf, warn() etc. would recursively call PerlIO and be a | |
548 | problem. | |
50b80e25 | 549 | |
2104c695 CB |
550 | PerlIO_debug writes to the file named by $ENV{'PERLIO_DEBUG'} or defaults |
551 | to stderr if the environment variable is not defined. Typical | |
06936a3c | 552 | use might be |
50b80e25 | 553 | |
ada498b9 | 554 | Bourne shells (sh, ksh, bash, zsh, ash, ...): |
2104c695 | 555 | PERLIO_DEBUG=/tmp/perliodebug.log ./perl -Di somescript some args |
50b80e25 | 556 | |
ada498b9 | 557 | Csh/Tcsh: |
2104c695 CB |
558 | setenv PERLIO_DEBUG /tmp/perliodebug.log |
559 | ./perl -Di somescript some args | |
50b80e25 | 560 | |
ada498b9 | 561 | If you have the "env" utility: |
2104c695 | 562 | env PERLIO_DEBUG=/tmp/perliodebug.log ./perl -Di somescript args |
ada498b9 | 563 | |
50b80e25 | 564 | Win32: |
2104c695 CB |
565 | set PERLIO_DEBUG=perliodebug.log |
566 | perl -Di somescript some args | |
50b80e25 | 567 | |
2104c695 CB |
568 | On a Perl built without C<-DDEBUGGING>, or when the C<-Di> command-line switch |
569 | is not specified, or under taint, PerlIO_debug() is a no-op. | |
760ac839 | 570 | |
54310121 | 571 | =back |