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