1PERLAPIO(1)            Perl Programmers Reference Guide            PERLAPIO(1)
2
3
4

NAME

6       perlapio - perl's IO abstraction interface.
7

SYNOPSIS

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, /* deprecated */
19                 const char *mode, PerlIO *old);
20         int     PerlIO_close(PerlIO *f);
21
22         int     PerlIO_stdoutf(const char *fmt,...)
23         int     PerlIO_puts(PerlIO *f,const char *string);
24         int     PerlIO_putc(PerlIO *f,int ch);
25         SSize_t PerlIO_write(PerlIO *f,const void *buf,size_t numbytes);
26         int     PerlIO_printf(PerlIO *f, const char *fmt,...);
27         int     PerlIO_vprintf(PerlIO *f, const char *fmt, va_list args);
28         int     PerlIO_flush(PerlIO *f);
29
30         int     PerlIO_fill(PerlIO *f);
31         int     PerlIO_eof(PerlIO *f);
32         int     PerlIO_error(PerlIO *f);
33         void    PerlIO_clearerr(PerlIO *f);
34
35         int     PerlIO_getc(PerlIO *d);
36         int     PerlIO_ungetc(PerlIO *f,int ch);
37         SSize_t PerlIO_read(PerlIO *f, void *buf, size_t numbytes);
38         Size_t  PerlIO_unread(PerlIO *f,const void *vbuf, size_t count
39
40         int     PerlIO_fileno(PerlIO *f);
41
42         void    PerlIO_setlinebuf(PerlIO *f);
43
44         Off_t   PerlIO_tell(PerlIO *f);
45         int     PerlIO_seek(PerlIO *f, Off_t offset, int whence);
46         void    PerlIO_rewind(PerlIO *f);
47
48         int     PerlIO_getpos(PerlIO *f, SV *save);    /* prototype changed */
49         int     PerlIO_setpos(PerlIO *f, SV *saved);   /* prototype changed */
50
51         int     PerlIO_fast_gets(PerlIO *f);
52         int     PerlIO_has_cntptr(PerlIO *f);
53         SSize_t PerlIO_get_cnt(PerlIO *f);
54         char   *PerlIO_get_ptr(PerlIO *f);
55         void    PerlIO_set_ptrcnt(PerlIO *f, char *ptr, SSize_t count);
56
57         int     PerlIO_canset_cnt(PerlIO *f);              /* deprecated */
58         void    PerlIO_set_cnt(PerlIO *f, int count);      /* deprecated */
59
60         int     PerlIO_has_base(PerlIO *f);
61         char   *PerlIO_get_base(PerlIO *f);
62         SSize_t PerlIO_get_bufsiz(PerlIO *f);
63
64         PerlIO *PerlIO_importFILE(FILE *stdio, const char *mode);
65         FILE   *PerlIO_exportFILE(PerlIO *f, const char *mode);
66         FILE   *PerlIO_findFILE(PerlIO *f);
67         void    PerlIO_releaseFILE(PerlIO *f,FILE *stdio);
68
69         int     PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode,
70                                                           const char *layers);
71         int     PerlIO_binmode(pTHX_ PerlIO *f, int ptype, int imode,
72                                                           const char *layers);
73         void    PerlIO_debug(const char *fmt,...);
74

DESCRIPTION

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