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