You are viewing the version of this documentation from Perl blead. This is the main development branch of Perl. (git commit bdd2c794f79baa3bf919296f597a683a6eaa25ef)

CONTENTS

NAME

perlclib - Interacting with standard C library functions

DESCRIPTION

The perl interpreter is written in C; XS code also expands to C. Inevitably, this code will call some functions from the C library, libc. This document gives some guidance on interfacing with that library.

One thing Perl porters should note is that perl doesn't tend to use that much of the C standard library internally; you'll see very little use of, for example, the ctype.h functions in there. This is because Perl tends to reimplement or abstract standard library functions, so that we know exactly how they're going to operate.

libc functions to avoid

There are many many libc functions. Most of them are fair game to use, but some are not. Some of the possible reasons are:

The following commentary and tables give some functions in the first column that shouldn't be used in C or XS code, with the preferred alternative (if any) in the second column.

Conventions

In the following tables:

~

marks the function as deprecated; it should not be used regardless.

t

is a type.

p

is a pointer.

n

is a number.

s

is a string.

sv, av, hv, etc. represent variables of their respective types.

File Operations

Instead of the stdio.h functions, you should use the Perl abstraction layer. Instead of FILE* types, you need to be handling PerlIO* types. Don't forget that with the new PerlIO layered I/O abstraction FILE* types may not even be available. See also the perlapio documentation for more information about the following functions:

Instead Of:                 Use:

stdin                       PerlIO_stdin()
stdout                      PerlIO_stdout()
stderr                      PerlIO_stderr()

fopen(fn, mode)             PerlIO_open(fn, mode)
freopen(fn, mode, stream)   PerlIO_reopen(fn, mode, perlio) (Dep-
                              recated)
fflush(stream)              PerlIO_flush(perlio)
fclose(stream)              PerlIO_close(perlio)