PERLAPIO(1)
π NAME π
perlapio β perl's IO abstraction interface, aka the glow-up perl gave to stdio so everything stays portable and cute π
β¨ SYNOPSIS β¨
#define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */
#include <perlio.h> /* Usually via #include <perl.h> */
PerlIO *PerlIO_stdin(void);
PerlIO *PerlIO_stdout(void);
PerlIO *PerlIO_stderr(void);
PerlIO *PerlIO_open(const char *path,const char *mode);
PerlIO *PerlIO_fdopen(int fd, const char *mode);
PerlIO *PerlIO_reopen(const char *path, /* deprecated */
const char *mode, PerlIO *old);
int PerlIO_close(PerlIO *f);
int PerlIO_stdoutf(const char *fmt,...)
int PerlIO_puts(PerlIO *f,const char *string);
int PerlIO_putc(PerlIO *f,int ch);
SSize_t PerlIO_write(PerlIO *f,const void *buf,size_t numbytes);
int PerlIO_printf(PerlIO *f, const char *fmt,...);
int PerlIO_vprintf(PerlIO *f, const char *fmt, va_list args);
int PerlIO_flush(PerlIO *f);
int PerlIO_fill(PerlIO *f);
int PerlIO_eof(PerlIO *f);
int PerlIO_error(PerlIO *f);
void PerlIO_clearerr(PerlIO *f);
int PerlIO_getc(PerlIO *d);
int PerlIO_ungetc(PerlIO *f,int ch);
SSize_t PerlIO_read(PerlIO *f, void *buf, size_t numbytes);
Size_t PerlIO_unread(PerlIO *f,const void *vbuf, size_t count
int PerlIO_fileno(PerlIO *f);
void PerlIO_setlinebuf(PerlIO *f);
Off_t PerlIO_tell(PerlIO *f);
int PerlIO_seek(PerlIO *f, Off_t offset, int whence);
void PerlIO_rewind(PerlIO *f);
int PerlIO_getpos(PerlIO *f, SV *save); /* prototype changed */
int PerlIO_setpos(PerlIO *f, SV *saved); /* prototype changed */
int PerlIO_fast_gets(PerlIO *f);
int PerlIO_has_cntptr(PerlIO *f);
SSize_t PerlIO_get_cnt(PerlIO *f);
char *PerlIO_get_ptr(PerlIO *f);
void PerlIO_set_ptrcnt(PerlIO *f, char *ptr, SSize_t count);
int PerlIO_canset_cnt(PerlIO *f); /* deprecated */
void PerlIO_set_cnt(PerlIO *f, int count); /* deprecated */
int PerlIO_has_base(PerlIO *f);
char *PerlIO_get_base(PerlIO *f);
SSize_t PerlIO_get_bufsiz(PerlIO *f);
PerlIO *PerlIO_importFILE(FILE *stdio, const char *mode);
FILE *PerlIO_exportFILE(PerlIO *f, const char *mode);
FILE *PerlIO_findFILE(PerlIO *f);
void PerlIO_releaseFILE(PerlIO *f,FILE *stdio);
int PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode,
const char *layers);
int PerlIO_binmode(pTHX_ PerlIO *f, int ptype, int imode,
const char *layers);
void PerlIO_debug(const char *fmt,...);
π DESCRIPTION π
okay so here's the tea π« β if you're writing perl's own source code, or you're writing an extension and you want maximum portability, you should use all those functions above instead of the ones from ANSI C's stdio.h. No cap. And the perl headers β especially perlio.h β will #define them to whatever I/O mechanism got picked at Configure time. iconic behaviour, honestly β¨
The functions are modeled on the ones in stdio.h, except the parameter order has been "tidied up a little". (perl said: let me fix that for you π
)
PerlIO * takes the place of FILE *. And like FILE *, you should treat it as opaque β it is probably safe to assume it is a pointer to something, but that's literally all you get, bestie.
There are currently two implementations:
USE_STDIO
All above are #define'd to stdio functions, or they're trivial wrapper functions which call stdio. In this case only PerlIO * is a FILE *. This has been the default implementation since the abstraction was introduced in perl5.003_02. OG status π
USE_PERLIO
Introduced just after perl5.7.0, this is a re-implementation of the abstraction above which lets perl have way more control over how IO is done, because it decouples IO from the way the operating system and the C library choose to do things. For USE_PERLIO, PerlIO * has an extra layer of indirection β it is a pointer-to-a-pointer. This lets the PerlIO * keep a known value while the implementation gets swapped around underneath it at run time. Wild, I know π In this case all of the above are true (but very simple) functions which call the underlying implementation.
This is the only implementation for which PerlIO_apply_layers() does anything "interesting". π
The USE_PERLIO implementation is described in perliol.
Because perlio.h is a thin layer (for efficiency, duh), the semantics of these functions are somewhat dependent on the underlying implementation. Where these variations are understood, they're noted below.
Unless otherwise noted, functions return0on success, or a negative value (usuallyEOF, which is usually-1) and seterrnoon error. β οΈ
πΈ the functions, one by one πΈ
PerlIO_stdin(),PerlIO_stdout(),PerlIO_stderr()β use these instead ofstdin,stdout,stderr. They're written to look like "function calls" rather than variables, because that makes it easier to actually make them function calls if a platform can't export data to loaded modules, or if (say) different "threads" might have different values. future-proofing queen π§PerlIO_open(path, mode),PerlIO_fdopen(fd,mode)β these correspond tofopen()/fdopen()and the arguments are the same. They returnNULLand seterrnoif there's an error. Heads up though: there may be an implementation limit on the number of open handles, and that limit may be lower than the limit on the number of open files β anderrnomay not be set whenNULLis returned if this limit is exceeded. so like, don't assumeerrnohas your back here πPerlIO_reopen(path,mode,f)β while this currently exists in both implementations, perl itself does not use it. And because perl doesn't use it, it is not well tested. Perl prefers to "dup" the new low-level descriptor to the descriptor used by the existing PerlIO. This may become the behaviour of this function in the future. (also the synopsis literally marks it/* deprecated */, so, yk. π©)PerlIO_printf(f,fmt,...),PerlIO_vprintf(f,fmt,a)β these are thefprintf()/vfprintf()equivalents.PerlIO_stdoutf(fmt,...)β this is theprintf()equivalent.printfis#defined to this function, so it is (currently) legal to useprintf(fmt,...)in perl sources. slay β¨PerlIO_read(f,buf,count),PerlIO_write(f,buf,count)β these correspond functionally tofread()andfwrite(), but the arguments and return values are different. ThePerlIO_read()andPerlIO_write()signatures have been modeled on the much more sane low levelread()andwrite()functions instead: the "file" argument is passed first, there is only one "count", and the return value can distinguish between error andEOF. That's the good stuff π
They return a byte count if successful (which may be zero or positive), and return a negative value and set errno on error. Depending on implementation, errno may be EINTR if the operation was interrupted by a signal.
PerlIO_fill(f)β fills the buffer associated withfwith data from the layer below.PerlIO_readcalls this as part of its normal operation. Returns0upon success;-1on failure.PerlIO_close(f)β depending on implementation,errnomay beEINTRif the operation was interrupted by a signal.PerlIO_puts(f,s),PerlIO_putc(f,c)β these correspond tofputs()andfputc(). Note that the arguments have been revised to have "file" first. πPerlIO_ungetc(f,c)β this corresponds toungetc(). Note that the arguments have been revised to have "file" first. It arranges that the next read operation will return the bytec. Despite the implied "character" in the name, only values in the range0..0xFFare defined. Returns the bytecon success or-1(EOF) on error. And the number of bytes that can be "pushed back" may vary β only 1 character is certain, and then only if it is the last character that was read from the handle. so don't get greedy bestie πPerlIO_unread(f,buf,count)β this lets you unget more than a single byte. It effectively unshiftscountbytes onto the beginning of the bufferbuf, so that the next read operation(s) will return them before anything else that was in the buffer.
Returns the number of unread bytes.
PerlIO_getc(f)β this corresponds togetc(). Despite thecin the name, only the byte range0..0xFFis supported. Returns the character read or-1(EOF) on error.PerlIO_eof(f)β this corresponds tofeof(). Returns a true/false indication of whether the handle is at end of file. For terminal devices this may or may not be "sticky" depending on the implementation. The flag is cleared byPerlIO_seek(), orPerlIO_rewind().PerlIO_error(f)β this corresponds toferror(). Returns a true/false indication of whether there has been an IO error on the handle.PerlIO_fileno(f)β this corresponds tofileno(), but note that on some platforms, the meaning of "fileno" may not match Unix. Returns-1if the handle has no open descriptor associated with it.PerlIO_clearerr(f)β this corresponds toclearerr(), i.e., it clears the 'error' and (usually) 'eof' flags for the "stream". Does not return a value.PerlIO_flush(f)β this corresponds tofflush(). Sends any buffered write data to the underlying file. If called withNULLthis may flush all open streams (or core dump with someUSE_STDIOimplementations β yeah, seriously π). Calling it on a handle open for read only, or on which the last operation was a read of some kind, may lead to undefined behaviour on someUSE_STDIOimplementations. TheUSE_PERLIO(layers) implementation tries to behave better: it flushes all open streams when passedNULL, and attempts to retain data on read streams either in the buffer or by seeking the handle to the current logical position. she's doing her best π«ΆPerlIO_seek(f,offset,whence)β this corresponds tofseek(). Sends buffered write data to the underlying file, or discards any buffered read data, then positions the file descriptor as specified byoffsetandwhence(sic). This is the correct thing to do when switching between read and write on the same handle (see the issues withPerlIO_flush()above).offsetis of typeOff_t, which is a perlConfigurevalue and may not be the same as stdio'soff_t. π remembering that is the real flexPerlIO_tell(f)β this corresponds toftell(). Returns the current file position, or(Off_t) -1on error. It may just return the value the system "knows" without making a system call or checking the underlying file descriptor (so use on shared file descriptors is not safe without aPerlIO_seek()). The return value is of typeOff_t, which is a perlConfigurevalue and may not be the same as stdio'soff_t.
PerlIO_getpos(f,p), PerlIO_setpos(f,p)
Ok so these two are the lowkey cousins of fgetpos() and fsetpos() π
β like, they correspond to them, loosely, bestie. But instead of stdio's Fpos_t, they want a "Perl Scalar Value" handed over to them. And whatever gets stashed in there? Treat it as opaque, periodt. The layout of that data can literally vary from handle to handle. If you're not using stdio, or your platform just doesn't have those stdio calls, they get implemented in terms of PerlIO_tell() and PerlIO_seek() instead. π¦
PerlIO_rewind(f)
This one corresponds to rewind(), bestie. It's usually defined as being exactly this:
PerlIO_seek(f,(Off_t)0L, SEEK_SET);
PerlIO_clearerr(f);
No notes. That's the whole vibe. β¨
PerlIO_tmpfile()
Corresponds to tmpfile(), meaning it returns an anonymous PerlIO β or NULL on error π. The system will attempt to automatically delete the file when it's closed. On Unix the file is usually "unlink"-ed right after it's created, so it genuinely does not matter how it gets closed. On other systems though? The file may only get deleted if it's closed via PerlIO_close() and/or the program exits via "exit". And depending on the implementation there may be "race conditions" that let other processes get at the file β though in general it's safer on that front than ad. hoc. schemes. π«Ά
PerlIO_setlinebuf(f)
Corresponds to setlinebuf(). Does not return a value. What counts as a "line" is implementation dependent, but usually it means that writing "\n" flushes the buffer. What happens with stuff like "this\nthat" is uncertain π. (Perl core only uses it when "dumping"; it has nothing to do with $| auto-flush, ok?)
π€ Co-existence with stdio π€
There's outline support for PerlIO and stdio living together in harmony, roommates style π . Obviously if PerlIO is implemented in terms of stdio there's no problem at all. But in other cases, mechanisms have to exist to create a FILE * that can be passed to library code which is gonna use stdio calls.
Step one, bestie, is to add this line:
#define PERLIO_NOT_STDIO 0
β¦before including any perl header files. (This will probably become the default at some point, fyi.) That stops "perlio.h" from attempting to #define stdio functions onto PerlIO functions. π₯
XS code is probably better off using "typemap" if it expects FILE * arguments. The standard typemap will be adjusted to comprehend any changes in this area.
PerlIO_importFILE(f,mode)
Used to get a PerlIO * from a FILE *. π«
The mode argument should be a string as would be passed to fopen/PerlIO_open. If it's NULL then β for legacy support β the code will (depending upon the platform and the implementation) either attempt to empirically determine the mode in which f is open, or use "r+" to indicate a read/write stream.
Once called, theFILE *should ONLY be closed by callingPerlIO_close()on the returnedPerlIO *. No exceptions, bestie. π
The PerlIO is set to textmode. Use PerlIO_binmode if that's not the mode you want.
β οΈ This is not the reverse of PerlIO_exportFILE().
PerlIO_exportFILE(f,mode)
Given a PerlIO *, create a 'native' FILE * suitable for passing to code expecting to be compiled and linked with ANSI C stdio.h. The mode argument should be a string as would be passed to fopen/PerlIO_open. If it's NULL then β for legacy support β the FILE * is opened in the same mode as the PerlIO *.
The fact that such a FILE * has been 'exported' gets recorded (normally by pushing a new :stdio "layer" onto the PerlIO *), which may affect future PerlIO operations on the original PerlIO *. You should not call fclose() on the file unless you call PerlIO_releaseFILE() to disassociate it from the PerlIO *. (And do not use PerlIO_importFILE() for doing the disassociation, okay?)
Calling this function repeatedly will create a FILE * on each call (and will push an :stdio layer each time as well). So, like, keep that in mind π
PerlIO_releaseFILE(p,f)
Calling PerlIO_releaseFILE informs PerlIO that all use of FILE * is complete. It's removed from the list of 'exported' FILE *s, and the associated PerlIO * should revert to its original behaviour.
Use this to disassociate a file from a PerlIO * that was associated using PerlIO_exportFILE(). π
PerlIO_findFILE(f)
Returns a native FILE * used by a stdio layer. If there isn't one, it will create one with PerlIO_exportFILE. Either way, the FILE * should be considered as belonging to the PerlIO subsystem and should only be closed by calling PerlIO_close(). π
β‘ "Fast gets" Functions β‘
In addition to the standard-like API defined so far above, there's an "implementation" interface which lets perl get at the internals of PerlIO. The following calls correspond to the various FILE_xxx macros determined by Configure β or their equivalent in other implementations. Real talk: this section is really of interest to only those concerned with detailed perl-core behaviour, implementing a PerlIO mapping, or writing code which can make use of the "read ahead" that's been done by the IO system in the same way perl does. And note this: any code that uses these interfaces must be prepared to do things the traditional way if a handle doesn't support them. π§
PerlIO_fast_gets(f)
Returns true if the implementation has all the interfaces required to allow perl's "sv_gets" to "bypass" the normal IO mechanism. This can vary from handle to handle.
PerlIO_fast_gets(f) = PerlIO_has_cntptr(f) && \
PerlIO_canset_cnt(f) && \
'Can set pointer into buffer'
PerlIO_has_cntptr(f)
Implementation can return a pointer to the current position in the "buffer" and a count of bytes available in the buffer. π«Ά Do not use this β use PerlIO_fast_gets.
PerlIO_get_cnt(f)
Return count of readable bytes in the buffer. A zero or negative return means no more bytes available.
PerlIO_get_ptr(f)
Return pointer to the next readable byte in the buffer. Accessing via the pointer (dereferencing) is only safe if PerlIO_get_cnt() has returned a positive value. And only positive offsets up to the value returned by PerlIO_get_cnt() are allowed, bestie. No vibing past the limit π
PerlIO_set_ptrcnt(f,p,c)
Set pointer into the buffer, and a count of bytes still in the buffer. Should be used only to set the pointer to within the range implied by previous calls to "PerlIO_get_ptr" and "PerlIO_get_cnt". The two values must be consistent with each other (implementation may only use one or the other, or may require both). π
PerlIO_canset_cnt(f)
Implementation can adjust its idea of the number of bytes in the buffer. Do not use this β use PerlIO_fast_gets.
PerlIO_set_cnt(f,c)
Obscure π β set the count of bytes in the buffer. Deprecated. Only usable if PerlIO_canset_cnt() returns true. Currently used in only doio.c to force a count less than -1 to -1. Perhaps should be PerlIO_set_empty or similar. This call may actually do nothing if "count" is deduced from a pointer and a "limit". Do not use this β use PerlIO_set_ptrcnt().
PerlIO_has_base(f)
Returns true if the implementation has a buffer, and can return a pointer to the whole buffer and its size. Used by perl for -T / -B tests. Other uses would be very obscure... π
PerlIO_get_base(f)
Return the start of the buffer. Access only positive offsets in the buffer up to the value returned by PerlIO_get_bufsiz().
PerlIO_get_bufsiz(f)
Return the total number of bytes in the buffer. Careful bestie, this is neither the number that can be read, nor the amount of memory allocated to the buffer. Rather, it's what the operating system and/or implementation happened to read() (or whatever) the last time IO was requested. π«
π§° Other Functions π§°
PerlIO_apply_layers(aTHX_ f,mode,layers)
The new interface to the USE_PERLIO implementation. The layers ":crlf" and ":raw" are the only ones allowed for other implementations, and those are silently ignored. (As of perl5.8, ":raw" is deprecated.) Use PerlIO_binmode() below for the portable case. π
PerlIO_binmode(aTHX_ f,ptype,imode,layers)
The hook used by perl's "binmode" operator. ptype is perl's character for the kind of IO:
'<'β read'>'β write'+'β read/write
imode is "O_BINARY" or "O_TEXT".
layers is a string of layers to apply; only ":crlf" makes sense in the non-USE_PERLIO case. (As of perl5.8, ":raw" is deprecated in favour of passing NULL.)
The portable cases are:
PerlIO_binmode(aTHX_ f,ptype,O_BINARY,NULL);
and
PerlIO_binmode(aTHX_ f,ptype,O_TEXT,":crlf");
On Unix these calls probably have no effect whatsoever π΄. Elsewhere they alter "\n" to CR,LF translation and possibly cause a special text "end of file" indicator to be written or honoured on read. The effect of making the call after doing any IO to the handle depends on the implementation. (It may be ignored, affect any data which is already buffered as well, or only apply to subsequent data.) π
PerlIO_debug(fmt,...)
PerlIO_debug is a printf()-like function which can be used for debugging. No return value. Its main use is inside PerlIO, where using real printf, warn() etc. would recursively call PerlIO and be a whole problem π.
PerlIO_debug writes to the file named by $ENV{'PERLIO_DEBUG'} or defaults to stderr if the environment variable isn't defined. Typical use might be:
Bourne shells (sh, ksh, bash, zsh, ash, ...):
PERLIO_DEBUG=/tmp/perliodebug.log ./perl -Di somescript some args
Csh/Tcsh:
setenv PERLIO_DEBUG /tmp/perliodebug.log
./perl -Di somescript some args
If you have the "env" utility:
env PERLIO_DEBUG=/tmp/perliodebug.log ./perl -Di somescript args
Win32:
set PERLIO_DEBUG=perliodebug.log
perl -Di somescript some args
Heads up bestie: on a Perl built without-DDEBUGGING, or when the-Dicommand-line switch isn't specified, or under taint,PerlIO_debug()is a no-op. Like, it just does nothing. π«
And that's a wrap, fr fr π
This whole page is from perl v5.42.3, dated 2026-08-03, and it's the PERLAPIO(1) manual page. Go forth and slay your PerlIO internals, bestie πβ¨π¦
π¦ where this page comes from
- fedora 43: perl-doc 5.42.3-524.fc43 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND Unicode-3.0 AND LicenseRef-Fedora-UltraPermissive
- fedora 44: perl-doc 5.42.3-525.fc44 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND Unicode-3.0 AND LicenseRef-Fedora-UltraPermissive
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms