1PERLCLIB(1) Perl Programmers Reference Guide PERLCLIB(1)
2
3
4
6 perlclib - Internal replacements for standard C library functions
7
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) (Deprecated)
51 fflush(stream) PerlIO_flush(perlio)
52 fclose(stream) PerlIO_close(perlio)
53
54 File Input and Output
55 Instead Of: Use:
56
57 fprintf(stream, fmt, ...) PerlIO_printf(perlio, fmt, ...)
58
59 [f]getc(stream) PerlIO_getc(perlio)
60 [f]putc(stream, n) PerlIO_putc(perlio, n)
61 ungetc(n, stream) PerlIO_ungetc(perlio, n)
62
63 Note that the PerlIO equivalents of "fread" and "fwrite" are slightly
64 different from their C library counterparts:
65
66 fread(p, size, n, stream) PerlIO_read(perlio, buf, numbytes)
67 fwrite(p, size, n, stream) PerlIO_write(perlio, buf, numbytes)
68
69 fputs(s, stream) PerlIO_puts(perlio, s)
70
71 There is no equivalent to "fgets"; one should use "sv_gets" instead:
72
73 fgets(s, n, stream) sv_gets(sv, perlio, append)
74
75 File Positioning
76 Instead Of: Use:
77
78 feof(stream) PerlIO_eof(perlio)
79 fseek(stream, n, whence) PerlIO_seek(perlio, n, whence)
80 rewind(stream) PerlIO_rewind(perlio)
81
82 fgetpos(stream, p) PerlIO_getpos(perlio, sv)
83 fsetpos(stream, p) PerlIO_setpos(perlio, sv)
84
85 ferror(stream) PerlIO_error(perlio)
86 clearerr(stream) PerlIO_clearerr(perlio)
87
88 Memory Management and String Handling
89 Instead Of: Use:
90
91 t* p = malloc(n) Newx(id, p, n, t)
92 t* p = calloc(n, s) Newxz(id, p, n, t)
93 p = realloc(p, n) Renew(p, n, t)
94 memcpy(dst, src, n) Copy(src, dst, n, t)
95 memmove(dst, src, n) Move(src, dst, n, t)
96 memcpy(dst, src, sizeof(t)) StructCopy(src, dst, t)
97 memset(dst, 0, n * sizeof(t)) Zero(dst, n, t)
98 memzero(dst, 0) Zero(dst, n, char)
99 free(p) Safefree(p)
100
101 strdup(p) savepv(p)
102 strndup(p, n) savepvn(p, n) (Hey, strndup doesn't exist!)
103
104 strstr(big, little) instr(big, little)
105 strcmp(s1, s2) strLE(s1, s2) / strEQ(s1, s2) / strGT(s1,s2)
106 strncmp(s1, s2, n) strnNE(s1, s2, n) / strnEQ(s1, s2, n)
107
108 Notice the different order of arguments to "Copy" and "Move" than used
109 in "memcpy" and "memmove".
110
111 Most of the time, though, you'll want to be dealing with SVs internally
112 instead of raw "char *" strings:
113
114 strlen(s) sv_len(sv)
115 strcpy(dt, src) sv_setpv(sv, s)
116 strncpy(dt, src, n) sv_setpvn(sv, s, n)
117 strcat(dt, src) sv_catpv(sv, s)
118 strncat(dt, src) sv_catpvn(sv, s)
119 sprintf(s, fmt, ...) sv_setpvf(sv, fmt, ...)
120
121 Note also the existence of "sv_catpvf" and "sv_vcatpvfn", combining
122 concatenation with formatting.
123
124 Sometimes instead of zeroing the allocated heap by using Newxz() you
125 should consider "poisoning" the data. This means writing a bit pattern
126 into it that should be illegal as pointers (and floating point
127 numbers), and also hopefully surprising enough as integers, so that any
128 code attempting to use the data without forethought will break sooner
129 rather than later. Poisoning can be done using the Poison() macros,
130 which have similar arguments as Zero():
131
132 PoisonWith(dst, n, t, b) scribble memory with byte b
133 PoisonNew(dst, n, t) equal to PoisonWith(dst, n, t, 0xAB)
134 PoisonFree(dst, n, t) equal to PoisonWith(dst, n, t, 0xEF)
135 Poison(dst, n, t) equal to PoisonFree(dst, n, t)
136
137 Character Class Tests
138 There are two types of character class tests that Perl implements: one
139 type deals in "char"s and are thus not Unicode aware (and hence
140 deprecated unless you know you should use them) and the other type deal
141 in "UV"s and know about Unicode properties. In the following table, "c"
142 is a "char", and "u" is a Unicode codepoint.
143
144 Instead Of: Use: But better use:
145
146 isalnum(c) isALNUM(c) isALNUM_uni(u)
147 isalpha(c) isALPHA(c) isALPHA_uni(u)
148 iscntrl(c) isCNTRL(c) isCNTRL_uni(u)
149 isdigit(c) isDIGIT(c) isDIGIT_uni(u)
150 isgraph(c) isGRAPH(c) isGRAPH_uni(u)
151 islower(c) isLOWER(c) isLOWER_uni(u)
152 isprint(c) isPRINT(c) isPRINT_uni(u)
153 ispunct(c) isPUNCT(c) isPUNCT_uni(u)
154 isspace(c) isSPACE(c) isSPACE_uni(u)
155 isupper(c) isUPPER(c) isUPPER_uni(u)
156 isxdigit(c) isXDIGIT(c) isXDIGIT_uni(u)
157
158 tolower(c) toLOWER(c) toLOWER_uni(u)
159 toupper(c) toUPPER(c) toUPPER_uni(u)
160
161 stdlib.h functions
162 Instead Of: Use:
163
164 atof(s) Atof(s)
165 atol(s) Atol(s)
166 strtod(s, &p) Nothing. Just don't use it.
167 strtol(s, &p, n) Strtol(s, &p, n)
168 strtoul(s, &p, n) Strtoul(s, &p, n)
169
170 Notice also the "grok_bin", "grok_hex", and "grok_oct" functions in
171 numeric.c for converting strings representing numbers in the respective
172 bases into "NV"s.
173
174 In theory "Strtol" and "Strtoul" may not be defined if the machine perl
175 is built on doesn't actually have strtol and strtoul. But as those 2
176 functions are part of the 1989 ANSI C spec we suspect you'll find them
177 everywhere by now.
178
179 int rand() double Drand01()
180 srand(n) { seedDrand01((Rand_seed_t)n);
181 PL_srand_called = TRUE; }
182
183 exit(n) my_exit(n)
184 system(s) Don't. Look at pp_system or use my_popen
185
186 getenv(s) PerlEnv_getenv(s)
187 setenv(s, val) my_putenv(s, val)
188
189 Miscellaneous functions
190 You should not even want to use setjmp.h functions, but if you think
191 you do, use the "JMPENV" stack in scope.h instead.
192
193 For "signal"/"sigaction", use "rsignal(signo, handler)".
194
196 "perlapi", "perlapio", "perlguts"
197
198
199
200perl v5.10.1 2009-02-12 PERLCLIB(1)