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

NAME

6       perlclib - Internal replacements for standard C library functions
7

DESCRIPTION

9       One thing Perl porters should note is that perl doesn't tend to use
10       that much of the C standard library internally; you'll see very little
11       use of, for example, the ctype.h functions in there. This is because
12       Perl tends to reimplement or abstract standard library functions, so
13       that we know exactly how they're going to operate.
14
15       This is a reference card for people who are familiar with the C library
16       and who want to do things the Perl way; to tell them which functions
17       they ought to use instead of the more normal C functions.
18
19   Conventions
20       In the following tables:
21
22       "t"
23          is a type.
24
25       "p"
26          is a pointer.
27
28       "n"
29          is a number.
30
31       "s"
32          is a string.
33
34       "sv", "av", "hv", etc. represent variables of their respective types.
35
36   File Operations
37       Instead of the stdio.h functions, you should use the Perl abstraction
38       layer. Instead of "FILE*" types, you need to be handling "PerlIO*"
39       types.  Don't forget that with the new PerlIO layered I/O abstraction
40       "FILE*" types may not even be available. See also the "perlapio"
41       documentation for more information about the following functions:
42
43        Instead Of:                 Use:
44
45        stdin                       PerlIO_stdin()
46        stdout                      PerlIO_stdout()
47        stderr                      PerlIO_stderr()
48
49        fopen(fn, mode)             PerlIO_open(fn, mode)
50        freopen(fn, mode, stream)   PerlIO_reopen(fn, mode, perlio) (Dep-
51                                      recated)
52        fflush(stream)              PerlIO_flush(perlio)
53        fclose(stream)              PerlIO_close(perlio)
54
55   File Input and Output
56        Instead Of:                 Use:
57
58        fprintf(stream, fmt, ...)   PerlIO_printf(perlio, fmt, ...)
59
60        [f]getc(stream)             PerlIO_getc(perlio)
61        [f]putc(stream, n)          PerlIO_putc(perlio, n)
62        ungetc(n, stream)           PerlIO_ungetc(perlio, n)
63
64       Note that the PerlIO equivalents of "fread" and "fwrite" are slightly
65       different from their C library counterparts:
66
67        fread(p, size, n, stream)   PerlIO_read(perlio, buf, numbytes)
68        fwrite(p, size, n, stream)  PerlIO_write(perlio, buf, numbytes)
69
70        fputs(s, stream)            PerlIO_puts(perlio, s)
71
72       There is no equivalent to "fgets"; one should use "sv_gets" instead:
73
74        fgets(s, n, stream)         sv_gets(sv, perlio, append)
75
76   File Positioning
77        Instead Of:                 Use:
78
79        feof(stream)                PerlIO_eof(perlio)
80        fseek(stream, n, whence)    PerlIO_seek(perlio, n, whence)
81        rewind(stream)              PerlIO_rewind(perlio)
82
83        fgetpos(stream, p)          PerlIO_getpos(perlio, sv)
84        fsetpos(stream, p)          PerlIO_setpos(perlio, sv)
85
86        ferror(stream)              PerlIO_error(perlio)
87        clearerr(stream)            PerlIO_clearerr(perlio)
88
89   Memory Management and String Handling
90        Instead Of:                    Use:
91
92        t* p = malloc(n)               Newx(p, n, t)
93        t* p = calloc(n, s)            Newxz(p, n, t)
94        p = realloc(p, n)              Renew(p, n, t)
95        memcpy(dst, src, n)            Copy(src, dst, n, t)
96        memmove(dst, src, n)           Move(src, dst, n, t)
97        memcpy(dst, src, sizeof(t))    StructCopy(src, dst, t)
98        memset(dst, 0, n * sizeof(t))  Zero(dst, n, t)
99        memzero(dst, 0)                Zero(dst, n, char)
100        free(p)                        Safefree(p)
101
102        strdup(p)                      savepv(p)
103        strndup(p, n)                  savepvn(p, n) (Hey, strndup doesn't
104                                                      exist!)
105
106        strstr(big, little)            instr(big, little)
107        strcmp(s1, s2)                 strLE(s1, s2) / strEQ(s1, s2)
108                                                     / strGT(s1,s2)
109        strncmp(s1, s2, n)             strnNE(s1, s2, n) / strnEQ(s1, s2, n)
110
111        memcmp(p1, p2, n)              memNE(p1, p2, n)
112        !memcmp(p1, p2, n)             memEQ(p1, p2, n)
113
114       Notice the different order of arguments to "Copy" and "Move" than used
115       in "memcpy" and "memmove".
116
117       Most of the time, though, you'll want to be dealing with SVs internally
118       instead of raw "char *" strings:
119
120        strlen(s)                   sv_len(sv)
121        strcpy(dt, src)             sv_setpv(sv, s)
122        strncpy(dt, src, n)         sv_setpvn(sv, s, n)
123        strcat(dt, src)             sv_catpv(sv, s)
124        strncat(dt, src)            sv_catpvn(sv, s)
125        sprintf(s, fmt, ...)        sv_setpvf(sv, fmt, ...)
126
127       Note also the existence of "sv_catpvf" and "sv_vcatpvfn", combining
128       concatenation with formatting.
129
130       Sometimes instead of zeroing the allocated heap by using Newxz() you
131       should consider "poisoning" the data.  This means writing a bit pattern
132       into it that should be illegal as pointers (and floating point
133       numbers), and also hopefully surprising enough as integers, so that any
134       code attempting to use the data without forethought will break sooner
135       rather than later.  Poisoning can be done using the Poison() macros,
136       which have similar arguments to Zero():
137
138        PoisonWith(dst, n, t, b)    scribble memory with byte b
139        PoisonNew(dst, n, t)        equal to PoisonWith(dst, n, t, 0xAB)
140        PoisonFree(dst, n, t)       equal to PoisonWith(dst, n, t, 0xEF)
141        Poison(dst, n, t)           equal to PoisonFree(dst, n, t)
142
143   Character Class Tests
144       There are several types of character class tests that Perl implements.
145       The only ones described here are those that directly correspond to C
146       library functions that operate on 8-bit characters, but there are
147       equivalents that operate on wide characters, and UTF-8 encoded strings.
148       All are more fully described in "Character classification" in perlapi
149       and "Character case changing" in perlapi.
150
151       The C library routines listed in the table below return values based on
152       the current locale.  Use the entries in the final column for that
153       functionality.  The other two columns always assume a POSIX (or C)
154       locale.  The entries in the ASCII column are only meaningful for ASCII
155       inputs, returning FALSE for anything else.  Use these only when you
156       know that is what you want.  The entries in the Latin1 column assume
157       that the non-ASCII 8-bit characters are as Unicode defines, them, the
158       same as ISO-8859-1, often called Latin 1.
159
160        Instead Of:  Use for ASCII:   Use for Latin1:      Use for locale:
161
162        isalnum(c)  isALPHANUMERIC(c) isALPHANUMERIC_L1(c) isALPHANUMERIC_LC(c)
163        isalpha(c)  isALPHA(c)        isALPHA_L1(c)        isALPHA_LC(u )
164        isascii(c)  isASCII(c)                             isASCII_LC(c)
165        isblank(c)  isBLANK(c)        isBLANK_L1(c)        isBLANK_LC(c)
166        iscntrl(c)  isCNTRL(c)        isCNTRL_L1(c)        isCNTRL_LC(c)
167        isdigit(c)  isDIGIT(c)        isDIGIT_L1(c)        isDIGIT_LC(c)
168        isgraph(c)  isGRAPH(c)        isGRAPH_L1(c)        isGRAPH_LC(c)
169        islower(c)  isLOWER(c)        isLOWER_L1(c)        isLOWER_LC(c)
170        isprint(c)  isPRINT(c)        isPRINT_L1(c)        isPRINT_LC(c)
171        ispunct(c)  isPUNCT(c)        isPUNCT_L1(c)        isPUNCT_LC(c)
172        isspace(c)  isSPACE(c)        isSPACE_L1(c)        isSPACE_LC(c)
173        isupper(c)  isUPPER(c)        isUPPER_L1(c)        isUPPER_LC(c)
174        isxdigit(c) isXDIGIT(c)       isXDIGIT_L1(c)       isXDIGIT_LC(c)
175
176        tolower(c)  toLOWER(c)        toLOWER_L1(c)
177        toupper(c)  toUPPER(c)
178
179       To emphasize that you are operating only on ASCII characters, you can
180       append "_A" to each of the macros in the ASCII column: "isALPHA_A",
181       "isDIGIT_A", and so on.
182
183       (There is no entry in the Latin1 column for "isascii" even though there
184       is an "isASCII_L1", which is identical to "isASCII";  the latter name
185       is clearer.  There is no entry in the Latin1 column for "toupper"
186       because the result can be non-Latin1.  You have to use "toUPPER_uvchr",
187       as described in "Character case changing" in perlapi.)
188
189   stdlib.h functions
190        Instead Of:                 Use:
191
192        atof(s)                     Atof(s)
193        atoi(s)                     grok_atoUV(s, &uv, &e)
194        atol(s)                     grok_atoUV(s, &uv, &e)
195        strtod(s, &p)               Strtod(s, &p)
196        strtol(s, &p, n)            Strtol(s, &p, b)
197        strtoul(s, &p, n)           Strtoul(s, &p, b)
198
199       Typical use is to do range checks on "uv" before casting:
200
201         int i; UV uv;
202         char* end_ptr = input_end;
203         if (grok_atoUV(input, &uv, &end_ptr)
204             && uv <= INT_MAX)
205           i = (int)uv;
206           ... /* continue parsing from end_ptr */
207         } else {
208           ... /* parse error: not a decimal integer in range 0 .. MAX_IV */
209         }
210
211       Notice also the "grok_bin", "grok_hex", and "grok_oct" functions in
212       numeric.c for converting strings representing numbers in the respective
213       bases into "NV"s.  Note that grok_atoUV() doesn't handle negative
214       inputs, or leading whitespace (being purposefully strict).
215
216       Note that strtol() and strtoul() may be disguised as Strtol(),
217       Strtoul(), Atol(), Atoul().  Avoid those, too.
218
219       In theory "Strtol" and "Strtoul" may not be defined if the machine perl
220       is built on doesn't actually have strtol and strtoul. But as those 2
221       functions are part of the 1989 ANSI C spec we suspect you'll find them
222       everywhere by now.
223
224        int rand()                  double Drand01()
225        srand(n)                    { seedDrand01((Rand_seed_t)n);
226                                      PL_srand_called = TRUE; }
227
228        exit(n)                     my_exit(n)
229        system(s)                   Don't. Look at pp_system or use my_popen.
230
231        getenv(s)                   PerlEnv_getenv(s)
232        setenv(s, val)              my_setenv(s, val)
233
234   Miscellaneous functions
235       You should not even want to use setjmp.h functions, but if you think
236       you do, use the "JMPENV" stack in scope.h instead.
237
238       For "signal"/"sigaction", use "rsignal(signo, handler)".
239

SEE ALSO

241       perlapi, perlapio, perlguts
242
243
244
245perl v5.38.2                      2023-11-30                       PERLCLIB(1)
Impressum