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, 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

DESCRIPTION

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
103           implementation around underneath at run time. In this case all the
104           above are true (but very simple) functions which call the
105           underlying 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
114       implementation.  Where these variations are understood they are noted
115       below.
116
117       Unless otherwise noted, functions return 0 on success, or a negative
118       value (usually "EOF" which is usually -1) and set "errno" on error.
119
120       PerlIO_stdin(), PerlIO_stdout(), PerlIO_stderr()
121           Use these rather than "stdin", "stdout", "stderr". They are written
122           to look like "function calls" rather than variables because this
123           makes it easier to make them function calls if platform cannot
124           export data to loaded modules, or if (say) different "threads"
125           might have different values.
126
127       PerlIO_open(path, mode), PerlIO_fdopen(fd,mode)
128           These correspond to fopen()/fdopen() and the arguments are the
129           same.  Return "NULL" and set "errno" if there is an error.  There
130           may be an implementation limit on the number of open handles, which
131           may be lower than the limit on the number of open files - "errno"
132           may not be set when "NULL" is returned if this limit is exceeded.
133
134       PerlIO_reopen(path,mode,f)
135           While this currently exists in all three implementations perl
136           itself does not use it. As perl does not use it, it is not well
137           tested.
138
139           Perl prefers to "dup" the new low-level descriptor to the
140           descriptor used by the existing PerlIO. This may become the
141           behaviour of this function in the future.
142
143       PerlIO_printf(f,fmt,...), PerlIO_vprintf(f,fmt,a)
144           These are fprintf()/vfprintf() equivalents.
145
146       PerlIO_stdoutf(fmt,...)
147           This is printf() equivalent. printf is #defined to this function,
148           so it is (currently) legal to use "printf(fmt,...)" in perl
149           sources.
150
151       PerlIO_read(f,buf,count), PerlIO_write(f,buf,count)
152           These correspond functionally to fread() and fwrite() but the
153           arguments and return values are different.  The PerlIO_read() and
154           PerlIO_write() signatures have been modeled on the more sane low
155           level read() and write() functions instead: The "file" argument is
156           passed first, there is only one "count", and the return value can
157           distinguish between error and "EOF".
158
159           Returns a byte count if successful (which may be zero or positive),
160           returns negative value and sets "errno" on error.  Depending on
161           implementation "errno" may be "EINTR" if operation was interrupted
162           by a signal.
163
164       PerlIO_close(f)
165           Depending on implementation "errno" may be "EINTR" if operation was
166           interrupted by a signal.
167
168       PerlIO_puts(f,s), PerlIO_putc(f,c)
169           These correspond to fputs() and fputc().  Note that arguments have
170           been revised to have "file" first.
171
172       PerlIO_ungetc(f,c)
173           This corresponds to ungetc().  Note that arguments have been
174           revised to have "file" first.  Arranges that next read operation
175           will return the byte c.  Despite the implied "character" in the
176           name only values in the range 0..0xFF are defined. Returns the byte
177           c on success or -1 ("EOF") on error.  The number of bytes that can
178           be "pushed back" may vary, only 1 character is certain, and then
179           only if it is the last character that was read from the handle.
180
181       PerlIO_getc(f)
182           This corresponds to getc().  Despite the c in the name only byte
183           range 0..0xFF is supported.  Returns the character read or -1
184           ("EOF") on error.
185
186       PerlIO_eof(f)
187           This corresponds to feof().  Returns a true/false indication of
188           whether the handle is at end of file.  For terminal devices this
189           may or may not be "sticky" depending on the implementation.  The
190           flag is cleared by PerlIO_seek(), or PerlIO_rewind().
191
192       PerlIO_error(f)
193           This corresponds to ferror().  Returns a true/false indication of
194           whether there has been an IO error on the handle.
195
196       PerlIO_fileno(f)
197           This corresponds to fileno(), note that on some platforms, the
198           meaning of "fileno" may not match Unix. Returns -1 if the handle
199           has no open descriptor associated with it.
200
201       PerlIO_clearerr(f)
202           This corresponds to clearerr(), i.e., clears 'error' and (usually)
203           'eof' flags for the "stream". Does not return a value.
204
205       PerlIO_flush(f)
206           This corresponds to fflush().  Sends any buffered write data to the
207           underlying file.  If called with "NULL" this may flush all open
208           streams (or core dump with some USE_STDIO implementations).
209           Calling on a handle open for read only, or on which last operation
210           was a read of some kind may lead to undefined behaviour on some
211           USE_STDIO implementations.  The USE_PERLIO (layers) implementation
212           tries to behave better: it flushes all open streams when passed
213           "NULL", and attempts to retain data on read streams either in the
214           buffer or by seeking the handle to the current logical position.
215
216       PerlIO_seek(f,offset,whence)
217           This corresponds to fseek().  Sends buffered write data to the
218           underlying file, or discards any buffered read data, then positions
219           the file descriptor as specified by offset and whence (sic).  This
220           is the correct thing to do when switching between read and write on
221           the same handle (see issues with PerlIO_flush() above).  Offset is
222           of type "Off_t" which is a perl Configure value which may not be
223           same as stdio's "off_t".
224
225       PerlIO_tell(f)
226           This corresponds to ftell().  Returns the current file position, or
227           (Off_t) -1 on error.  May just return value system "knows" without
228           making a system call or checking the underlying file descriptor (so
229           use on shared file descriptors is not safe without a
230           PerlIO_seek()). Return value is of type "Off_t" which is a perl
231           Configure value which may not be same as stdio's "off_t".
232
233       PerlIO_getpos(f,p), PerlIO_setpos(f,p)
234           These correspond (loosely) to fgetpos() and fsetpos(). Rather than
235           stdio's Fpos_t they expect a "Perl Scalar Value" to be passed. What
236           is stored there should be considered opaque. The layout of the data
237           may vary from handle to handle.  When not using stdio or if
238           platform does not have the stdio calls then they are implemented in
239           terms of PerlIO_tell() and PerlIO_seek().
240
241       PerlIO_rewind(f)
242           This corresponds to rewind(). It is usually defined as being
243
244               PerlIO_seek(f,(Off_t)0L, SEEK_SET);
245               PerlIO_clearerr(f);
246
247       PerlIO_tmpfile()
248           This corresponds to tmpfile(), i.e., returns an anonymous PerlIO or
249           NULL on error.  The system will attempt to automatically delete the
250           file when closed.  On Unix the file is usually "unlink"-ed just
251           after it is created so it does not matter how it gets closed. On
252           other systems the file may only be deleted if closed via
253           PerlIO_close() and/or the program exits via "exit".  Depending on
254           the implementation there may be "race conditions" which allow other
255           processes access to the file, though in general it will be safer in
256           this regard than ad. hoc. schemes.
257
258       PerlIO_setlinebuf(f)
259           This corresponds to setlinebuf().  Does not return a value. What
260           constitutes a "line" is implementation dependent but usually means
261           that writing "\n" flushes the buffer.  What happens with things
262           like "this\nthat" is uncertain.  (Perl core uses it only when
263           "dumping"; it has nothing to do with $| auto-flush.)
264
265   Co-existence with stdio
266       There is outline support for co-existence of PerlIO with stdio.
267       Obviously if PerlIO is implemented in terms of stdio there is no
268       problem. However in other cases then mechanisms must exist to create a
269       FILE * which can be passed to library code which is going to use stdio
270       calls.
271
272       The first step is to add this line:
273
274          #define PERLIO_NOT_STDIO 0
275
276       before including any perl header files. (This will probably become the
277       default at some point).  That prevents "perlio.h" from attempting to
278       #define stdio functions onto PerlIO functions.
279
280       XS code is probably better using "typemap" if it expects FILE *
281       arguments.  The standard typemap will be adjusted to comprehend any
282       changes in this area.
283
284       PerlIO_importFILE(f,mode)
285           Used to get a PerlIO * from a FILE *.
286
287           The mode argument should be a string as would be passed to
288           fopen/PerlIO_open.  If it is NULL then - for legacy support - the
289           code will (depending upon the platform and the implementation)
290           either attempt to empirically determine the mode in which f is
291           open, or use "r+" to indicate a read/write stream.
292
293           Once called the FILE * should ONLY be closed by calling
294           "PerlIO_close()" on the returned PerlIO *.
295
296           The PerlIO is set to textmode. Use PerlIO_binmode if this is not
297           the desired mode.
298
299           This is not the reverse of PerlIO_exportFILE().
300
301       PerlIO_exportFILE(f,mode)
302           Given a PerlIO * create a 'native' FILE * suitable for passing to
303           code expecting to be compiled and linked with ANSI C stdio.h.  The
304           mode argument should be a string as would be passed to
305           fopen/PerlIO_open.  If it is NULL then - for legacy support - the
306           FILE * is opened in same mode as the PerlIO *.
307
308           The fact that such a FILE * has been 'exported' is recorded,
309           (normally by pushing a new :stdio "layer" onto the PerlIO *), which
310           may affect future PerlIO operations on the original PerlIO *.  You
311           should not call "fclose()" on the file unless you call
312           "PerlIO_releaseFILE()" to disassociate it from the PerlIO *.  (Do
313           not use PerlIO_importFILE() for doing the disassociation.)
314
315           Calling this function repeatedly will create a FILE * on each call
316           (and will push an :stdio layer each time as well).
317
318       PerlIO_releaseFILE(p,f)
319           Calling PerlIO_releaseFILE informs PerlIO that all use of FILE * is
320           complete. It is removed from the list of 'exported' FILE *s, and
321           the associated PerlIO * should revert to its original behaviour.
322
323           Use this to disassociate a file from a PerlIO * that was associated
324           using PerlIO_exportFILE().
325
326       PerlIO_findFILE(f)
327           Returns a native FILE * used by a stdio layer. If there is none, it
328           will create one with PerlIO_exportFILE. In either case the FILE *
329           should be considered as belonging to PerlIO subsystem and should
330           only be closed by calling "PerlIO_close()".
331
332   "Fast gets" Functions
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
342       handle 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
355           "buffer" and a count of bytes available in the buffer.  Do not use
356           this - 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
371           previous calls to "PerlIO_get_ptr" and "PerlIO_get_cnt". The two
372           values must be consistent with each other (implementation may only
373           use 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
377           buffer.  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
383           PerlIO_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
400           implementation happened to "read()" (or whatever) last time IO was
401           requested.
402
403   Other Functions
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
408           deprecated.)  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
412           character 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,NULL);
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'}
444           typical 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.12.4                      2011-06-01                       PERLAPIO(1)
Impressum