Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.9 KiB

  1. =head1 NAME
  2. perlclib - Internal replacements for standard C library functions
  3. =head1 DESCRIPTION
  4. One thing Perl porters should note is that F<perl> doesn't tend to use that
  5. much of the C standard library internally; you'll see very little use of,
  6. for example, the F<ctype.h> functions in there. This is because Perl
  7. tends to reimplement or abstract standard library functions, so that we
  8. know exactly how they're going to operate.
  9. This is a reference card for people who are familiar with the C library
  10. and who want to do things the Perl way; to tell them which functions
  11. they ought to use instead of the more normal C functions.
  12. =head2 Conventions
  13. In the following tables:
  14. =over 3
  15. =item C<t>
  16. is a type.
  17. =item C<p>
  18. is a pointer.
  19. =item C<n>
  20. is a number.
  21. =item C<s>
  22. is a string.
  23. =back
  24. C<sv>, C<av>, C<hv>, etc. represent variables of their respective types.
  25. =head2 File Operations
  26. Instead of the F<stdio.h> functions, you should use the Perl abstraction
  27. layer. Instead of C<FILE*> types, you need to be handling C<PerlIO*>
  28. types. Don't forget that with the new PerlIO layered I/O abstraction
  29. C<FILE*> types may not even be available. See also the C<perlapio>
  30. documentation for more information about the following functions:
  31. Instead Of: Use:
  32. stdin PerlIO_stdin()
  33. stdout PerlIO_stdout()
  34. stderr PerlIO_stderr()
  35. fopen(fn, mode) PerlIO_open(fn, mode)
  36. freopen(fn, mode, stream) PerlIO_reopen(fn, mode, perlio) (Deprecated)
  37. fflush(stream) PerlIO_flush(perlio)
  38. fclose(stream) PerlIO_close(perlio)
  39. =head2 File Input and Output
  40. Instead Of: Use:
  41. fprintf(stream, fmt, ...) PerlIO_printf(perlio, fmt, ...)
  42. [f]getc(stream) PerlIO_getc(perlio)
  43. [f]putc(stream, n) PerlIO_putc(perlio, n)
  44. ungetc(n, stream) PerlIO_ungetc(perlio, n)
  45. Note that the PerlIO equivalents of C<fread> and C<fwrite> are slightly
  46. different from their C library counterparts:
  47. fread(p, size, n, stream) PerlIO_read(perlio, buf, numbytes)
  48. fwrite(p, size, n, stream) PerlIO_write(perlio, buf, numbytes)
  49. fputs(s, stream) PerlIO_puts(perlio, s)
  50. There is no equivalent to C<fgets>; one should use C<sv_gets> instead:
  51. fgets(s, n, stream) sv_gets(sv, perlio, append)
  52. =head2 File Positioning
  53. Instead Of: Use:
  54. feof(stream) PerlIO_eof(perlio)
  55. fseek(stream, n, whence) PerlIO_seek(perlio, n, whence)
  56. rewind(stream) PerlIO_rewind(perlio)
  57. fgetpos(stream, p) PerlIO_getpos(perlio, sv)
  58. fsetpos(stream, p) PerlIO_setpos(perlio, sv)
  59. ferror(stream) PerlIO_error(perlio)
  60. clearerr(stream) PerlIO_clearerr(perlio)
  61. =head2 Memory Management and String Handling
  62. Instead Of: Use:
  63. t* p = malloc(n) New(id, p, n, t)
  64. t* p = calloc(n, s) Newz(id, p, n, t)
  65. p = realloc(p, n) Renew(p, n, t)
  66. memcpy(dst, src, n) Copy(src, dst, n, t)
  67. memmove(dst, src, n) Move(src, dst, n, t)
  68. memcpy/*(struct foo *) StructCopy(src, dst, t)
  69. free(p) Safefree(p)
  70. strdup(p) savepv(p)
  71. strndup(p, n) savepvn(p, n) (Hey, strndup doesn't exist!)
  72. strstr(big, little) instr(big, little)
  73. strcmp(s1, s2) strLE(s1, s2) / strEQ(s1, s2) / strGT(s1,s2)
  74. strncmp(s1, s2, n) strnNE(s1, s2, n) / strnEQ(s1, s2, n)
  75. Notice the different order of arguments to C<Copy> and C<Move> than used
  76. in C<memcpy> and C<memmove>.
  77. Most of the time, though, you'll want to be dealing with SVs internally
  78. instead of raw C<char *> strings:
  79. strlen(s) sv_len(sv)
  80. strcpy(dt, src) sv_setpv(sv, s)
  81. strncpy(dt, src, n) sv_setpvn(sv, s, n)
  82. strcat(dt, src) sv_catpv(sv, s)
  83. strncat(dt, src) sv_catpvn(sv, s)
  84. sprintf(s, fmt, ...) sv_setpvf(sv, fmt, ...)
  85. Note also the existence of C<sv_catpvf> and C<sv_catpvfn>, combining
  86. concatenation with formatting.
  87. =head2 Character Class Tests
  88. There are two types of character class tests that Perl implements: one
  89. type deals in C<char>s and are thus B<not> Unicode aware (and hence
  90. deprecated unless you B<know> you should use them) and the other type
  91. deal in C<UV>s and know about Unicode properties. In the following
  92. table, C<c> is a C<char>, and C<u> is a Unicode codepoint.
  93. Instead Of: Use: But better use:
  94. isalnum(c) isALNUM(c) isALNUM_uni(u)
  95. isalpha(c) isALPHA(c) isALPHA_uni(u)
  96. iscntrl(c) isCNTRL(c) isCNTRL_uni(u)
  97. isdigit(c) isDIGIT(c) isDIGIT_uni(u)
  98. isgraph(c) isGRAPH(c) isGRAPH_uni(u)
  99. islower(c) isLOWER(c) isLOWER_uni(u)
  100. isprint(c) isPRINT(c) isPRINT_uni(u)
  101. ispunct(c) isPUNCT(c) isPUNCT_uni(u)
  102. isspace(c) isSPACE(c) isSPACE_uni(u)
  103. isupper(c) isUPPER(c) isUPPER_uni(u)
  104. isxdigit(c) isXDIGIT(c) isXDIGIT_uni(u)
  105. tolower(c) toLOWER(c) toLOWER_uni(u)
  106. toupper(c) toUPPER(c) toUPPER_uni(u)
  107. =head2 F<stdlib.h> functions
  108. Instead Of: Use:
  109. atof(s) Atof(s)
  110. atol(s) Atol(s)
  111. strtod(s, *p) Nothing. Just don't use it.
  112. strtol(s, *p, n) Strtol(s, *p, n)
  113. strtoul(s, *p, n) Strtoul(s, *p, n)
  114. Notice also the C<scan_bin>, C<scan_hex>, and C<scan_oct> functions in
  115. F<util.c> for converting strings representing numbers in the respective
  116. bases into C<NV>s.
  117. In theory C<Strtol> and C<Strtoul> may not be defined if the machine perl is
  118. built on doesn't actually have strtol and strtoul. But as those 2
  119. functions are part of the 1989 ANSI C spec we suspect you'll find them
  120. everywhere by now.
  121. int rand() double Drand01()
  122. srand(n) { seedDrand01((Rand_seed_t)n);
  123. PL_srand_called = TRUE; }
  124. exit(n) my_exit(n)
  125. system(s) Don't. Look at pp_system or use my_popen
  126. getenv(s) PerlEnv_getenv(s)
  127. setenv(s, val) my_putenv(s, val)
  128. =head2 Miscellaneous functions
  129. You should not even B<want> to use F<setjmp.h> functions, but if you
  130. think you do, use the C<JMPENV> stack in F<scope.h> instead.
  131. For C<signal>/C<sigaction>, use C<rsignal(signo, handler)>.
  132. =head1 SEE ALSO
  133. C<perlapi>, C<perlapio>, C<perlguts>