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