PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/lib/pod/perlguts.pod

#
Unknown | 2318 lines | 1722 code | 596 blank | 0 comment | 0 complexity | fc01ac7c98729bd3b2f13f0038743c8c MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. =head1 NAME
  2. perlguts - Introduction to the Perl API
  3. =head1 DESCRIPTION
  4. This document attempts to describe how to use the Perl API, as well as
  5. containing some info on the basic workings of the Perl core. It is far
  6. from complete and probably contains many errors. Please refer any
  7. questions or comments to the author below.
  8. =head1 Variables
  9. =head2 Datatypes
  10. Perl has three typedefs that handle Perl's three main data types:
  11. SV Scalar Value
  12. AV Array Value
  13. HV Hash Value
  14. Each typedef has specific routines that manipulate the various data types.
  15. =head2 What is an "IV"?
  16. Perl uses a special typedef IV which is a simple signed integer type that is
  17. guaranteed to be large enough to hold a pointer (as well as an integer).
  18. Additionally, there is the UV, which is simply an unsigned IV.
  19. Perl also uses two special typedefs, I32 and I16, which will always be at
  20. least 32-bits and 16-bits long, respectively. (Again, there are U32 and U16,
  21. as well.)
  22. =head2 Working with SVs
  23. An SV can be created and loaded with one command. There are four types of
  24. values that can be loaded: an integer value (IV), a double (NV),
  25. a string (PV), and another scalar (SV).
  26. The six routines are:
  27. SV* newSViv(IV);
  28. SV* newSVnv(double);
  29. SV* newSVpv(const char*, int);
  30. SV* newSVpvn(const char*, int);
  31. SV* newSVpvf(const char*, ...);
  32. SV* newSVsv(SV*);
  33. To change the value of an *already-existing* SV, there are seven routines:
  34. void sv_setiv(SV*, IV);
  35. void sv_setuv(SV*, UV);
  36. void sv_setnv(SV*, double);
  37. void sv_setpv(SV*, const char*);
  38. void sv_setpvn(SV*, const char*, int)
  39. void sv_setpvf(SV*, const char*, ...);
  40. void sv_setpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool);
  41. void sv_setsv(SV*, SV*);
  42. Notice that you can choose to specify the length of the string to be
  43. assigned by using C<sv_setpvn>, C<newSVpvn>, or C<newSVpv>, or you may
  44. allow Perl to calculate the length by using C<sv_setpv> or by specifying
  45. 0 as the second argument to C<newSVpv>. Be warned, though, that Perl will
  46. determine the string's length by using C<strlen>, which depends on the
  47. string terminating with a NUL character.
  48. The arguments of C<sv_setpvf> are processed like C<sprintf>, and the
  49. formatted output becomes the value.
  50. C<sv_setpvfn> is an analogue of C<vsprintf>, but it allows you to specify
  51. either a pointer to a variable argument list or the address and length of
  52. an array of SVs. The last argument points to a boolean; on return, if that
  53. boolean is true, then locale-specific information has been used to format
  54. the string, and the string's contents are therefore untrustworthy (see
  55. L<perlsec>). This pointer may be NULL if that information is not
  56. important. Note that this function requires you to specify the length of
  57. the format.
  58. STRLEN is an integer type (Size_t, usually defined as size_t in
  59. config.h) guaranteed to be large enough to represent the size of
  60. any string that perl can handle.
  61. The C<sv_set*()> functions are not generic enough to operate on values
  62. that have "magic". See L<Magic Virtual Tables> later in this document.
  63. All SVs that contain strings should be terminated with a NUL character.
  64. If it is not NUL-terminated there is a risk of
  65. core dumps and corruptions from code which passes the string to C
  66. functions or system calls which expect a NUL-terminated string.
  67. Perl's own functions typically add a trailing NUL for this reason.
  68. Nevertheless, you should be very careful when you pass a string stored
  69. in an SV to a C function or system call.
  70. To access the actual value that an SV points to, you can use the macros:
  71. SvIV(SV*)
  72. SvUV(SV*)
  73. SvNV(SV*)
  74. SvPV(SV*, STRLEN len)
  75. SvPV_nolen(SV*)
  76. which will automatically coerce the actual scalar type into an IV, UV, double,
  77. or string.
  78. In the C<SvPV> macro, the length of the string returned is placed into the
  79. variable C<len> (this is a macro, so you do I<not> use C<&len>). If you do
  80. not care what the length of the data is, use the C<SvPV_nolen> macro.
  81. Historically the C<SvPV> macro with the global variable C<PL_na> has been
  82. used in this case. But that can be quite inefficient because C<PL_na> must
  83. be accessed in thread-local storage in threaded Perl. In any case, remember
  84. that Perl allows arbitrary strings of data that may both contain NULs and
  85. might not be terminated by a NUL.
  86. Also remember that C doesn't allow you to safely say C<foo(SvPV(s, len),
  87. len);>. It might work with your compiler, but it won't work for everyone.
  88. Break this sort of statement up into separate assignments:
  89. SV *s;
  90. STRLEN len;
  91. char * ptr;
  92. ptr = SvPV(s, len);
  93. foo(ptr, len);
  94. If you want to know if the scalar value is TRUE, you can use:
  95. SvTRUE(SV*)
  96. Although Perl will automatically grow strings for you, if you need to force
  97. Perl to allocate more memory for your SV, you can use the macro
  98. SvGROW(SV*, STRLEN newlen)
  99. which will determine if more memory needs to be allocated. If so, it will
  100. call the function C<sv_grow>. Note that C<SvGROW> can only increase, not
  101. decrease, the allocated memory of an SV and that it does not automatically
  102. add a byte for the a trailing NUL (perl's own string functions typically do
  103. C<SvGROW(sv, len + 1)>).
  104. If you have an SV and want to know what kind of data Perl thinks is stored
  105. in it, you can use the following macros to check the type of SV you have.
  106. SvIOK(SV*)
  107. SvNOK(SV*)
  108. SvPOK(SV*)
  109. You can get and set the current length of the string stored in an SV with
  110. the following macros:
  111. SvCUR(SV*)
  112. SvCUR_set(SV*, I32 val)
  113. You can also get a pointer to the end of the string stored in the SV
  114. with the macro:
  115. SvEND(SV*)
  116. But note that these last three macros are valid only if C<SvPOK()> is true.
  117. If you want to append something to the end of string stored in an C<SV*>,
  118. you can use the following functions:
  119. void sv_catpv(SV*, const char*);
  120. void sv_catpvn(SV*, const char*, STRLEN);
  121. void sv_catpvf(SV*, const char*, ...);
  122. void sv_catpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool);
  123. void sv_catsv(SV*, SV*);
  124. The first function calculates the length of the string to be appended by
  125. using C<strlen>. In the second, you specify the length of the string
  126. yourself. The third function processes its arguments like C<sprintf> and
  127. appends the formatted output. The fourth function works like C<vsprintf>.
  128. You can specify the address and length of an array of SVs instead of the
  129. va_list argument. The fifth function extends the string stored in the first
  130. SV with the string stored in the second SV. It also forces the second SV
  131. to be interpreted as a string.
  132. The C<sv_cat*()> functions are not generic enough to operate on values that
  133. have "magic". See L<Magic Virtual Tables> later in this document.
  134. If you know the name of a scalar variable, you can get a pointer to its SV
  135. by using the following:
  136. SV* get_sv("package::varname", FALSE);
  137. This returns NULL if the variable does not exist.
  138. If you want to know if this variable (or any other SV) is actually C<defined>,
  139. you can call:
  140. SvOK(SV*)
  141. The scalar C<undef> value is stored in an SV instance called C<PL_sv_undef>. Its
  142. address can be used whenever an C<SV*> is needed.
  143. There are also the two values C<PL_sv_yes> and C<PL_sv_no>, which contain Boolean
  144. TRUE and FALSE values, respectively. Like C<PL_sv_undef>, their addresses can
  145. be used whenever an C<SV*> is needed.
  146. Do not be fooled into thinking that C<(SV *) 0> is the same as C<&PL_sv_undef>.
  147. Take this code:
  148. SV* sv = (SV*) 0;
  149. if (I-am-to-return-a-real-value) {
  150. sv = sv_2mortal(newSViv(42));
  151. }
  152. sv_setsv(ST(0), sv);
  153. This code tries to return a new SV (which contains the value 42) if it should
  154. return a real value, or undef otherwise. Instead it has returned a NULL
  155. pointer which, somewhere down the line, will cause a segmentation violation,
  156. bus error, or just weird results. Change the zero to C<&PL_sv_undef> in the first
  157. line and all will be well.
  158. To free an SV that you've created, call C<SvREFCNT_dec(SV*)>. Normally this
  159. call is not necessary (see L<Reference Counts and Mortality>).
  160. =head2 Offsets
  161. Perl provides the function C<sv_chop> to efficiently remove characters
  162. from the beginning of a string; you give it an SV and a pointer to
  163. somewhere inside the the PV, and it discards everything before the
  164. pointer. The efficiency comes by means of a little hack: instead of
  165. actually removing the characters, C<sv_chop> sets the flag C<OOK>
  166. (offset OK) to signal to other functions that the offset hack is in
  167. effect, and it puts the number of bytes chopped off into the IV field
  168. of the SV. It then moves the PV pointer (called C<SvPVX>) forward that
  169. many bytes, and adjusts C<SvCUR> and C<SvLEN>.
  170. Hence, at this point, the start of the buffer that we allocated lives
  171. at C<SvPVX(sv) - SvIV(sv)> in memory and the PV pointer is pointing
  172. into the middle of this allocated storage.
  173. This is best demonstrated by example:
  174. % ./perl -Ilib -MDevel::Peek -le '$a="12345"; $a=~s/.//; Dump($a)'
  175. SV = PVIV(0x8128450) at 0x81340f0
  176. REFCNT = 1
  177. FLAGS = (POK,OOK,pPOK)
  178. IV = 1 (OFFSET)
  179. PV = 0x8135781 ( "1" . ) "2345"\0
  180. CUR = 4
  181. LEN = 5
  182. Here the number of bytes chopped off (1) is put into IV, and
  183. C<Devel::Peek::Dump> helpfully reminds us that this is an offset. The
  184. portion of the string between the "real" and the "fake" beginnings is
  185. shown in parentheses, and the values of C<SvCUR> and C<SvLEN> reflect
  186. the fake beginning, not the real one.
  187. Something similar to the offset hack is perfomed on AVs to enable
  188. efficient shifting and splicing off the beginning of the array; while
  189. C<AvARRAY> points to the first element in the array that is visible from
  190. Perl, C<AvALLOC> points to the real start of the C array. These are
  191. usually the same, but a C<shift> operation can be carried out by
  192. increasing C<AvARRAY> by one and decreasing C<AvFILL> and C<AvLEN>.
  193. Again, the location of the real start of the C array only comes into
  194. play when freeing the array. See C<av_shift> in F<av.c>.
  195. =head2 What's Really Stored in an SV?
  196. Recall that the usual method of determining the type of scalar you have is
  197. to use C<Sv*OK> macros. Because a scalar can be both a number and a string,
  198. usually these macros will always return TRUE and calling the C<Sv*V>
  199. macros will do the appropriate conversion of string to integer/double or
  200. integer/double to string.
  201. If you I<really> need to know if you have an integer, double, or string
  202. pointer in an SV, you can use the following three macros instead:
  203. SvIOKp(SV*)
  204. SvNOKp(SV*)
  205. SvPOKp(SV*)
  206. These will tell you if you truly have an integer, double, or string pointer
  207. stored in your SV. The "p" stands for private.
  208. In general, though, it's best to use the C<Sv*V> macros.
  209. =head2 Working with AVs
  210. There are two ways to create and load an AV. The first method creates an
  211. empty AV:
  212. AV* newAV();
  213. The second method both creates the AV and initially populates it with SVs:
  214. AV* av_make(I32 num, SV **ptr);
  215. The second argument points to an array containing C<num> C<SV*>'s. Once the
  216. AV has been created, the SVs can be destroyed, if so desired.
  217. Once the AV has been created, the following operations are possible on AVs:
  218. void av_push(AV*, SV*);
  219. SV* av_pop(AV*);
  220. SV* av_shift(AV*);
  221. void av_unshift(AV*, I32 num);
  222. These should be familiar operations, with the exception of C<av_unshift>.
  223. This routine adds C<num> elements at the front of the array with the C<undef>
  224. value. You must then use C<av_store> (described below) to assign values
  225. to these new elements.
  226. Here are some other functions:
  227. I32 av_len(AV*);
  228. SV** av_fetch(AV*, I32 key, I32 lval);
  229. SV** av_store(AV*, I32 key, SV* val);
  230. The C<av_len> function returns the highest index value in array (just
  231. like $#array in Perl). If the array is empty, -1 is returned. The
  232. C<av_fetch> function returns the value at index C<key>, but if C<lval>
  233. is non-zero, then C<av_fetch> will store an undef value at that index.
  234. The C<av_store> function stores the value C<val> at index C<key>, and does
  235. not increment the reference count of C<val>. Thus the caller is responsible
  236. for taking care of that, and if C<av_store> returns NULL, the caller will
  237. have to decrement the reference count to avoid a memory leak. Note that
  238. C<av_fetch> and C<av_store> both return C<SV**>'s, not C<SV*>'s as their
  239. return value.
  240. void av_clear(AV*);
  241. void av_undef(AV*);
  242. void av_extend(AV*, I32 key);
  243. The C<av_clear> function deletes all the elements in the AV* array, but
  244. does not actually delete the array itself. The C<av_undef> function will
  245. delete all the elements in the array plus the array itself. The
  246. C<av_extend> function extends the array so that it contains at least C<key+1>
  247. elements. If C<key+1> is less than the currently allocated length of the array,
  248. then nothing is done.
  249. If you know the name of an array variable, you can get a pointer to its AV
  250. by using the following:
  251. AV* get_av("package::varname", FALSE);
  252. This returns NULL if the variable does not exist.
  253. See L<Understanding the Magic of Tied Hashes and Arrays> for more
  254. information on how to use the array access functions on tied arrays.
  255. =head2 Working with HVs
  256. To create an HV, you use the following routine:
  257. HV* newHV();
  258. Once the HV has been created, the following operations are possible on HVs:
  259. SV** hv_store(HV*, const char* key, U32 klen, SV* val, U32 hash);
  260. SV** hv_fetch(HV*, const char* key, U32 klen, I32 lval);
  261. The C<klen> parameter is the length of the key being passed in (Note that
  262. you cannot pass 0 in as a value of C<klen> to tell Perl to measure the
  263. length of the key). The C<val> argument contains the SV pointer to the
  264. scalar being stored, and C<hash> is the precomputed hash value (zero if
  265. you want C<hv_store> to calculate it for you). The C<lval> parameter
  266. indicates whether this fetch is actually a part of a store operation, in
  267. which case a new undefined value will be added to the HV with the supplied
  268. key and C<hv_fetch> will return as if the value had already existed.
  269. Remember that C<hv_store> and C<hv_fetch> return C<SV**>'s and not just
  270. C<SV*>. To access the scalar value, you must first dereference the return
  271. value. However, you should check to make sure that the return value is
  272. not NULL before dereferencing it.
  273. These two functions check if a hash table entry exists, and deletes it.
  274. bool hv_exists(HV*, const char* key, U32 klen);
  275. SV* hv_delete(HV*, const char* key, U32 klen, I32 flags);
  276. If C<flags> does not include the C<G_DISCARD> flag then C<hv_delete> will
  277. create and return a mortal copy of the deleted value.
  278. And more miscellaneous functions:
  279. void hv_clear(HV*);
  280. void hv_undef(HV*);
  281. Like their AV counterparts, C<hv_clear> deletes all the entries in the hash
  282. table but does not actually delete the hash table. The C<hv_undef> deletes
  283. both the entries and the hash table itself.
  284. Perl keeps the actual data in linked list of structures with a typedef of HE.
  285. These contain the actual key and value pointers (plus extra administrative
  286. overhead). The key is a string pointer; the value is an C<SV*>. However,
  287. once you have an C<HE*>, to get the actual key and value, use the routines
  288. specified below.
  289. I32 hv_iterinit(HV*);
  290. /* Prepares starting point to traverse hash table */
  291. HE* hv_iternext(HV*);
  292. /* Get the next entry, and return a pointer to a
  293. structure that has both the key and value */
  294. char* hv_iterkey(HE* entry, I32* retlen);
  295. /* Get the key from an HE structure and also return
  296. the length of the key string */
  297. SV* hv_iterval(HV*, HE* entry);
  298. /* Return a SV pointer to the value of the HE
  299. structure */
  300. SV* hv_iternextsv(HV*, char** key, I32* retlen);
  301. /* This convenience routine combines hv_iternext,
  302. hv_iterkey, and hv_iterval. The key and retlen
  303. arguments are return values for the key and its
  304. length. The value is returned in the SV* argument */
  305. If you know the name of a hash variable, you can get a pointer to its HV
  306. by using the following:
  307. HV* get_hv("package::varname", FALSE);
  308. This returns NULL if the variable does not exist.
  309. The hash algorithm is defined in the C<PERL_HASH(hash, key, klen)> macro:
  310. hash = 0;
  311. while (klen--)
  312. hash = (hash * 33) + *key++;
  313. hash = hash + (hash >> 5); /* after 5.6 */
  314. The last step was added in version 5.6 to improve distribution of
  315. lower bits in the resulting hash value.
  316. See L<Understanding the Magic of Tied Hashes and Arrays> for more
  317. information on how to use the hash access functions on tied hashes.
  318. =head2 Hash API Extensions
  319. Beginning with version 5.004, the following functions are also supported:
  320. HE* hv_fetch_ent (HV* tb, SV* key, I32 lval, U32 hash);
  321. HE* hv_store_ent (HV* tb, SV* key, SV* val, U32 hash);
  322. bool hv_exists_ent (HV* tb, SV* key, U32 hash);
  323. SV* hv_delete_ent (HV* tb, SV* key, I32 flags, U32 hash);
  324. SV* hv_iterkeysv (HE* entry);
  325. Note that these functions take C<SV*> keys, which simplifies writing
  326. of extension code that deals with hash structures. These functions
  327. also allow passing of C<SV*> keys to C<tie> functions without forcing
  328. you to stringify the keys (unlike the previous set of functions).
  329. They also return and accept whole hash entries (C<HE*>), making their
  330. use more efficient (since the hash number for a particular string
  331. doesn't have to be recomputed every time). See L<perlapi> for detailed
  332. descriptions.
  333. The following macros must always be used to access the contents of hash
  334. entries. Note that the arguments to these macros must be simple
  335. variables, since they may get evaluated more than once. See
  336. L<perlapi> for detailed descriptions of these macros.
  337. HePV(HE* he, STRLEN len)
  338. HeVAL(HE* he)
  339. HeHASH(HE* he)
  340. HeSVKEY(HE* he)
  341. HeSVKEY_force(HE* he)
  342. HeSVKEY_set(HE* he, SV* sv)
  343. These two lower level macros are defined, but must only be used when
  344. dealing with keys that are not C<SV*>s:
  345. HeKEY(HE* he)
  346. HeKLEN(HE* he)
  347. Note that both C<hv_store> and C<hv_store_ent> do not increment the
  348. reference count of the stored C<val>, which is the caller's responsibility.
  349. If these functions return a NULL value, the caller will usually have to
  350. decrement the reference count of C<val> to avoid a memory leak.
  351. =head2 References
  352. References are a special type of scalar that point to other data types
  353. (including references).
  354. To create a reference, use either of the following functions:
  355. SV* newRV_inc((SV*) thing);
  356. SV* newRV_noinc((SV*) thing);
  357. The C<thing> argument can be any of an C<SV*>, C<AV*>, or C<HV*>. The
  358. functions are identical except that C<newRV_inc> increments the reference
  359. count of the C<thing>, while C<newRV_noinc> does not. For historical
  360. reasons, C<newRV> is a synonym for C<newRV_inc>.
  361. Once you have a reference, you can use the following macro to dereference
  362. the reference:
  363. SvRV(SV*)
  364. then call the appropriate routines, casting the returned C<SV*> to either an
  365. C<AV*> or C<HV*>, if required.
  366. To determine if an SV is a reference, you can use the following macro:
  367. SvROK(SV*)
  368. To discover what type of value the reference refers to, use the following
  369. macro and then check the return value.
  370. SvTYPE(SvRV(SV*))
  371. The most useful types that will be returned are:
  372. SVt_IV Scalar
  373. SVt_NV Scalar
  374. SVt_PV Scalar
  375. SVt_RV Scalar
  376. SVt_PVAV Array
  377. SVt_PVHV Hash
  378. SVt_PVCV Code
  379. SVt_PVGV Glob (possible a file handle)
  380. SVt_PVMG Blessed or Magical Scalar
  381. See the sv.h header file for more details.
  382. =head2 Blessed References and Class Objects
  383. References are also used to support object-oriented programming. In the
  384. OO lexicon, an object is simply a reference that has been blessed into a
  385. package (or class). Once blessed, the programmer may now use the reference
  386. to access the various methods in the class.
  387. A reference can be blessed into a package with the following function:
  388. SV* sv_bless(SV* sv, HV* stash);
  389. The C<sv> argument must be a reference. The C<stash> argument specifies
  390. which class the reference will belong to. See
  391. L<Stashes and Globs> for information on converting class names into stashes.
  392. /* Still under construction */
  393. Upgrades rv to reference if not already one. Creates new SV for rv to
  394. point to. If C<classname> is non-null, the SV is blessed into the specified
  395. class. SV is returned.
  396. SV* newSVrv(SV* rv, const char* classname);
  397. Copies integer or double into an SV whose reference is C<rv>. SV is blessed
  398. if C<classname> is non-null.
  399. SV* sv_setref_iv(SV* rv, const char* classname, IV iv);
  400. SV* sv_setref_nv(SV* rv, const char* classname, NV iv);
  401. Copies the pointer value (I<the address, not the string!>) into an SV whose
  402. reference is rv. SV is blessed if C<classname> is non-null.
  403. SV* sv_setref_pv(SV* rv, const char* classname, PV iv);
  404. Copies string into an SV whose reference is C<rv>. Set length to 0 to let
  405. Perl calculate the string length. SV is blessed if C<classname> is non-null.
  406. SV* sv_setref_pvn(SV* rv, const char* classname, PV iv, STRLEN length);
  407. Tests whether the SV is blessed into the specified class. It does not
  408. check inheritance relationships.
  409. int sv_isa(SV* sv, const char* name);
  410. Tests whether the SV is a reference to a blessed object.
  411. int sv_isobject(SV* sv);
  412. Tests whether the SV is derived from the specified class. SV can be either
  413. a reference to a blessed object or a string containing a class name. This
  414. is the function implementing the C<UNIVERSAL::isa> functionality.
  415. bool sv_derived_from(SV* sv, const char* name);
  416. To check if you've got an object derived from a specific class you have
  417. to write:
  418. if (sv_isobject(sv) && sv_derived_from(sv, class)) { ... }
  419. =head2 Creating New Variables
  420. To create a new Perl variable with an undef value which can be accessed from
  421. your Perl script, use the following routines, depending on the variable type.
  422. SV* get_sv("package::varname", TRUE);
  423. AV* get_av("package::varname", TRUE);
  424. HV* get_hv("package::varname", TRUE);
  425. Notice the use of TRUE as the second parameter. The new variable can now
  426. be set, using the routines appropriate to the data type.
  427. There are additional macros whose values may be bitwise OR'ed with the
  428. C<TRUE> argument to enable certain extra features. Those bits are:
  429. GV_ADDMULTI Marks the variable as multiply defined, thus preventing the
  430. "Name <varname> used only once: possible typo" warning.
  431. GV_ADDWARN Issues the warning "Had to create <varname> unexpectedly" if
  432. the variable did not exist before the function was called.
  433. If you do not specify a package name, the variable is created in the current
  434. package.
  435. =head2 Reference Counts and Mortality
  436. Perl uses an reference count-driven garbage collection mechanism. SVs,
  437. AVs, or HVs (xV for short in the following) start their life with a
  438. reference count of 1. If the reference count of an xV ever drops to 0,
  439. then it will be destroyed and its memory made available for reuse.
  440. This normally doesn't happen at the Perl level unless a variable is
  441. undef'ed or the last variable holding a reference to it is changed or
  442. overwritten. At the internal level, however, reference counts can be
  443. manipulated with the following macros:
  444. int SvREFCNT(SV* sv);
  445. SV* SvREFCNT_inc(SV* sv);
  446. void SvREFCNT_dec(SV* sv);
  447. However, there is one other function which manipulates the reference
  448. count of its argument. The C<newRV_inc> function, you will recall,
  449. creates a reference to the specified argument. As a side effect,
  450. it increments the argument's reference count. If this is not what
  451. you want, use C<newRV_noinc> instead.
  452. For example, imagine you want to return a reference from an XSUB function.
  453. Inside the XSUB routine, you create an SV which initially has a reference
  454. count of one. Then you call C<newRV_inc>, passing it the just-created SV.
  455. This returns the reference as a new SV, but the reference count of the
  456. SV you passed to C<newRV_inc> has been incremented to two. Now you
  457. return the reference from the XSUB routine and forget about the SV.
  458. But Perl hasn't! Whenever the returned reference is destroyed, the
  459. reference count of the original SV is decreased to one and nothing happens.
  460. The SV will hang around without any way to access it until Perl itself
  461. terminates. This is a memory leak.
  462. The correct procedure, then, is to use C<newRV_noinc> instead of
  463. C<newRV_inc>. Then, if and when the last reference is destroyed,
  464. the reference count of the SV will go to zero and it will be destroyed,
  465. stopping any memory leak.
  466. There are some convenience functions available that can help with the
  467. destruction of xVs. These functions introduce the concept of "mortality".
  468. An xV that is mortal has had its reference count marked to be decremented,
  469. but not actually decremented, until "a short time later". Generally the
  470. term "short time later" means a single Perl statement, such as a call to
  471. an XSUB function. The actual determinant for when mortal xVs have their
  472. reference count decremented depends on two macros, SAVETMPS and FREETMPS.
  473. See L<perlcall> and L<perlxs> for more details on these macros.
  474. "Mortalization" then is at its simplest a deferred C<SvREFCNT_dec>.
  475. However, if you mortalize a variable twice, the reference count will
  476. later be decremented twice.
  477. You should be careful about creating mortal variables. Strange things
  478. can happen if you make the same value mortal within multiple contexts,
  479. or if you make a variable mortal multiple times.
  480. To create a mortal variable, use the functions:
  481. SV* sv_newmortal()
  482. SV* sv_2mortal(SV*)
  483. SV* sv_mortalcopy(SV*)
  484. The first call creates a mortal SV, the second converts an existing
  485. SV to a mortal SV (and thus defers a call to C<SvREFCNT_dec>), and the
  486. third creates a mortal copy of an existing SV.
  487. The mortal routines are not just for SVs -- AVs and HVs can be
  488. made mortal by passing their address (type-casted to C<SV*>) to the
  489. C<sv_2mortal> or C<sv_mortalcopy> routines.
  490. =head2 Stashes and Globs
  491. A "stash" is a hash that contains all of the different objects that
  492. are contained within a package. Each key of the stash is a symbol
  493. name (shared by all the different types of objects that have the same
  494. name), and each value in the hash table is a GV (Glob Value). This GV
  495. in turn contains references to the various objects of that name,
  496. including (but not limited to) the following:
  497. Scalar Value
  498. Array Value
  499. Hash Value
  500. I/O Handle
  501. Format
  502. Subroutine
  503. There is a single stash called "PL_defstash" that holds the items that exist
  504. in the "main" package. To get at the items in other packages, append the
  505. string "::" to the package name. The items in the "Foo" package are in
  506. the stash "Foo::" in PL_defstash. The items in the "Bar::Baz" package are
  507. in the stash "Baz::" in "Bar::"'s stash.
  508. To get the stash pointer for a particular package, use the function:
  509. HV* gv_stashpv(const char* name, I32 create)
  510. HV* gv_stashsv(SV*, I32 create)
  511. The first function takes a literal string, the second uses the string stored
  512. in the SV. Remember that a stash is just a hash table, so you get back an
  513. C<HV*>. The C<create> flag will create a new package if it is set.
  514. The name that C<gv_stash*v> wants is the name of the package whose symbol table
  515. you want. The default package is called C<main>. If you have multiply nested
  516. packages, pass their names to C<gv_stash*v>, separated by C<::> as in the Perl
  517. language itself.
  518. Alternately, if you have an SV that is a blessed reference, you can find
  519. out the stash pointer by using:
  520. HV* SvSTASH(SvRV(SV*));
  521. then use the following to get the package name itself:
  522. char* HvNAME(HV* stash);
  523. If you need to bless or re-bless an object you can use the following
  524. function:
  525. SV* sv_bless(SV*, HV* stash)
  526. where the first argument, an C<SV*>, must be a reference, and the second
  527. argument is a stash. The returned C<SV*> can now be used in the same way
  528. as any other SV.
  529. For more information on references and blessings, consult L<perlref>.
  530. =head2 Double-Typed SVs
  531. Scalar variables normally contain only one type of value, an integer,
  532. double, pointer, or reference. Perl will automatically convert the
  533. actual scalar data from the stored type into the requested type.
  534. Some scalar variables contain more than one type of scalar data. For
  535. example, the variable C<$!> contains either the numeric value of C<errno>
  536. or its string equivalent from either C<strerror> or C<sys_errlist[]>.
  537. To force multiple data values into an SV, you must do two things: use the
  538. C<sv_set*v> routines to add the additional scalar type, then set a flag
  539. so that Perl will believe it contains more than one type of data. The
  540. four macros to set the flags are:
  541. SvIOK_on
  542. SvNOK_on
  543. SvPOK_on
  544. SvROK_on
  545. The particular macro you must use depends on which C<sv_set*v> routine
  546. you called first. This is because every C<sv_set*v> routine turns on
  547. only the bit for the particular type of data being set, and turns off
  548. all the rest.
  549. For example, to create a new Perl variable called "dberror" that contains
  550. both the numeric and descriptive string error values, you could use the
  551. following code:
  552. extern int dberror;
  553. extern char *dberror_list;
  554. SV* sv = get_sv("dberror", TRUE);
  555. sv_setiv(sv, (IV) dberror);
  556. sv_setpv(sv, dberror_list[dberror]);
  557. SvIOK_on(sv);
  558. If the order of C<sv_setiv> and C<sv_setpv> had been reversed, then the
  559. macro C<SvPOK_on> would need to be called instead of C<SvIOK_on>.
  560. =head2 Magic Variables
  561. [This section still under construction. Ignore everything here. Post no
  562. bills. Everything not permitted is forbidden.]
  563. Any SV may be magical, that is, it has special features that a normal
  564. SV does not have. These features are stored in the SV structure in a
  565. linked list of C<struct magic>'s, typedef'ed to C<MAGIC>.
  566. struct magic {
  567. MAGIC* mg_moremagic;
  568. MGVTBL* mg_virtual;
  569. U16 mg_private;
  570. char mg_type;
  571. U8 mg_flags;
  572. SV* mg_obj;
  573. char* mg_ptr;
  574. I32 mg_len;
  575. };
  576. Note this is current as of patchlevel 0, and could change at any time.
  577. =head2 Assigning Magic
  578. Perl adds magic to an SV using the sv_magic function:
  579. void sv_magic(SV* sv, SV* obj, int how, const char* name, I32 namlen);
  580. The C<sv> argument is a pointer to the SV that is to acquire a new magical
  581. feature.
  582. If C<sv> is not already magical, Perl uses the C<SvUPGRADE> macro to
  583. set the C<SVt_PVMG> flag for the C<sv>. Perl then continues by adding
  584. it to the beginning of the linked list of magical features. Any prior
  585. entry of the same type of magic is deleted. Note that this can be
  586. overridden, and multiple instances of the same type of magic can be
  587. associated with an SV.
  588. The C<name> and C<namlen> arguments are used to associate a string with
  589. the magic, typically the name of a variable. C<namlen> is stored in the
  590. C<mg_len> field and if C<name> is non-null and C<namlen> >= 0 a malloc'd
  591. copy of the name is stored in C<mg_ptr> field.
  592. The sv_magic function uses C<how> to determine which, if any, predefined
  593. "Magic Virtual Table" should be assigned to the C<mg_virtual> field.
  594. See the "Magic Virtual Table" section below. The C<how> argument is also
  595. stored in the C<mg_type> field.
  596. The C<obj> argument is stored in the C<mg_obj> field of the C<MAGIC>
  597. structure. If it is not the same as the C<sv> argument, the reference
  598. count of the C<obj> object is incremented. If it is the same, or if
  599. the C<how> argument is "#", or if it is a NULL pointer, then C<obj> is
  600. merely stored, without the reference count being incremented.
  601. There is also a function to add magic to an C<HV>:
  602. void hv_magic(HV *hv, GV *gv, int how);
  603. This simply calls C<sv_magic> and coerces the C<gv> argument into an C<SV>.
  604. To remove the magic from an SV, call the function sv_unmagic:
  605. void sv_unmagic(SV *sv, int type);
  606. The C<type> argument should be equal to the C<how> value when the C<SV>
  607. was initially made magical.
  608. =head2 Magic Virtual Tables
  609. The C<mg_virtual> field in the C<MAGIC> structure is a pointer to a
  610. C<MGVTBL>, which is a structure of function pointers and stands for
  611. "Magic Virtual Table" to handle the various operations that might be
  612. applied to that variable.
  613. The C<MGVTBL> has five pointers to the following routine types:
  614. int (*svt_get)(SV* sv, MAGIC* mg);
  615. int (*svt_set)(SV* sv, MAGIC* mg);
  616. U32 (*svt_len)(SV* sv, MAGIC* mg);
  617. int (*svt_clear)(SV* sv, MAGIC* mg);
  618. int (*svt_free)(SV* sv, MAGIC* mg);
  619. This MGVTBL structure is set at compile-time in C<perl.h> and there are
  620. currently 19 types (or 21 with overloading turned on). These different
  621. structures contain pointers to various routines that perform additional
  622. actions depending on which function is being called.
  623. Function pointer Action taken
  624. ---------------- ------------
  625. svt_get Do something after the value of the SV is retrieved.
  626. svt_set Do something after the SV is assigned a value.
  627. svt_len Report on the SV's length.
  628. svt_clear Clear something the SV represents.
  629. svt_free Free any extra storage associated with the SV.
  630. For instance, the MGVTBL structure called C<vtbl_sv> (which corresponds
  631. to an C<mg_type> of '\0') contains:
  632. { magic_get, magic_set, magic_len, 0, 0 }
  633. Thus, when an SV is determined to be magical and of type '\0', if a get
  634. operation is being performed, the routine C<magic_get> is called. All
  635. the various routines for the various magical types begin with C<magic_>.
  636. NOTE: the magic routines are not considered part of the Perl API, and may
  637. not be exported by the Perl library.
  638. The current kinds of Magic Virtual Tables are:
  639. mg_type MGVTBL Type of magic
  640. ------- ------ ----------------------------
  641. \0 vtbl_sv Special scalar variable
  642. A vtbl_amagic %OVERLOAD hash
  643. a vtbl_amagicelem %OVERLOAD hash element
  644. c (none) Holds overload table (AMT) on stash
  645. B vtbl_bm Boyer-Moore (fast string search)
  646. D vtbl_regdata Regex match position data (@+ and @- vars)
  647. d vtbl_regdatum Regex match position data element
  648. E vtbl_env %ENV hash
  649. e vtbl_envelem %ENV hash element
  650. f vtbl_fm Formline ('compiled' format)
  651. g vtbl_mglob m//g target / study()ed string
  652. I vtbl_isa @ISA array
  653. i vtbl_isaelem @ISA array element
  654. k vtbl_nkeys scalar(keys()) lvalue
  655. L (none) Debugger %_<filename
  656. l vtbl_dbline Debugger %_<filename element
  657. o vtbl_collxfrm Locale transformation
  658. P vtbl_pack Tied array or hash
  659. p vtbl_packelem Tied array or hash element
  660. q vtbl_packelem Tied scalar or handle
  661. S vtbl_sig %SIG hash
  662. s vtbl_sigelem %SIG hash element
  663. t vtbl_taint Taintedness
  664. U vtbl_uvar Available for use by extensions
  665. v vtbl_vec vec() lvalue
  666. x vtbl_substr substr() lvalue
  667. y vtbl_defelem Shadow "foreach" iterator variable /
  668. smart parameter vivification
  669. * vtbl_glob GV (typeglob)
  670. # vtbl_arylen Array length ($#ary)
  671. . vtbl_pos pos() lvalue
  672. ~ (none) Available for use by extensions
  673. When an uppercase and lowercase letter both exist in the table, then the
  674. uppercase letter is used to represent some kind of composite type (a list
  675. or a hash), and the lowercase letter is used to represent an element of
  676. that composite type.
  677. The '~' and 'U' magic types are defined specifically for use by
  678. extensions and will not be used by perl itself. Extensions can use
  679. '~' magic to 'attach' private information to variables (typically
  680. objects). This is especially useful because there is no way for
  681. normal perl code to corrupt this private information (unlike using
  682. extra elements of a hash object).
  683. Similarly, 'U' magic can be used much like tie() to call a C function
  684. any time a scalar's value is used or changed. The C<MAGIC>'s
  685. C<mg_ptr> field points to a C<ufuncs> structure:
  686. struct ufuncs {
  687. I32 (*uf_val)(IV, SV*);
  688. I32 (*uf_set)(IV, SV*);
  689. IV uf_index;
  690. };
  691. When the SV is read from or written to, the C<uf_val> or C<uf_set>
  692. function will be called with C<uf_index> as the first arg and a
  693. pointer to the SV as the second. A simple example of how to add 'U'
  694. magic is shown below. Note that the ufuncs structure is copied by
  695. sv_magic, so you can safely allocate it on the stack.
  696. void
  697. Umagic(sv)
  698. SV *sv;
  699. PREINIT:
  700. struct ufuncs uf;
  701. CODE:
  702. uf.uf_val = &my_get_fn;
  703. uf.uf_set = &my_set_fn;
  704. uf.uf_index = 0;
  705. sv_magic(sv, 0, 'U', (char*)&uf, sizeof(uf));
  706. Note that because multiple extensions may be using '~' or 'U' magic,
  707. it is important for extensions to take extra care to avoid conflict.
  708. Typically only using the magic on objects blessed into the same class
  709. as the extension is sufficient. For '~' magic, it may also be
  710. appropriate to add an I32 'signature' at the top of the private data
  711. area and check that.
  712. Also note that the C<sv_set*()> and C<sv_cat*()> functions described
  713. earlier do B<not> invoke 'set' magic on their targets. This must
  714. be done by the user either by calling the C<SvSETMAGIC()> macro after
  715. calling these functions, or by using one of the C<sv_set*_mg()> or
  716. C<sv_cat*_mg()> functions. Similarly, generic C code must call the
  717. C<SvGETMAGIC()> macro to invoke any 'get' magic if they use an SV
  718. obtained from external sources in functions that don't handle magic.
  719. See L<perlapi> for a description of these functions.
  720. For example, calls to the C<sv_cat*()> functions typically need to be
  721. followed by C<SvSETMAGIC()>, but they don't need a prior C<SvGETMAGIC()>
  722. since their implementation handles 'get' magic.
  723. =head2 Finding Magic
  724. MAGIC* mg_find(SV*, int type); /* Finds the magic pointer of that type */
  725. This routine returns a pointer to the C<MAGIC> structure stored in the SV.
  726. If the SV does not have that magical feature, C<NULL> is returned. Also,
  727. if the SV is not of type SVt_PVMG, Perl may core dump.
  728. int mg_copy(SV* sv, SV* nsv, const char* key, STRLEN klen);
  729. This routine checks to see what types of magic C<sv> has. If the mg_type
  730. field is an uppercase letter, then the mg_obj is copied to C<nsv>, but
  731. the mg_type field is changed to be the lowercase letter.
  732. =head2 Understanding the Magic of Tied Hashes and Arrays
  733. Tied hashes and arrays are magical beasts of the 'P' magic type.
  734. WARNING: As of the 5.004 release, proper usage of the array and hash
  735. access functions requires understanding a few caveats. Some
  736. of these caveats are actually considered bugs in the API, to be fixed
  737. in later releases, and are bracketed with [MAYCHANGE] below. If
  738. you find yourself actually applying such information in this section, be
  739. aware that the behavior may change in the future, umm, without warning.
  740. The perl tie function associates a variable with an object that implements
  741. the various GET, SET etc methods. To perform the equivalent of the perl
  742. tie function from an XSUB, you must mimic this behaviour. The code below
  743. carries out the necessary steps - firstly it creates a new hash, and then
  744. creates a second hash which it blesses into the class which will implement
  745. the tie methods. Lastly it ties the two hashes together, and returns a
  746. reference to the new tied hash. Note that the code below does NOT call the
  747. TIEHASH method in the MyTie class -
  748. see L<Calling Perl Routines from within C Programs> for details on how
  749. to do this.
  750. SV*
  751. mytie()
  752. PREINIT:
  753. HV *hash;
  754. HV *stash;
  755. SV *tie;
  756. CODE:
  757. hash = newHV();
  758. tie = newRV_noinc((SV*)newHV());
  759. stash = gv_stashpv("MyTie", TRUE);
  760. sv_bless(tie, stash);
  761. hv_magic(hash, tie, 'P');
  762. RETVAL = newRV_noinc(hash);
  763. OUTPUT:
  764. RETVAL
  765. The C<av_store> function, when given a tied array argument, merely
  766. copies the magic of the array onto the value to be "stored", using
  767. C<mg_copy>. It may also return NULL, indicating that the value did not
  768. actually need to be stored in the array. [MAYCHANGE] After a call to
  769. C<av_store> on a tied array, the caller will usually need to call
  770. C<mg_set(val)> to actually invoke the perl level "STORE" method on the
  771. TIEARRAY object. If C<av_store> did return NULL, a call to
  772. C<SvREFCNT_dec(val)> will also be usually necessary to avoid a memory
  773. leak. [/MAYCHANGE]
  774. The previous paragraph is applicable verbatim to tied hash access using the
  775. C<hv_store> and C<hv_store_ent> functions as well.
  776. C<av_fetch> and the corresponding hash functions C<hv_fetch> and
  777. C<hv_fetch_ent> actually return an undefined mortal value whose magic
  778. has been initialized using C<mg_copy>. Note the value so returned does not
  779. need to be deallocated, as it is already mortal. [MAYCHANGE] But you will
  780. need to call C<mg_get()> on the returned value in order to actually invoke
  781. the perl level "FETCH" method on the underlying TIE object. Similarly,
  782. you may also call C<mg_set()> on the return value after possibly assigning
  783. a suitable value to it using C<sv_setsv>, which will invoke the "STORE"
  784. method on the TIE object. [/MAYCHANGE]
  785. [MAYCHANGE]
  786. In other words, the array or hash fetch/store functions don't really
  787. fetch and store actual values in the case of tied arrays and hashes. They
  788. merely call C<mg_copy> to attach magic to the values that were meant to be
  789. "stored" or "fetched". Later calls to C<mg_get> and C<mg_set> actually
  790. do the job of invoking the TIE methods on the underlying objects. Thus
  791. the magic mechanism currently implements a kind of lazy access to arrays
  792. and hashes.
  793. Currently (as of perl version 5.004), use of the hash and array access
  794. functions requires the user to be aware of whether they are operating on
  795. "normal" hashes and arrays, or on their tied variants. The API may be
  796. changed to provide more transparent access to both tied and normal data
  797. types in future versions.
  798. [/MAYCHANGE]
  799. You would do well to understand that the TIEARRAY and TIEHASH interfaces
  800. are mere sugar to invoke some perl method calls while using the uniform hash
  801. and array syntax. The use of this sugar imposes some overhead (typically
  802. about two to four extra opcodes per FETCH/STORE operation, in addition to
  803. the creation of all the mortal variables required to invoke the methods).
  804. This overhead will be comparatively small if the TIE methods are themselves
  805. substantial, but if they are only a few statements long, the overhead
  806. will not be insignificant.
  807. =head2 Localizing changes
  808. Perl has a very handy construction
  809. {
  810. local $var = 2;
  811. ...
  812. }
  813. This construction is I<approximately> equivalent to
  814. {
  815. my $oldvar = $var;
  816. $var = 2;
  817. ...
  818. $var = $oldvar;
  819. }
  820. The biggest difference is that the first construction would
  821. reinstate the initial value of $var, irrespective of how control exits
  822. the block: C<goto>, C<return>, C<die>/C<eval> etc. It is a little bit
  823. more efficient as well.
  824. There is a way to achieve a similar task from C via Perl API: create a
  825. I<pseudo-block>, and arrange for some changes to be automatically
  826. undone at the end of it, either explicit, or via a non-local exit (via
  827. die()). A I<block>-like construct is created by a pair of
  828. C<ENTER>/C<LEAVE> macros (see L<perlcall/"Returning a Scalar">).
  829. Such a construct may be created specially for some important localized
  830. task, or an existing one (like boundaries of enclosing Perl
  831. subroutine/block, or an existing pair for freeing TMPs) may be
  832. used. (In the second case the overhead of additional localization must
  833. be almost negligible.) Note that any XSUB is automatically enclosed in
  834. an C<ENTER>/C<LEAVE> pair.
  835. Inside such a I<pseudo-block> the following service is available:
  836. =over 4
  837. =item C<SAVEINT(int i)>
  838. =item C<SAVEIV(IV i)>
  839. =item C<SAVEI32(I32 i)>
  840. =item C<SAVELONG(long i)>
  841. These macros arrange things to restore the value of integer variable
  842. C<i> at the end of enclosing I<pseudo-block>.
  843. =item C<SAVESPTR(s)>
  844. =item C<SAVEPPTR(p)>
  845. These macros arrange things to restore the value of pointers C<s> and
  846. C<p>. C<s> must be a pointer of a type which survives conversion to
  847. C<SV*> and back, C<p> should be able to survive conversion to C<char*>
  848. and back.
  849. =item C<SAVEFREESV(SV *sv)>
  850. The refcount of C<sv> would be decremented at the end of
  851. I<pseudo-block>. This is similar to C<sv_2mortal> in that it is also a
  852. mechanism for doing a delayed C<SvREFCNT_dec>. However, while C<sv_2mortal>
  853. extends the lifetime of C<sv> until the beginning of the next statement,
  854. C<SAVEFREESV> extends it until the end of the enclosing scope. These
  855. lifetimes can be wildly different.
  856. Also compare C<SAVEMORTALIZESV>.
  857. =item C<SAVEMORTALIZESV(SV *sv)>
  858. Just like C<SAVEFREESV>, but mortalizes C<sv> at the end of the current
  859. scope instead of decrementing its reference count. This usually has the
  860. effect of keeping C<sv> alive until the statement that called the currently
  861. live scope has finished executing.
  862. =item C<SAVEFREEOP(OP *op)>
  863. The C<OP *> is op_free()ed at the end of I<pseudo-block>.
  864. =item C<SAVEFREEPV(p)>
  865. The chunk of memory which is pointed to by C<p> is Safefree()ed at the
  866. end of I<pseudo-block>.
  867. =item C<SAVECLEARSV(SV *sv)>
  868. Clears a slot in the current scratchpad which corresponds to C<sv> at
  869. the end of I<pseudo-block>.
  870. =item C<SAVEDELETE(HV *hv, char *key, I32 length)>
  871. The key C<key> of C<hv> is deleted at the end of I<pseudo-block>. The
  872. string pointed to by C<key> is Safefree()ed. If one has a I<key> in
  873. short-lived storage, the corresponding string may be reallocated like
  874. this:
  875. SAVEDELETE(PL_defstash, savepv(tmpbuf), strlen(tmpbuf));
  876. =item C<SAVEDESTRUCTOR(DESTRUCTORFUNC_NOCONTEXT_t f, void *p)>
  877. At the end of I<pseudo-block> the function C<f> is called with the
  878. only argument C<p>.
  879. =item C<SAVEDESTRUCTOR_X(DESTRUCTORFUNC_t f, void *p)>
  880. At the end of I<pseudo-block> the function C<f> is called with the
  881. implicit context argument (if any), and C<p>.
  882. =item C<SAVESTACK_POS()>
  883. The current offset on the Perl internal stack (cf. C<SP>) is restored
  884. at the end of I<pseudo-block>.
  885. =back
  886. The following API list contains functions, thus one needs to
  887. provide pointers to the modifiable data explicitly (either C pointers,
  888. or Perlish C<GV *>s). Where the above macros take C<int>, a similar
  889. function takes C<int *>.
  890. =over 4
  891. =item C<SV* save_scalar(GV *gv)>
  892. Equivalent to Perl code C<local $gv>.
  893. =item C<AV* save_ary(GV *gv)>
  894. =item C<HV* save_hash(GV *gv)>
  895. Similar to C<save_scalar>, but localize C<@gv> and C<%gv>.
  896. =item C<void save_item(SV *item)>
  897. Duplicates the current value of C<SV>, on the exit from the current
  898. C<ENTER>/C<LEAVE> I<pseudo-block> will restore the value of C<SV>
  899. using the stored value.
  900. =item C<void save_list(SV **sarg, I32 maxsarg)>
  901. A variant of C<save_item> which takes multiple arguments via an array
  902. C<sarg> of C<SV*> of length C<maxsarg>.
  903. =item C<SV* save_svref(SV **sptr)>
  904. Similar to C<save_scalar>, but will reinstate a C<SV *>.
  905. =item C<void save_aptr(AV **aptr)>
  906. =item C<void save_hptr(HV **hptr)>
  907. Similar to C<save_svref>, but localize C<AV *> and C<HV *>.
  908. =back
  909. The C<Alias> module implements localization of the basic types within the
  910. I<caller's scope>. People who are interested in how to localize things in
  911. the containing scope should take a look there too.
  912. =head1 Subroutines
  913. =head2 XSUBs and the Argument Stack
  914. The XSUB mechanism is a simple way for Perl programs to access C subroutines.
  915. An XSUB routine will have a stack that contains the arguments from the Perl
  916. program, and a way to map from the Perl data structures to a C equivalent.
  917. The stack arguments are accessible through the C<ST(n)> macro, which returns
  918. the C<n>'th stack argument. Argument 0 is the first argument passed in the
  919. Perl subroutine call. These arguments are C<SV*>, and can be used anywhere
  920. an C<SV*> is used.
  921. Most of the time, output from the C routine can be handled through use of
  922. the RETVAL and OUTPUT directives. However, there are some cases where the
  923. argument stack is not already long enough to handle all the return values.
  924. An example is the POSIX tzname() call, which takes no arguments, but returns
  925. two, the local time zone's standard and summer time abbreviations.
  926. To handle this

Large files files are truncated, but you can click here to view the full file