version 2, including all changes.
.
Rev |
Author |
# |
Line |
1 |
perry |
1 |
PERLCLIB |
|
|
2 |
!!!PERLCLIB |
|
|
3 |
NAME |
|
|
4 |
DESCRIPTION |
|
|
5 |
SEE ALSO |
|
|
6 |
---- |
|
|
7 |
!!NAME |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
perlclib - Internal replacements for standard C library functions |
|
|
11 |
!!DESCRIPTION |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
One thing Perl porters should note is that ''perl'' |
|
|
15 |
doesn't tend to use that much of the C standard library |
|
|
16 |
internally; you'll see very little use of, for example, the |
|
|
17 |
''ctype.h'' functions in there. This is because Perl |
|
|
18 |
tends to reimplement or abstract standard library functions, |
|
|
19 |
so that we know exactly how they're going to |
|
|
20 |
operate. |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
This is a reference card for people who are familiar with |
|
|
24 |
the C library and who want to do things the Perl way; to |
|
|
25 |
tell them which functions they ought to use instead of the |
|
|
26 |
more normal C functions. |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
__Conventions__ |
|
|
30 |
|
|
|
31 |
|
|
|
32 |
In the following tables: |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
t |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
is a type. |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
p |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
is a pointer. |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
n |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
is a number. |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
s |
|
|
54 |
|
|
|
55 |
|
|
|
56 |
is a string. |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
sv, av, hv, etc. represent |
|
|
60 |
variables of their respective types. |
|
|
61 |
|
|
|
62 |
|
|
|
63 |
__File Operations__ |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
Instead of the ''stdio.h'' functions, you should use the |
|
|
67 |
Perl abstraction layer. Instead of FILE* types, you |
|
|
68 |
need to be handling PerlIO* types. Don't forget |
|
|
69 |
that with the new PerlIO layered I/O abstraction |
|
|
70 |
FILE* types may not even be available. See also the |
|
|
71 |
perlapio documentation for more information about |
|
|
72 |
the following functions: |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
Instead Of: Use: |
|
|
76 |
stdin PerlIO_stdin() |
|
|
77 |
stdout PerlIO_stdout() |
|
|
78 |
stderr PerlIO_stderr() |
|
|
79 |
fopen(fn, mode) PerlIO_open(fn, mode) |
|
|
80 |
freopen(fn, mode, stream) PerlIO_reopen(fn, mode, perlio) (Deprecated) |
|
|
81 |
fflush(stream) PerlIO_flush(perlio) |
|
|
82 |
fclose(stream) PerlIO_close(perlio) |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
__File Input and Output__ |
|
|
86 |
|
|
|
87 |
|
|
|
88 |
Instead Of: Use: |
|
|
89 |
fprintf(stream, fmt, ...) PerlIO_printf(perlio, fmt, ...) |
|
|
90 |
[[f]getc(stream) PerlIO_getc(perlio) |
|
|
91 |
[[f]putc(stream, n) PerlIO_putc(perlio, n) |
|
|
92 |
ungetc(n, stream) PerlIO_ungetc(perlio, n) |
|
|
93 |
Note that the PerlIO equivalents of fread and fwrite are slightly different from their C library counterparts: |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
fread(p, size, n, stream) PerlIO_read(perlio, buf, numbytes) |
|
|
97 |
fwrite(p, size, n, stream) PerlIO_write(perlio, buf, numbytes) |
|
|
98 |
fputs(s, stream) PerlIO_puts(perlio, s) |
|
|
99 |
There is no equivalent to fgets; one should use sv_gets instead: |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
fgets(s, n, stream) sv_gets(sv, perlio, append) |
|
|
103 |
|
|
|
104 |
|
|
|
105 |
__File Positioning__ |
|
|
106 |
|
|
|
107 |
|
|
|
108 |
Instead Of: Use: |
|
|
109 |
feof(stream) PerlIO_eof(perlio) |
|
|
110 |
fseek(stream, n, whence) PerlIO_seek(perlio, n, whence) |
|
|
111 |
rewind(stream) PerlIO_rewind(perlio) |
|
|
112 |
fgetpos(stream, p) PerlIO_getpos(perlio, sv) |
|
|
113 |
fsetpos(stream, p) PerlIO_setpos(perlio, sv) |
|
|
114 |
ferror(stream) PerlIO_error(perlio) |
|
|
115 |
clearerr(stream) PerlIO_clearerr(perlio) |
|
|
116 |
|
|
|
117 |
|
|
|
118 |
__Memory Management and String Handling__ |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
Instead Of: Use: |
|
|
122 |
t* p = malloc(n) New(id, p, n, t) |
|
|
123 |
t* p = calloc(n, s) Newz(id, p, n, t) |
|
|
124 |
p = realloc(p, n) Renew(p, n, t) |
|
|
125 |
memcpy(dst, src, n) Copy(src, dst, n, t) |
|
|
126 |
memmove(dst, src, n) Move(src, dst, n, t) |
2 |
perry |
127 |
memcpy/*(struct foo *) !StructCopy(src, dst, t) |
1 |
perry |
128 |
free(p) Safefree(p) |
|
|
129 |
strdup(p) savepv(p) |
|
|
130 |
strndup(p, n) savepvn(p, n) (Hey, strndup doesn't exist!) |
|
|
131 |
strstr(big, little) instr(big, little) |
|
|
132 |
strcmp(s1, s2) strLE(s1, s2) / strEQ(s1, s2) / strGT(s1,s2) |
|
|
133 |
strncmp(s1, s2, n) strnNE(s1, s2, n) / strnEQ(s1, s2, n) |
|
|
134 |
Notice the different order of arguments to Copy and Move than used in memcpy and memmove. |
|
|
135 |
|
|
|
136 |
|
|
|
137 |
Most of the time, though, you'll want to be dealing with SVs |
|
|
138 |
internally instead of raw char * |
|
|
139 |
strings: |
|
|
140 |
|
|
|
141 |
|
|
|
142 |
strlen(s) sv_len(sv) |
|
|
143 |
strcpy(dt, src) sv_setpv(sv, s) |
|
|
144 |
strncpy(dt, src, n) sv_setpvn(sv, s, n) |
|
|
145 |
strcat(dt, src) sv_catpv(sv, s) |
|
|
146 |
strncat(dt, src) sv_catpvn(sv, s) |
|
|
147 |
sprintf(s, fmt, ...) sv_setpvf(sv, fmt, ...) |
|
|
148 |
Note also the existence of sv_catpvf and sv_catpvfn, combining concatenation with formatting. |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
__Character Class Tests__ |
|
|
152 |
|
|
|
153 |
|
|
|
154 |
There are two types of character class tests that Perl |
|
|
155 |
implements: one type deals in chars and are thus |
|
|
156 |
__not__ Unicode aware (and hence deprecated unless you |
|
|
157 |
__know__ you should use them) and the other type deal in |
|
|
158 |
UVs and know about Unicode properties. In the |
|
|
159 |
following table, c is a char, and |
|
|
160 |
u is a Unicode codepoint. |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
Instead Of: Use: But better use: |
|
|
164 |
isalnum(c) isALNUM(c) isALNUM_uni(u) |
|
|
165 |
isalpha(c) isALPHA(c) isALPHA_uni(u) |
|
|
166 |
iscntrl(c) isCNTRL(c) isCNTRL_uni(u) |
|
|
167 |
isdigit(c) isDIGIT(c) isDIGIT_uni(u) |
|
|
168 |
isgraph(c) isGRAPH(c) isGRAPH_uni(u) |
|
|
169 |
islower(c) isLOWER(c) isLOWER_uni(u) |
|
|
170 |
isprint(c) isPRINT(c) isPRINT_uni(u) |
|
|
171 |
ispunct(c) isPUNCT(c) isPUNCT_uni(u) |
|
|
172 |
isspace(c) isSPACE(c) isSPACE_uni(u) |
|
|
173 |
isupper(c) isUPPER(c) isUPPER_uni(u) |
|
|
174 |
isxdigit(c) isXDIGIT(c) isXDIGIT_uni(u) |
|
|
175 |
tolower(c) toLOWER(c) toLOWER_uni(u) |
|
|
176 |
toupper(c) toUPPER(c) toUPPER_uni(u) |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
''stdlib.h'' __functions__ |
|
|
180 |
|
|
|
181 |
|
|
|
182 |
Instead Of: Use: |
|
|
183 |
atof(s) Atof(s) |
|
|
184 |
atol(s) Atol(s) |
|
|
185 |
strtod(s, *p) Nothing. Just don't use it. |
|
|
186 |
strtol(s, *p, n) Strtol(s, *p, n) |
|
|
187 |
strtoul(s, *p, n) Strtoul(s, *p, n) |
|
|
188 |
Notice also the scan_bin, scan_hex, and scan_oct functions in ''util.c'' for converting strings representing numbers in the respective bases into NVs. |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
In theory Strtol and Strtoul may not be |
|
|
192 |
defined if the machine perl is built on doesn't actually |
|
|
193 |
have strtol and strtoul. But as those 2 functions are part |
|
|
194 |
of the 1989 ANSI C spec we suspect you'll |
|
|
195 |
find them everywhere by now. |
|
|
196 |
|
|
|
197 |
|
|
|
198 |
int rand() double Drand01() |
|
|
199 |
srand(n) { seedDrand01((Rand_seed_t)n); |
|
|
200 |
PL_srand_called = TRUE; } |
|
|
201 |
exit(n) my_exit(n) |
|
|
202 |
system(s) Don't. Look at pp_system or use my_popen |
2 |
perry |
203 |
getenv(s) !PerlEnv_getenv(s) |
1 |
perry |
204 |
setenv(s, val) my_putenv(s, val) |
|
|
205 |
|
|
|
206 |
|
|
|
207 |
__Miscellaneous functions__ |
|
|
208 |
|
|
|
209 |
|
|
|
210 |
You should not even __want__ to use ''setjmp.h'' |
|
|
211 |
functions, but if you think you do, use the JMPENV |
|
|
212 |
stack in ''scope.h'' instead. |
|
|
213 |
|
|
|
214 |
|
|
|
215 |
For signal/sigaction, use |
|
|
216 |
rsignal(signo, handler). |
|
|
217 |
!!SEE ALSO |
|
|
218 |
|
|
|
219 |
|
|
|
220 |
perlapi, perlapio, |
|
|
221 |
perlguts |
|
|
222 |
---- |