| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "PERLCLIB 1" |
| 132 | .TH PERLCLIB 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | perlclib \- Internal replacements for standard C library functions |
| 135 | .SH "DESCRIPTION" |
| 136 | .IX Header "DESCRIPTION" |
| 137 | One thing Perl porters should note is that \fIperl\fR doesn't tend to use that |
| 138 | much of the C standard library internally; you'll see very little use of, |
| 139 | for example, the \fIctype.h\fR functions in there. This is because Perl |
| 140 | tends to reimplement or abstract standard library functions, so that we |
| 141 | know exactly how they're going to operate. |
| 142 | .PP |
| 143 | This is a reference card for people who are familiar with the C library |
| 144 | and who want to do things the Perl way; to tell them which functions |
| 145 | they ought to use instead of the more normal C functions. |
| 146 | .Sh "Conventions" |
| 147 | .IX Subsection "Conventions" |
| 148 | In the following tables: |
| 149 | .ie n .IP """t""" 3 |
| 150 | .el .IP "\f(CWt\fR" 3 |
| 151 | .IX Item "t" |
| 152 | is a type. |
| 153 | .ie n .IP """p""" 3 |
| 154 | .el .IP "\f(CWp\fR" 3 |
| 155 | .IX Item "p" |
| 156 | is a pointer. |
| 157 | .ie n .IP """n""" 3 |
| 158 | .el .IP "\f(CWn\fR" 3 |
| 159 | .IX Item "n" |
| 160 | is a number. |
| 161 | .ie n .IP """s""" 3 |
| 162 | .el .IP "\f(CWs\fR" 3 |
| 163 | .IX Item "s" |
| 164 | is a string. |
| 165 | .PP |
| 166 | \&\f(CW\*(C`sv\*(C'\fR, \f(CW\*(C`av\*(C'\fR, \f(CW\*(C`hv\*(C'\fR, etc. represent variables of their respective types. |
| 167 | .Sh "File Operations" |
| 168 | .IX Subsection "File Operations" |
| 169 | Instead of the \fIstdio.h\fR functions, you should use the Perl abstraction |
| 170 | layer. Instead of \f(CW\*(C`FILE*\*(C'\fR types, you need to be handling \f(CW\*(C`PerlIO*\*(C'\fR |
| 171 | types. Don't forget that with the new PerlIO layered I/O abstraction |
| 172 | \&\f(CW\*(C`FILE*\*(C'\fR types may not even be available. See also the \f(CW\*(C`perlapio\*(C'\fR |
| 173 | documentation for more information about the following functions: |
| 174 | .PP |
| 175 | .Vb 1 |
| 176 | \& Instead Of: Use: |
| 177 | .Ve |
| 178 | .PP |
| 179 | .Vb 3 |
| 180 | \& stdin PerlIO_stdin() |
| 181 | \& stdout PerlIO_stdout() |
| 182 | \& stderr PerlIO_stderr() |
| 183 | .Ve |
| 184 | .PP |
| 185 | .Vb 4 |
| 186 | \& fopen(fn, mode) PerlIO_open(fn, mode) |
| 187 | \& freopen(fn, mode, stream) PerlIO_reopen(fn, mode, perlio) (Deprecated) |
| 188 | \& fflush(stream) PerlIO_flush(perlio) |
| 189 | \& fclose(stream) PerlIO_close(perlio) |
| 190 | .Ve |
| 191 | .Sh "File Input and Output" |
| 192 | .IX Subsection "File Input and Output" |
| 193 | .Vb 1 |
| 194 | \& Instead Of: Use: |
| 195 | .Ve |
| 196 | .PP |
| 197 | .Vb 1 |
| 198 | \& fprintf(stream, fmt, ...) PerlIO_printf(perlio, fmt, ...) |
| 199 | .Ve |
| 200 | .PP |
| 201 | .Vb 3 |
| 202 | \& [f]getc(stream) PerlIO_getc(perlio) |
| 203 | \& [f]putc(stream, n) PerlIO_putc(perlio, n) |
| 204 | \& ungetc(n, stream) PerlIO_ungetc(perlio, n) |
| 205 | .Ve |
| 206 | .PP |
| 207 | Note that the PerlIO equivalents of \f(CW\*(C`fread\*(C'\fR and \f(CW\*(C`fwrite\*(C'\fR are slightly |
| 208 | different from their C library counterparts: |
| 209 | .PP |
| 210 | .Vb 2 |
| 211 | \& fread(p, size, n, stream) PerlIO_read(perlio, buf, numbytes) |
| 212 | \& fwrite(p, size, n, stream) PerlIO_write(perlio, buf, numbytes) |
| 213 | .Ve |
| 214 | .PP |
| 215 | .Vb 1 |
| 216 | \& fputs(s, stream) PerlIO_puts(perlio, s) |
| 217 | .Ve |
| 218 | .PP |
| 219 | There is no equivalent to \f(CW\*(C`fgets\*(C'\fR; one should use \f(CW\*(C`sv_gets\*(C'\fR instead: |
| 220 | .PP |
| 221 | .Vb 1 |
| 222 | \& fgets(s, n, stream) sv_gets(sv, perlio, append) |
| 223 | .Ve |
| 224 | .Sh "File Positioning" |
| 225 | .IX Subsection "File Positioning" |
| 226 | .Vb 1 |
| 227 | \& Instead Of: Use: |
| 228 | .Ve |
| 229 | .PP |
| 230 | .Vb 3 |
| 231 | \& feof(stream) PerlIO_eof(perlio) |
| 232 | \& fseek(stream, n, whence) PerlIO_seek(perlio, n, whence) |
| 233 | \& rewind(stream) PerlIO_rewind(perlio) |
| 234 | .Ve |
| 235 | .PP |
| 236 | .Vb 2 |
| 237 | \& fgetpos(stream, p) PerlIO_getpos(perlio, sv) |
| 238 | \& fsetpos(stream, p) PerlIO_setpos(perlio, sv) |
| 239 | .Ve |
| 240 | .PP |
| 241 | .Vb 2 |
| 242 | \& ferror(stream) PerlIO_error(perlio) |
| 243 | \& clearerr(stream) PerlIO_clearerr(perlio) |
| 244 | .Ve |
| 245 | .Sh "Memory Management and String Handling" |
| 246 | .IX Subsection "Memory Management and String Handling" |
| 247 | .Vb 1 |
| 248 | \& Instead Of: Use: |
| 249 | .Ve |
| 250 | .PP |
| 251 | .Vb 9 |
| 252 | \& t* p = malloc(n) New(id, p, n, t) |
| 253 | \& t* p = calloc(n, s) Newz(id, p, n, t) |
| 254 | \& p = realloc(p, n) Renew(p, n, t) |
| 255 | \& memcpy(dst, src, n) Copy(src, dst, n, t) |
| 256 | \& memmove(dst, src, n) Move(src, dst, n, t) |
| 257 | \& memcpy/*(struct foo *) StructCopy(src, dst, t) |
| 258 | \& memset(dst, 0, n * sizeof(t)) Zero(dst, n, t) |
| 259 | \& memzero(dst, 0) Zero(dst, n, char) |
| 260 | \& free(p) Safefree(p) |
| 261 | .Ve |
| 262 | .PP |
| 263 | .Vb 2 |
| 264 | \& strdup(p) savepv(p) |
| 265 | \& strndup(p, n) savepvn(p, n) (Hey, strndup doesn't exist!) |
| 266 | .Ve |
| 267 | .PP |
| 268 | .Vb 3 |
| 269 | \& strstr(big, little) instr(big, little) |
| 270 | \& strcmp(s1, s2) strLE(s1, s2) / strEQ(s1, s2) / strGT(s1,s2) |
| 271 | \& strncmp(s1, s2, n) strnNE(s1, s2, n) / strnEQ(s1, s2, n) |
| 272 | .Ve |
| 273 | .PP |
| 274 | Notice the different order of arguments to \f(CW\*(C`Copy\*(C'\fR and \f(CW\*(C`Move\*(C'\fR than used |
| 275 | in \f(CW\*(C`memcpy\*(C'\fR and \f(CW\*(C`memmove\*(C'\fR. |
| 276 | .PP |
| 277 | Most of the time, though, you'll want to be dealing with SVs internally |
| 278 | instead of raw \f(CW\*(C`char *\*(C'\fR strings: |
| 279 | .PP |
| 280 | .Vb 6 |
| 281 | \& strlen(s) sv_len(sv) |
| 282 | \& strcpy(dt, src) sv_setpv(sv, s) |
| 283 | \& strncpy(dt, src, n) sv_setpvn(sv, s, n) |
| 284 | \& strcat(dt, src) sv_catpv(sv, s) |
| 285 | \& strncat(dt, src) sv_catpvn(sv, s) |
| 286 | \& sprintf(s, fmt, ...) sv_setpvf(sv, fmt, ...) |
| 287 | .Ve |
| 288 | .PP |
| 289 | Note also the existence of \f(CW\*(C`sv_catpvf\*(C'\fR and \f(CW\*(C`sv_vcatpvfn\*(C'\fR, combining |
| 290 | concatenation with formatting. |
| 291 | .PP |
| 292 | Sometimes instead of zeroing the allocated heap by using \fINewz()\fR you |
| 293 | should consider \*(L"poisoning\*(R" the data. This means writing a bit |
| 294 | pattern into it that should be illegal as pointers (and floating point |
| 295 | numbers), and also hopefully surprising enough as integers, so that |
| 296 | any code attempting to use the data without forethought will break |
| 297 | sooner rather than later. Poisoning can be done using the \fIPoison()\fR |
| 298 | macro, which has similar arguments as \fIZero()\fR: |
| 299 | .PP |
| 300 | .Vb 1 |
| 301 | \& Poison(dst, n, t) |
| 302 | .Ve |
| 303 | .Sh "Character Class Tests" |
| 304 | .IX Subsection "Character Class Tests" |
| 305 | There are two types of character class tests that Perl implements: one |
| 306 | type deals in \f(CW\*(C`char\*(C'\fRs and are thus \fBnot\fR Unicode aware (and hence |
| 307 | deprecated unless you \fBknow\fR you should use them) and the other type |
| 308 | deal in \f(CW\*(C`UV\*(C'\fRs and know about Unicode properties. In the following |
| 309 | table, \f(CW\*(C`c\*(C'\fR is a \f(CW\*(C`char\*(C'\fR, and \f(CW\*(C`u\*(C'\fR is a Unicode codepoint. |
| 310 | .PP |
| 311 | .Vb 1 |
| 312 | \& Instead Of: Use: But better use: |
| 313 | .Ve |
| 314 | .PP |
| 315 | .Vb 11 |
| 316 | \& isalnum(c) isALNUM(c) isALNUM_uni(u) |
| 317 | \& isalpha(c) isALPHA(c) isALPHA_uni(u) |
| 318 | \& iscntrl(c) isCNTRL(c) isCNTRL_uni(u) |
| 319 | \& isdigit(c) isDIGIT(c) isDIGIT_uni(u) |
| 320 | \& isgraph(c) isGRAPH(c) isGRAPH_uni(u) |
| 321 | \& islower(c) isLOWER(c) isLOWER_uni(u) |
| 322 | \& isprint(c) isPRINT(c) isPRINT_uni(u) |
| 323 | \& ispunct(c) isPUNCT(c) isPUNCT_uni(u) |
| 324 | \& isspace(c) isSPACE(c) isSPACE_uni(u) |
| 325 | \& isupper(c) isUPPER(c) isUPPER_uni(u) |
| 326 | \& isxdigit(c) isXDIGIT(c) isXDIGIT_uni(u) |
| 327 | .Ve |
| 328 | .PP |
| 329 | .Vb 2 |
| 330 | \& tolower(c) toLOWER(c) toLOWER_uni(u) |
| 331 | \& toupper(c) toUPPER(c) toUPPER_uni(u) |
| 332 | .Ve |
| 333 | .Sh "\fIstdlib.h\fP functions" |
| 334 | .IX Subsection "stdlib.h functions" |
| 335 | .Vb 1 |
| 336 | \& Instead Of: Use: |
| 337 | .Ve |
| 338 | .PP |
| 339 | .Vb 5 |
| 340 | \& atof(s) Atof(s) |
| 341 | \& atol(s) Atol(s) |
| 342 | \& strtod(s, *p) Nothing. Just don't use it. |
| 343 | \& strtol(s, *p, n) Strtol(s, *p, n) |
| 344 | \& strtoul(s, *p, n) Strtoul(s, *p, n) |
| 345 | .Ve |
| 346 | .PP |
| 347 | Notice also the \f(CW\*(C`grok_bin\*(C'\fR, \f(CW\*(C`grok_hex\*(C'\fR, and \f(CW\*(C`grok_oct\*(C'\fR functions in |
| 348 | \&\fInumeric.c\fR for converting strings representing numbers in the respective |
| 349 | bases into \f(CW\*(C`NV\*(C'\fRs. |
| 350 | .PP |
| 351 | In theory \f(CW\*(C`Strtol\*(C'\fR and \f(CW\*(C`Strtoul\*(C'\fR may not be defined if the machine perl is |
| 352 | built on doesn't actually have strtol and strtoul. But as those 2 |
| 353 | functions are part of the 1989 \s-1ANSI\s0 C spec we suspect you'll find them |
| 354 | everywhere by now. |
| 355 | .PP |
| 356 | .Vb 3 |
| 357 | \& int rand() double Drand01() |
| 358 | \& srand(n) { seedDrand01((Rand_seed_t)n); |
| 359 | \& PL_srand_called = TRUE; } |
| 360 | .Ve |
| 361 | .PP |
| 362 | .Vb 2 |
| 363 | \& exit(n) my_exit(n) |
| 364 | \& system(s) Don't. Look at pp_system or use my_popen |
| 365 | .Ve |
| 366 | .PP |
| 367 | .Vb 2 |
| 368 | \& getenv(s) PerlEnv_getenv(s) |
| 369 | \& setenv(s, val) my_putenv(s, val) |
| 370 | .Ve |
| 371 | .Sh "Miscellaneous functions" |
| 372 | .IX Subsection "Miscellaneous functions" |
| 373 | You should not even \fBwant\fR to use \fIsetjmp.h\fR functions, but if you |
| 374 | think you do, use the \f(CW\*(C`JMPENV\*(C'\fR stack in \fIscope.h\fR instead. |
| 375 | .PP |
| 376 | For \f(CW\*(C`signal\*(C'\fR/\f(CW\*(C`sigaction\*(C'\fR, use \f(CW\*(C`rsignal(signo, handler)\*(C'\fR. |
| 377 | .SH "SEE ALSO" |
| 378 | .IX Header "SEE ALSO" |
| 379 | \&\f(CW\*(C`perlapi\*(C'\fR, \f(CW\*(C`perlapio\*(C'\fR, \f(CW\*(C`perlguts\*(C'\fR |