3 perlguts - Perl's Internal Functions
7 This document attempts to describe some of the internal functions of the
8 Perl executable. It is far from complete and probably contains many errors.
9 Please refer any questions or comments to the author below.
15 Perl has three typedefs that handle Perl's three main data types:
21 Each typedef has specific routines that manipulate the various data types.
23 =head2 What is an "IV"?
25 Perl uses a special typedef IV which is a simple integer type that is
26 guaranteed to be large enough to hold a pointer (as well as an integer).
28 Perl also uses two special typedefs, I32 and I16, which will always be at
29 least 32-bits and 16-bits long, respectively.
31 =head2 Working with SV's
33 An SV can be created and loaded with one command. There are four types of
34 values that can be loaded: an integer value (IV), a double (NV), a string,
35 (PV), and another scalar (SV).
37 The four routines are:
41 SV* newSVpv(char*, int);
44 To change the value of an *already-existing* SV, there are five routines:
46 void sv_setiv(SV*, IV);
47 void sv_setnv(SV*, double);
48 void sv_setpvn(SV*, char*, int)
49 void sv_setpv(SV*, char*);
50 void sv_setsv(SV*, SV*);
52 Notice that you can choose to specify the length of the string to be
53 assigned by using C<sv_setpvn> or C<newSVpv>, or you may allow Perl to
54 calculate the length by using C<sv_setpv> or by specifying 0 as the second
55 argument to C<newSVpv>. Be warned, though, that Perl will determine the
56 string's length by using C<strlen>, which depends on the string terminating
59 All SV's that will contain strings should, but need not, be terminated
60 with a NUL character. If it is not NUL-terminated there is a risk of
61 core dumps and corruptions from code which passes the string to C
62 functions or system calls which expect a NUL-terminated string.
63 Perl's own functions typically add a trailing NUL for this reason.
64 Nevertheless, you should be very careful when you pass a string stored
65 in an SV to a C function or system call.
67 To access the actual value that an SV points to, you can use the macros:
73 which will automatically coerce the actual scalar type into an IV, double,
76 In the C<SvPV> macro, the length of the string returned is placed into the
77 variable C<len> (this is a macro, so you do I<not> use C<&len>). If you do not
78 care what the length of the data is, use the global variable C<na>. Remember,
79 however, that Perl allows arbitrary strings of data that may both contain
80 NUL's and might not be terminated by a NUL.
82 If you want to know if the scalar value is TRUE, you can use:
86 Although Perl will automatically grow strings for you, if you need to force
87 Perl to allocate more memory for your SV, you can use the macro
89 SvGROW(SV*, STRLEN newlen)
91 which will determine if more memory needs to be allocated. If so, it will
92 call the function C<sv_grow>. Note that C<SvGROW> can only increase, not
93 decrease, the allocated memory of an SV and that it does not automatically
94 add a byte for the a trailing NUL (perl's own string functions typically do
95 C<SvGROW(sv, len + 1)>).
97 If you have an SV and want to know what kind of data Perl thinks is stored
98 in it, you can use the following macros to check the type of SV you have.
104 You can get and set the current length of the string stored in an SV with
105 the following macros:
108 SvCUR_set(SV*, I32 val)
110 You can also get a pointer to the end of the string stored in the SV
115 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:
120 void sv_catpv(SV*, char*);
121 void sv_catpvn(SV*, char*, int);
122 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 extends the string stored in the first SV
127 with the string stored in the second SV. It also forces the second SV to
128 be interpreted as a string.
130 If you know the name of a scalar variable, you can get a pointer to its SV
131 by using the following:
133 SV* perl_get_sv("package::varname", FALSE);
135 This returns NULL if the variable does not exist.
137 If you want to know if this variable (or any other SV) is actually C<defined>,
142 The scalar C<undef> value is stored in an SV instance called C<sv_undef>. Its
143 address can be used whenever an C<SV*> is needed.
145 There are also the two values C<sv_yes> and C<sv_no>, which contain Boolean
146 TRUE and FALSE values, respectively. Like C<sv_undef>, their addresses can
147 be used whenever an C<SV*> is needed.
149 Do not be fooled into thinking that C<(SV *) 0> is the same as C<&sv_undef>.
153 if (I-am-to-return-a-real-value) {
154 sv = sv_2mortal(newSViv(42));
158 This code tries to return a new SV (which contains the value 42) if it should
159 return a real value, or undef otherwise. Instead it has returned a null
160 pointer which, somewhere down the line, will cause a segmentation violation,
161 bus error, or just weird results. Change the zero to C<&sv_undef> in the first
162 line and all will be well.
164 To free an SV that you've created, call C<SvREFCNT_dec(SV*)>. Normally this
165 call is not necessary (see the section on L<Mortality>).
167 =head2 What's Really Stored in an SV?
169 Recall that the usual method of determining the type of scalar you have is
170 to use C<Sv*OK> macros. Because a scalar can be both a number and a string,
171 usually these macros will always return TRUE and calling the C<Sv*V>
172 macros will do the appropriate conversion of string to integer/double or
173 integer/double to string.
175 If you I<really> need to know if you have an integer, double, or string
176 pointer in an SV, you can use the following three macros instead:
182 These will tell you if you truly have an integer, double, or string pointer
183 stored in your SV. The "p" stands for private.
185 In general, though, it's best to use the C<Sv*V> macros.
187 =head2 Working with AV's
189 There are two ways to create and load an AV. The first method creates an
194 The second method both creates the AV and initially populates it with SV's:
196 AV* av_make(I32 num, SV **ptr);
198 The second argument points to an array containing C<num> C<SV*>'s. Once the
199 AV has been created, the SV's can be destroyed, if so desired.
201 Once the AV has been created, the following operations are possible on AV's:
203 void av_push(AV*, SV*);
206 void av_unshift(AV*, I32 num);
208 These should be familiar operations, with the exception of C<av_unshift>.
209 This routine adds C<num> elements at the front of the array with the C<undef>
210 value. You must then use C<av_store> (described below) to assign values
211 to these new elements.
213 Here are some other functions:
216 SV** av_fetch(AV*, I32 key, I32 lval);
217 SV** av_store(AV*, I32 key, SV* val);
219 The C<av_len> function returns the highest index value in array (just
220 like $#array in Perl). If the array is empty, -1 is returned. The
221 C<av_fetch> function returns the value at index C<key>, but if C<lval>
222 is non-zero, then C<av_fetch> will store an undef value at that index.
223 The C<av_store> function stores the value C<val> at index C<key>.
224 note that C<av_fetch> and C<av_store> both return C<SV**>'s, not C<SV*>'s
225 as their return value.
229 void av_extend(AV*, I32 key);
231 The C<av_clear> function deletes all the elements in the AV* array, but
232 does not actually delete the array itself. The C<av_undef> function will
233 delete all the elements in the array plus the array itself. The
234 C<av_extend> function extends the array so that it contains C<key>
235 elements. If C<key> is less than the current length of the array, then
238 If you know the name of an array variable, you can get a pointer to its AV
239 by using the following:
241 AV* perl_get_av("package::varname", FALSE);
243 This returns NULL if the variable does not exist.
245 =head2 Working with HV's
247 To create an HV, you use the following routine:
251 Once the HV has been created, the following operations are possible on HV's:
253 SV** hv_store(HV*, char* key, U32 klen, SV* val, U32 hash);
254 SV** hv_fetch(HV*, char* key, U32 klen, I32 lval);
256 The C<klen> parameter is the length of the key being passed in (Note that
257 you cannot pass 0 in as a value of C<klen> to tell Perl to measure the
258 length of the key). The C<val> argument contains the SV pointer to the
259 scalar being stored, and C<hash> is the pre-computed hash value (zero if
260 you want C<hv_store> to calculate it for you). The C<lval> parameter
261 indicates whether this fetch is actually a part of a store operation, in
262 which case a new undefined value will be added to the HV with the supplied
263 key and C<hv_fetch> will return as if the value had already existed.
265 Remember that C<hv_store> and C<hv_fetch> return C<SV**>'s and not just
266 C<SV*>. To access the scalar value, you must first dereference the return
267 value. However, you should check to make sure that the return value is
268 not NULL before dereferencing it.
270 These two functions check if a hash table entry exists, and deletes it.
272 bool hv_exists(HV*, char* key, U32 klen);
273 SV* hv_delete(HV*, char* key, U32 klen, I32 flags);
275 If C<flags> does not include the C<G_DISCARD> flag then C<hv_delete> will
276 create and return a mortal copy of the deleted value.
278 And more miscellaneous functions:
283 Like their AV counterparts, C<hv_clear> deletes all the entries in the hash
284 table but does not actually delete the hash table. The C<hv_undef> deletes
285 both the entries and the hash table itself.
287 Perl keeps the actual data in linked list of structures with a typedef of HE.
288 These contain the actual key and value pointers (plus extra administrative
289 overhead). The key is a string pointer; the value is an C<SV*>. However,
290 once you have an C<HE*>, to get the actual key and value, use the routines
293 I32 hv_iterinit(HV*);
294 /* Prepares starting point to traverse hash table */
295 HE* hv_iternext(HV*);
296 /* Get the next entry, and return a pointer to a
297 structure that has both the key and value */
298 char* hv_iterkey(HE* entry, I32* retlen);
299 /* Get the key from an HE structure and also return
300 the length of the key string */
301 SV* hv_iterval(HV*, HE* entry);
302 /* Return a SV pointer to the value of the HE
304 SV* hv_iternextsv(HV*, char** key, I32* retlen);
305 /* This convenience routine combines hv_iternext,
306 hv_iterkey, and hv_iterval. The key and retlen
307 arguments are return values for the key and its
308 length. The value is returned in the SV* argument */
310 If you know the name of a hash variable, you can get a pointer to its HV
311 by using the following:
313 HV* perl_get_hv("package::varname", FALSE);
315 This returns NULL if the variable does not exist.
317 The hash algorithm is defined in the C<PERL_HASH(hash, key, klen)> macro:
323 hash = hash * 33 + *s++;
327 References are a special type of scalar that point to other data types
328 (including references).
330 To create a reference, use either of the following functions:
332 SV* newRV_inc((SV*) thing);
333 SV* newRV_noinc((SV*) thing);
335 The C<thing> argument can be any of an C<SV*>, C<AV*>, or C<HV*>. The
336 functions are identical except that C<newRV_inc> increments the reference
337 count of the C<thing>, while C<newRV_noinc> does not. For historical
338 reasons, C<newRV> is a synonym for C<newRV_inc>.
340 Once you have a reference, you can use the following macro to dereference
345 then call the appropriate routines, casting the returned C<SV*> to either an
346 C<AV*> or C<HV*>, if required.
348 To determine if an SV is a reference, you can use the following macro:
352 To discover what type of value the reference refers to, use the following
353 macro and then check the return value.
357 The most useful types that will be returned are:
366 SVt_PVGV Glob (possible a file handle)
367 SVt_PVMG Blessed or Magical Scalar
369 See the sv.h header file for more details.
371 =head2 Blessed References and Class Objects
373 References are also used to support object-oriented programming. In the
374 OO lexicon, an object is simply a reference that has been blessed into a
375 package (or class). Once blessed, the programmer may now use the reference
376 to access the various methods in the class.
378 A reference can be blessed into a package with the following function:
380 SV* sv_bless(SV* sv, HV* stash);
382 The C<sv> argument must be a reference. The C<stash> argument specifies
383 which class the reference will belong to. See the section on L<Stashes>
384 for information on converting class names into stashes.
386 /* Still under construction */
388 Upgrades rv to reference if not already one. Creates new SV for rv to
389 point to. If C<classname> is non-null, the SV is blessed into the specified
390 class. SV is returned.
392 SV* newSVrv(SV* rv, char* classname);
394 Copies integer or double into an SV whose reference is C<rv>. SV is blessed
395 if C<classname> is non-null.
397 SV* sv_setref_iv(SV* rv, char* classname, IV iv);
398 SV* sv_setref_nv(SV* rv, char* classname, NV iv);
400 Copies the pointer value (I<the address, not the string!>) into an SV whose
401 reference is rv. SV is blessed if C<classname> is non-null.
403 SV* sv_setref_pv(SV* rv, char* classname, PV iv);
405 Copies string into an SV whose reference is C<rv>. Set length to 0 to let
406 Perl calculate the string length. SV is blessed if C<classname> is non-null.
408 SV* sv_setref_pvn(SV* rv, char* classname, PV iv, int length);
410 int sv_isa(SV* sv, char* name);
411 int sv_isobject(SV* sv);
413 =head2 Creating New Variables
415 To create a new Perl variable with an undef value which can be accessed from
416 your Perl script, use the following routines, depending on the variable type.
418 SV* perl_get_sv("package::varname", TRUE);
419 AV* perl_get_av("package::varname", TRUE);
420 HV* perl_get_hv("package::varname", TRUE);
422 Notice the use of TRUE as the second parameter. The new variable can now
423 be set, using the routines appropriate to the data type.
425 There are additional macros whose values may be bitwise OR'ed with the
426 C<TRUE> argument to enable certain extra features. Those bits are:
428 GV_ADDMULTI Marks the variable as multiply defined, thus preventing the
429 "Indentifier <varname> used only once: possible typo" warning.
430 GV_ADDWARN Issues the warning "Had to create <varname> unexpectedly" if
431 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
436 =head2 Reference Counts and Mortality
438 Perl uses an reference count-driven garbage collection mechanism. SV's,
439 AV's, or HV's (xV for short in the following) start their life with a
440 reference count of 1. If the reference count of an xV ever drops to 0,
441 then it will be destroyed and its memory made available for reuse.
443 This normally doesn't happen at the Perl level unless a variable is
444 undef'ed or the last variable holding a reference to it is changed or
445 overwritten. At the internal level, however, reference counts can be
446 manipulated with the following macros:
448 int SvREFCNT(SV* sv);
449 SV* SvREFCNT_inc(SV* sv);
450 void SvREFCNT_dec(SV* sv);
452 However, there is one other function which manipulates the reference
453 count of its argument. The C<newRV_inc> function, you will recall,
454 creates a reference to the specified argument. As a side effect,
455 it increments the argument's reference count. If this is not what
456 you want, use C<newRV_noinc> instead.
458 For example, imagine you want to return a reference from an XSUB function.
459 Inside the XSUB routine, you create an SV which initially has a reference
460 count of one. Then you call C<newRV_inc>, passing it the just-created SV.
461 This returns the reference as a new SV, but the reference count of the
462 SV you passed to C<newRV_inc> has been incremented to two. Now you
463 return the reference from the XSUB routine and forget about the SV.
464 But Perl hasn't! Whenever the returned reference is destroyed, the
465 reference count of the original SV is decreased to one and nothing happens.
466 The SV will hang around without any way to access it until Perl itself
467 terminates. This is a memory leak.
469 The correct procedure, then, is to use C<newRV_noinc> instead of
470 C<newRV_inc>. Then, if and when the last reference is destroyed,
471 the reference count of the SV will go to zero and it will be destroyed,
472 stopping any memory leak.
474 There are some convenience functions available that can help with the
475 destruction of xV's. These functions introduce the concept of "mortality".
476 An xV that is mortal has had its reference count marked to be decremented,
477 but not actually decremented, until "a short time later". Generally the
478 term "short time later" means a single Perl statement, such as a call to
479 an XSUB function. The actual determinant for when mortal xV's have their
480 reference count decremented depends on two macros, SAVETMPS and FREETMPS.
481 See L<perlcall> and L<perlxs> for more details on these macros.
483 "Mortalization" then is at its simplest a deferred C<SvREFCNT_dec>.
484 However, if you mortalize a variable twice, the reference count will
485 later be decremented twice.
487 You should be careful about creating mortal variables. Strange things
488 can happen if you make the same value mortal within multiple contexts,
489 or if you make a variable mortal multiple times.
491 To create a mortal variable, use the functions:
495 SV* sv_mortalcopy(SV*)
497 The first call creates a mortal SV, the second converts an existing
498 SV to a mortal SV (and thus defers a call to C<SvREFCNT_dec>), and the
499 third creates a mortal copy of an existing SV.
501 The mortal routines are not just for SV's -- AV's and HV's can be
502 made mortal by passing their address (type-casted to C<SV*>) to the
503 C<sv_2mortal> or C<sv_mortalcopy> routines.
505 =head2 Stashes and Globs
507 A "stash" is a hash that contains all of the different objects that
508 are contained within a package. Each key of the stash is a symbol
509 name (shared by all the different types of objects that have the same
510 name), and each value in the hash table is a GV (Glob Value). This GV
511 in turn contains references to the various objects of that name,
512 including (but not limited to) the following:
522 There is a single stash called "defstash" that holds the items that exist
523 in the "main" package. To get at the items in other packages, append the
524 string "::" to the package name. The items in the "Foo" package are in
525 the stash "Foo::" in defstash. The items in the "Bar::Baz" package are
526 in the stash "Baz::" in "Bar::"'s stash.
528 To get the stash pointer for a particular package, use the function:
530 HV* gv_stashpv(char* name, I32 create)
531 HV* gv_stashsv(SV*, I32 create)
533 The first function takes a literal string, the second uses the string stored
534 in the SV. Remember that a stash is just a hash table, so you get back an
535 C<HV*>. The C<create> flag will create a new package if it is set.
537 The name that C<gv_stash*v> wants is the name of the package whose symbol table
538 you want. The default package is called C<main>. If you have multiply nested
539 packages, pass their names to C<gv_stash*v>, separated by C<::> as in the Perl
542 Alternately, if you have an SV that is a blessed reference, you can find
543 out the stash pointer by using:
545 HV* SvSTASH(SvRV(SV*));
547 then use the following to get the package name itself:
549 char* HvNAME(HV* stash);
551 If you need to bless or re-bless an object you can use the following
554 SV* sv_bless(SV*, HV* stash)
556 where the first argument, an C<SV*>, must be a reference, and the second
557 argument is a stash. The returned C<SV*> can now be used in the same way
560 For more information on references and blessings, consult L<perlref>.
562 =head2 Double-Typed SV's
564 Scalar variables normally contain only one type of value, an integer,
565 double, pointer, or reference. Perl will automatically convert the
566 actual scalar data from the stored type into the requested type.
568 Some scalar variables contain more than one type of scalar data. For
569 example, the variable C<$!> contains either the numeric value of C<errno>
570 or its string equivalent from either C<strerror> or C<sys_errlist[]>.
572 To force multiple data values into an SV, you must do two things: use the
573 C<sv_set*v> routines to add the additional scalar type, then set a flag
574 so that Perl will believe it contains more than one type of data. The
575 four macros to set the flags are:
582 The particular macro you must use depends on which C<sv_set*v> routine
583 you called first. This is because every C<sv_set*v> routine turns on
584 only the bit for the particular type of data being set, and turns off
587 For example, to create a new Perl variable called "dberror" that contains
588 both the numeric and descriptive string error values, you could use the
592 extern char *dberror_list;
594 SV* sv = perl_get_sv("dberror", TRUE);
595 sv_setiv(sv, (IV) dberror);
596 sv_setpv(sv, dberror_list[dberror]);
599 If the order of C<sv_setiv> and C<sv_setpv> had been reversed, then the
600 macro C<SvPOK_on> would need to be called instead of C<SvIOK_on>.
602 =head2 Magic Variables
604 [This section still under construction. Ignore everything here. Post no
605 bills. Everything not permitted is forbidden.]
607 Any SV may be magical, that is, it has special features that a normal
608 SV does not have. These features are stored in the SV structure in a
609 linked list of C<struct magic>'s, typedef'ed to C<MAGIC>.
622 Note this is current as of patchlevel 0, and could change at any time.
624 =head2 Assigning Magic
626 Perl adds magic to an SV using the sv_magic function:
628 void sv_magic(SV* sv, SV* obj, int how, char* name, I32 namlen);
630 The C<sv> argument is a pointer to the SV that is to acquire a new magical
633 If C<sv> is not already magical, Perl uses the C<SvUPGRADE> macro to
634 set the C<SVt_PVMG> flag for the C<sv>. Perl then continues by adding
635 it to the beginning of the linked list of magical features. Any prior
636 entry of the same type of magic is deleted. Note that this can be
637 overridden, and multiple instances of the same type of magic can be
638 associated with an SV.
640 The C<name> and C<namlem> arguments are used to associate a string with
641 the magic, typically the name of a variable. C<namlem> is stored in the
642 C<mg_len> field and if C<name> is non-null and C<namlem> >= 0 a malloc'd
643 copy of the name is stored in C<mg_ptr> field.
645 The sv_magic function uses C<how> to determine which, if any, predefined
646 "Magic Virtual Table" should be assigned to the C<mg_virtual> field.
647 See the "Magic Virtual Table" section below. The C<how> argument is also
648 stored in the C<mg_type> field.
650 The C<obj> argument is stored in the C<mg_obj> field of the C<MAGIC>
651 structure. If it is not the same as the C<sv> argument, the reference
652 count of the C<obj> object is incremented. If it is the same, or if
653 the C<how> argument is "#", or if it is a null pointer, then C<obj> is
654 merely stored, without the reference count being incremented.
656 There is also a function to add magic to an C<HV>:
658 void hv_magic(HV *hv, GV *gv, int how);
660 This simply calls C<sv_magic> and coerces the C<gv> argument into an C<SV>.
662 To remove the magic from an SV, call the function sv_unmagic:
664 void sv_unmagic(SV *sv, int type);
666 The C<type> argument should be equal to the C<how> value when the C<SV>
667 was initially made magical.
669 =head2 Magic Virtual Tables
671 The C<mg_virtual> field in the C<MAGIC> structure is a pointer to a
672 C<MGVTBL>, which is a structure of function pointers and stands for
673 "Magic Virtual Table" to handle the various operations that might be
674 applied to that variable.
676 The C<MGVTBL> has five pointers to the following routine types:
678 int (*svt_get)(SV* sv, MAGIC* mg);
679 int (*svt_set)(SV* sv, MAGIC* mg);
680 U32 (*svt_len)(SV* sv, MAGIC* mg);
681 int (*svt_clear)(SV* sv, MAGIC* mg);
682 int (*svt_free)(SV* sv, MAGIC* mg);
684 This MGVTBL structure is set at compile-time in C<perl.h> and there are
685 currently 19 types (or 21 with overloading turned on). These different
686 structures contain pointers to various routines that perform additional
687 actions depending on which function is being called.
689 Function pointer Action taken
690 ---------------- ------------
691 svt_get Do something after the value of the SV is retrieved.
692 svt_set Do something after the SV is assigned a value.
693 svt_len Report on the SV's length.
694 svt_clear Clear something the SV represents.
695 svt_free Free any extra storage associated with the SV.
697 For instance, the MGVTBL structure called C<vtbl_sv> (which corresponds
698 to an C<mg_type> of '\0') contains:
700 { magic_get, magic_set, magic_len, 0, 0 }
702 Thus, when an SV is determined to be magical and of type '\0', if a get
703 operation is being performed, the routine C<magic_get> is called. All
704 the various routines for the various magical types begin with C<magic_>.
706 The current kinds of Magic Virtual Tables are:
708 mg_type MGVTBL Type of magical
709 ------- ------ ----------------------------
711 A vtbl_amagic Operator Overloading
712 a vtbl_amagicelem Operator Overloading
713 c 0 Used in Operator Overloading
714 B vtbl_bm Boyer-Moore???
716 e vtbl_envelem %ENV hash element
717 g vtbl_mglob Regexp /g flag???
718 I vtbl_isa @ISA array
719 i vtbl_isaelem @ISA array element
720 L 0 (but sets RMAGICAL) Perl Module/Debugger???
721 l vtbl_dbline Debugger?
722 o vtbl_collxfrm Locale transformation
723 P vtbl_pack Tied Array or Hash
724 p vtbl_packelem Tied Array or Hash element
725 q vtbl_packelem Tied Scalar or Handle
726 S vtbl_sig Signal Hash
727 s vtbl_sigelem Signal Hash element
728 t vtbl_taint Taintedness
731 x vtbl_substr Substring???
732 y vtbl_itervar Shadow "foreach" iterator variable
734 # vtbl_arylen Array Length
735 . vtbl_pos $. scalar variable
736 ~ None Used by certain extensions
738 When an upper-case and lower-case letter both exist in the table, then the
739 upper-case letter is used to represent some kind of composite type (a list
740 or a hash), and the lower-case letter is used to represent an element of
743 The '~' magic type is defined specifically for use by extensions and
744 will not be used by perl itself. Extensions can use ~ magic to 'attach'
745 private information to variables (typically objects). This is especially
746 useful because there is no way for normal perl code to corrupt this
747 private information (unlike using extra elements of a hash object).
749 Note that because multiple extensions may be using ~ magic it is
750 important for extensions to take extra care with it. Typically only
751 using it on objects blessed into the same class as the extension
752 is sufficient. It may also be appropriate to add an I32 'signature'
753 at the top of the private data area and check that.
757 MAGIC* mg_find(SV*, int type); /* Finds the magic pointer of that type */
759 This routine returns a pointer to the C<MAGIC> structure stored in the SV.
760 If the SV does not have that magical feature, C<NULL> is returned. Also,
761 if the SV is not of type SVt_PVMG, Perl may core-dump.
763 int mg_copy(SV* sv, SV* nsv, char* key, STRLEN klen);
765 This routine checks to see what types of magic C<sv> has. If the mg_type
766 field is an upper-case letter, then the mg_obj is copied to C<nsv>, but
767 the mg_type field is changed to be the lower-case letter.
771 =head2 XSUB's and the Argument Stack
773 The XSUB mechanism is a simple way for Perl programs to access C subroutines.
774 An XSUB routine will have a stack that contains the arguments from the Perl
775 program, and a way to map from the Perl data structures to a C equivalent.
777 The stack arguments are accessible through the C<ST(n)> macro, which returns
778 the C<n>'th stack argument. Argument 0 is the first argument passed in the
779 Perl subroutine call. These arguments are C<SV*>, and can be used anywhere
782 Most of the time, output from the C routine can be handled through use of
783 the RETVAL and OUTPUT directives. However, there are some cases where the
784 argument stack is not already long enough to handle all the return values.
785 An example is the POSIX tzname() call, which takes no arguments, but returns
786 two, the local time zone's standard and summer time abbreviations.
788 To handle this situation, the PPCODE directive is used and the stack is
789 extended using the macro:
793 where C<sp> is the stack pointer, and C<num> is the number of elements the
794 stack should be extended by.
796 Now that there is room on the stack, values can be pushed on it using the
797 macros to push IV's, doubles, strings, and SV pointers respectively:
804 And now the Perl program calling C<tzname>, the two values will be assigned
807 ($standard_abbrev, $summer_abbrev) = POSIX::tzname;
809 An alternate (and possibly simpler) method to pushing values on the stack is
817 These macros automatically adjust the stack for you, if needed. Thus, you
818 do not need to call C<EXTEND> to extend the stack.
820 For more information, consult L<perlxs> and L<perlxstut>.
822 =head2 Calling Perl Routines from within C Programs
824 There are four routines that can be used to call a Perl subroutine from
825 within a C program. These four are:
827 I32 perl_call_sv(SV*, I32);
828 I32 perl_call_pv(char*, I32);
829 I32 perl_call_method(char*, I32);
830 I32 perl_call_argv(char*, I32, register char**);
832 The routine most often used is C<perl_call_sv>. The C<SV*> argument
833 contains either the name of the Perl subroutine to be called, or a
834 reference to the subroutine. The second argument consists of flags
835 that control the context in which the subroutine is called, whether
836 or not the subroutine is being passed arguments, how errors should be
837 trapped, and how to treat return values.
839 All four routines return the number of arguments that the subroutine returned
842 When using any of these routines (except C<perl_call_argv>), the programmer
843 must manipulate the Perl stack. These include the following macros and
857 For a detailed description of calling conventions from C to Perl,
860 =head2 Memory Allocation
862 It is suggested that you use the version of malloc that is distributed
863 with Perl. It keeps pools of various sizes of unallocated memory in
864 order to satisfy allocation requests more quickly. However, on some
865 platforms, it may cause spurious malloc or free errors.
867 New(x, pointer, number, type);
868 Newc(x, pointer, number, type, cast);
869 Newz(x, pointer, number, type);
871 These three macros are used to initially allocate memory.
873 The first argument C<x> was a "magic cookie" that was used to keep track
874 of who called the macro, to help when debugging memory problems. However,
875 the current code makes no use of this feature (most Perl developers now
876 use run-time memory checkers), so this argument can be any number.
878 The second argument C<pointer> should be the name of a variable that will
879 point to the newly allocated memory.
881 The third and fourth arguments C<number> and C<type> specify how many of
882 the specified type of data structure should be allocated. The argument
883 C<type> is passed to C<sizeof>. The final argument to C<Newc>, C<cast>,
884 should be used if the C<pointer> argument is different from the C<type>
887 Unlike the C<New> and C<Newc> macros, the C<Newz> macro calls C<memzero>
888 to zero out all the newly allocated memory.
890 Renew(pointer, number, type);
891 Renewc(pointer, number, type, cast);
894 These three macros are used to change a memory buffer size or to free a
895 piece of memory no longer needed. The arguments to C<Renew> and C<Renewc>
896 match those of C<New> and C<Newc> with the exception of not needing the
897 "magic cookie" argument.
899 Move(source, dest, number, type);
900 Copy(source, dest, number, type);
901 Zero(dest, number, type);
903 These three macros are used to move, copy, or zero out previously allocated
904 memory. The C<source> and C<dest> arguments point to the source and
905 destination starting points. Perl will move, copy, or zero out C<number>
906 instances of the size of the C<type> data structure (using the C<sizeof>
911 The most recent development releases of Perl has been experimenting with
912 removing Perl's dependency on the "normal" standard I/O suite and allowing
913 other stdio implementations to be used. This involves creating a new
914 abstraction layer that then calls whichever implementation of stdio Perl
915 was compiled with. All XSUB's should now use the functions in the PerlIO
916 abstraction layer and not make any assumptions about what kind of stdio
919 For a complete description of the PerlIO abstraction, consult L<perlapio>.
921 =head2 Putting a C value on Perl stack
923 A lot of opcodes (this is an elementary operation in the internal perl
924 stack machine) put an SV* on the stack. However, as an optimization
925 the corresponding SV is (usually) not recreated each time. The opcodes
926 reuse specially assigned SVs (I<target>s) which are (as a corollary)
927 not constantly freed/created.
929 Each of the targets is created only once (but see
930 L<Scratchpads and recursion> below), and when an opcode needs to put
931 an integer, a double, or a string on stack, it just sets the
932 corresponding parts of its I<target> and puts the I<target> on stack.
934 The macro to put this target on stack is C<PUSHTARG>, and it is
935 directly used in some opcodes, as well as indirectly in zillions of
936 others, which use it via C<(X)PUSH[pni]>.
940 The question remains on when the SV's which are I<target>s for opcodes
941 are created. The answer is that they are created when the current unit --
942 a subroutine or a file (for opcodes for statements outside of
943 subroutines) -- is compiled. During this time a special anonymous Perl
944 array is created, which is called a scratchpad for the current
947 A scratchpad keeps SV's which are lexicals for the current unit and are
948 targets for opcodes. One can deduce that an SV lives on a scratchpad
949 by looking on its flags: lexicals have C<SVs_PADMY> set, and
950 I<target>s have C<SVs_PADTMP> set.
952 The correspondence between OP's and I<target>s is not 1-to-1. Different
953 OP's in the compile tree of the unit can use the same target, if this
954 would not conflict with the expected life of the temporary.
956 =head2 Scratchpads and recursions
958 In fact it is not 100% true that a compiled unit contains a pointer to
959 the scratchpad AV. In fact it contains a pointer to an AV of
960 (initially) one element, and this element is the scratchpad AV. Why do
961 we need an extra level of indirection?
963 The answer is B<recursion>, and maybe (sometime soon) B<threads>. Both
964 these can create several execution pointers going into the same
965 subroutine. For the subroutine-child not write over the temporaries
966 for the subroutine-parent (lifespan of which covers the call to the
967 child), the parent and the child should have different
968 scratchpads. (I<And> the lexicals should be separate anyway!)
970 So each subroutine is born with an array of scratchpads (of length 1).
971 On each entry to the subroutine it is checked that the current
972 depth of the recursion is not more than the length of this array, and
973 if it is, new scratchpad is created and pushed into the array.
975 The I<target>s on this scratchpad are C<undef>s, but they are already
976 marked with correct flags.
982 Here we describe the internal form your code is converted to by
983 Perl. Start with a simple example:
987 This is converted to a tree similar to this one:
995 (but slightly more complicated). This tree reflect the way Perl
996 parsed your code, but has nothing to do with the execution order.
997 There is an additional "thread" going through the nodes of the tree
998 which shows the order of execution of the nodes. In our simplified
999 example above it looks like:
1001 $b ---> $c ---> + ---> $a ---> assign-to
1003 But with the actual compile tree for C<$a = $b + $c> it is different:
1004 some nodes I<optimized away>. As a corollary, though the actual tree
1005 contains more nodes than our simplified example, the execution order
1006 is the same as in our example.
1008 =head2 Examining the tree
1010 If you have your perl compiled for debugging (usually done with C<-D
1011 optimize=-g> on C<Configure> command line), you may examine the
1012 compiled tree by specifying C<-Dx> on the Perl command line. The
1013 output takes several lines per node, and for C<$b+$c> it looks like
1018 FLAGS = (SCALAR,KIDS)
1020 TYPE = null ===> (4)
1022 FLAGS = (SCALAR,KIDS)
1024 3 TYPE = gvsv ===> 4
1030 TYPE = null ===> (5)
1032 FLAGS = (SCALAR,KIDS)
1034 4 TYPE = gvsv ===> 5
1040 This tree has 5 nodes (one per C<TYPE> specifier), only 3 of them are
1041 not optimized away (one per number in the left column). The immediate
1042 children of the given node correspond to C<{}> pairs on the same level
1043 of indentation, thus this listing corresponds to the tree:
1051 The execution order is indicated by C<===E<gt>> marks, thus it is C<3
1052 4 5 6> (node C<6> is not included into above listing), i.e.,
1053 C<gvsv gvsv add whatever>.
1055 =head2 Compile pass 1: check routines
1057 The tree is created by the I<pseudo-compiler> while yacc code feeds it
1058 the constructions it recognizes. Since yacc works bottom-up, so does
1059 the first pass of perl compilation.
1061 What makes this pass interesting for perl developers is that some
1062 optimization may be performed on this pass. This is optimization by
1063 so-called I<check routines>. The correspondence between node names
1064 and corresponding check routines is described in F<opcode.pl> (do not
1065 forget to run C<make regen_headers> if you modify this file).
1067 A check routine is called when the node is fully constructed except
1068 for the execution-order thread. Since at this time there is no
1069 back-links to the currently constructed node, one can do most any
1070 operation to the top-level node, including freeing it and/or creating
1071 new nodes above/below it.
1073 The check routine returns the node which should be inserted into the
1074 tree (if the top-level node was not modified, check routine returns
1077 By convention, check routines have names C<ck_*>. They are usually
1078 called from C<new*OP> subroutines (or C<convert>) (which in turn are
1079 called from F<perly.y>).
1081 =head2 Compile pass 1a: constant folding
1083 Immediately after the check routine is called the returned node is
1084 checked for being compile-time executable. If it is (the value is
1085 judged to be constant) it is immediately executed, and a I<constant>
1086 node with the "return value" of the corresponding subtree is
1087 substituted instead. The subtree is deleted.
1089 If constant folding was not performed, the execution-order thread is
1092 =head2 Compile pass 2: context propagation
1094 When a context for a part of compile tree is known, it is propagated
1095 down through the tree. Aat this time the context can have 5 values
1096 (instead of 2 for runtime context): void, boolean, scalar, list, and
1097 lvalue. In contrast with the pass 1 this pass is processed from top
1098 to bottom: a node's context determines the context for its children.
1100 Additional context-dependent optimizations are performed at this time.
1101 Since at this moment the compile tree contains back-references (via
1102 "thread" pointers), nodes cannot be free()d now. To allow
1103 optimized-away nodes at this stage, such nodes are null()ified instead
1104 of free()ing (i.e. their type is changed to OP_NULL).
1106 =head2 Compile pass 3: peephole optimization
1108 After the compile tree for a subroutine (or for an C<eval> or a file)
1109 is created, an additional pass over the code is performed. This pass
1110 is neither top-down or bottom-up, but in the execution order (with
1111 additional compilications for conditionals). These optimizations are
1112 done in the subroutine peep(). Optimizations performed at this stage
1113 are subject to the same restrictions as in the pass 2.
1117 This is a listing of functions, macros, flags, and variables that may be
1118 useful to extension writers or that may be found while reading other
1129 Clears an array, making it empty.
1131 void av_clear _((AV* ar));
1135 Pre-extend an array. The C<key> is the index to which the array should be
1138 void av_extend _((AV* ar, I32 key));
1142 Returns the SV at the specified index in the array. The C<key> is the
1143 index. If C<lval> is set then the fetch will be part of a store. Check
1144 that the return value is non-null before dereferencing it to a C<SV*>.
1146 SV** av_fetch _((AV* ar, I32 key, I32 lval));
1150 Returns the highest index in the array. Returns -1 if the array is empty.
1152 I32 av_len _((AV* ar));
1156 Creates a new AV and populates it with a list of SVs. The SVs are copied
1157 into the array, so they may be freed after the call to av_make. The new AV
1158 will have a reference count of 1.
1160 AV* av_make _((I32 size, SV** svp));
1164 Pops an SV off the end of the array. Returns C<&sv_undef> if the array is
1167 SV* av_pop _((AV* ar));
1171 Pushes an SV onto the end of the array. The array will grow automatically
1172 to accommodate the addition.
1174 void av_push _((AV* ar, SV* val));
1178 Shifts an SV off the beginning of the array.
1180 SV* av_shift _((AV* ar));
1184 Stores an SV in an array. The array index is specified as C<key>. The
1185 return value will be null if the operation failed, otherwise it can be
1186 dereferenced to get the original C<SV*>.
1188 SV** av_store _((AV* ar, I32 key, SV* val));
1192 Undefines the array.
1194 void av_undef _((AV* ar));
1198 Unshift an SV onto the beginning of the array. The array will grow
1199 automatically to accommodate the addition.
1201 void av_unshift _((AV* ar, I32 num));
1205 Variable which is setup by C<xsubpp> to indicate the class name for a C++ XS
1206 constructor. This is always a C<char*>. See C<THIS> and
1207 L<perlxs/"Using XS With C++">.
1211 The XSUB-writer's interface to the C C<memcpy> function. The C<s> is the
1212 source, C<d> is the destination, C<n> is the number of items, and C<t> is
1215 (void) Copy( s, d, n, t );
1219 This is the XSUB-writer's interface to Perl's C<die> function. Use this
1220 function the same way you use the C C<printf> function. See C<warn>.
1224 Returns the stash of the CV.
1226 HV * CvSTASH( SV* sv )
1230 When Perl is run in debugging mode, with the B<-d> switch, this SV is a
1231 boolean which indicates whether subs are being single-stepped.
1232 Single-stepping is automatically turned on after every step. This is the C
1233 variable which corresponds to Perl's $DB::single variable. See C<DBsub>.
1237 When Perl is run in debugging mode, with the B<-d> switch, this GV contains
1238 the SV which holds the name of the sub being debugged. This is the C
1239 variable which corresponds to Perl's $DB::sub variable. See C<DBsingle>.
1240 The sub name can be found by
1242 SvPV( GvSV( DBsub ), na )
1246 Trace variable used when Perl is run in debugging mode, with the B<-d>
1247 switch. This is the C variable which corresponds to Perl's $DB::trace
1248 variable. See C<DBsingle>.
1252 Declare a stack marker variable, C<mark>, for the XSUB. See C<MARK> and
1257 Saves the original stack mark for the XSUB. See C<ORIGMARK>.
1261 The C variable which corresponds to Perl's $^W warning variable.
1265 Declares a stack pointer variable, C<sp>, for the XSUB. See C<SP>.
1269 Sets up stack and mark pointers for an XSUB, calling dSP and dMARK. This is
1270 usually handled automatically by C<xsubpp>. Declares the C<items> variable
1271 to indicate the number of items on the stack.
1275 Sets up the C<ix> variable for an XSUB which has aliases. This is usually
1276 handled automatically by C<xsubpp>.
1280 Sets up the C<ix> variable for an XSUB which has aliases. This is usually
1281 handled automatically by C<xsubpp>.
1285 Opening bracket on a callback. See C<LEAVE> and L<perlcall>.
1291 Used to extend the argument stack for an XSUB's return values.
1293 EXTEND( sp, int x );
1297 Closing bracket for temporaries on a callback. See C<SAVETMPS> and
1304 Used to indicate array context. See C<GIMME> and L<perlcall>.
1308 Indicates that arguments returned from a callback should be discarded. See
1313 Used to force a Perl C<eval> wrapper around a callback. See L<perlcall>.
1317 The XSUB-writer's equivalent to Perl's C<wantarray>. Returns C<G_SCALAR> or
1318 C<G_ARRAY> for scalar or array context.
1322 Indicates that no arguments are being sent to a callback. See L<perlcall>.
1326 Used to indicate scalar context. See C<GIMME> and L<perlcall>.
1330 Returns the glob with the given C<name> and a defined subroutine or
1331 C<NULL>. The glob lives in the given C<stash>, or in the stashes accessable
1332 via @ISA and @<UNIVERSAL>.
1334 The argument C<level> should be either 0 or -1. If C<level==0>, as a
1335 side-effect creates a glob with the given C<name> in the given
1336 C<stash> which in the case of success contains an alias for the
1337 subroutine, and sets up caching info for this glob. Similarly for all
1338 the searched stashes.
1340 The GV returned from C<gv_fetchmeth> may be a method cache entry,
1341 which is not visible to Perl code. So when calling C<perl_call_sv>,
1342 you should not use the GV directly; instead, you should use the
1343 method's CV, which can be obtained from the GV with the C<GvCV> macro.
1345 GV* gv_fetchmeth _((HV* stash, char* name, STRLEN len, I32 level));
1347 =item gv_fetchmethod
1349 Returns the glob which contains the subroutine to call to invoke the
1350 method on the C<stash>. In fact in the presense of autoloading this may
1351 be the glob for "AUTOLOAD". In this case the corresponing variable
1352 $AUTOLOAD is already setup.
1354 Note that if you want to keep this glob for a long time, you need to
1355 check for it being "AUTOLOAD", since at the later time the the call
1356 may load a different subroutine due to $AUTOLOAD changing its value.
1357 Use the glob created via a side effect to do this.
1359 This function grants C<"SUPER"> token as prefix of name or postfix of
1362 Has the same side-effects and as C<gv_fetchmeth> with C<level==0>.
1363 C<name> should be writable if contains C<':'> or C<'\''>.
1364 The warning against passing the GV returned by C<gv_fetchmeth> to
1365 C<perl_call_sv> apply equally to C<gv_fetchmethod>.
1367 GV* gv_fetchmethod _((HV* stash, char* name));
1371 Returns a pointer to the stash for a specified package. If C<create> is set
1372 then the package will be created if it does not already exist. If C<create>
1373 is not set and the package does not exist then NULL is returned.
1375 HV* gv_stashpv _((char* name, I32 create));
1379 Returns a pointer to the stash for a specified package. See C<gv_stashpv>.
1381 HV* gv_stashsv _((SV* sv, I32 create));
1385 Return the SV from the GV.
1389 Releases a hash entry, such as while iterating though the hash, but
1390 delays actual freeing of key and value until the end of the current
1391 statement (or thereabouts) with C<sv_2mortal>. See C<hv_iternext>.
1393 void he_delayfree _((HV* hv, HE* hent));
1397 Releases a hash entry, such as while iterating though the hash. See
1400 void he_free _((HV* hv, HE* hent));
1404 Clears a hash, making it empty.
1406 void hv_clear _((HV* tb));
1410 Deletes a key/value pair in the hash. The value SV is removed from the hash
1411 and returned to the caller. The C<klen> is the length of the key. The
1412 C<flags> value will normally be zero; if set to G_DISCARD then null will be
1415 SV* hv_delete _((HV* tb, char* key, U32 klen, I32 flags));
1419 Returns a boolean indicating whether the specified hash key exists. The
1420 C<klen> is the length of the key.
1422 bool hv_exists _((HV* tb, char* key, U32 klen));
1426 Returns the SV which corresponds to the specified key in the hash. The
1427 C<klen> is the length of the key. If C<lval> is set then the fetch will be
1428 part of a store. Check that the return value is non-null before
1429 dereferencing it to a C<SV*>.
1431 SV** hv_fetch _((HV* tb, char* key, U32 klen, I32 lval));
1435 Prepares a starting point to traverse a hash table.
1437 I32 hv_iterinit _((HV* tb));
1441 Returns the key from the current position of the hash iterator. See
1444 char* hv_iterkey _((HE* entry, I32* retlen));
1448 Returns entries from a hash iterator. See C<hv_iterinit>.
1450 HE* hv_iternext _((HV* tb));
1454 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1457 SV * hv_iternextsv _((HV* hv, char** key, I32* retlen));
1461 Returns the value from the current position of the hash iterator. See
1464 SV* hv_iterval _((HV* tb, HE* entry));
1468 Adds magic to a hash. See C<sv_magic>.
1470 void hv_magic _((HV* hv, GV* gv, int how));
1474 Returns the package name of a stash. See C<SvSTASH>, C<CvSTASH>.
1476 char *HvNAME (HV* stash)
1480 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
1481 the length of the key. The C<hash> parameter is the pre-computed hash
1482 value; if it is zero then Perl will compute it. The return value will be
1483 null if the operation failed, otherwise it can be dereferenced to get the
1486 SV** hv_store _((HV* tb, char* key, U32 klen, SV* val, U32 hash));
1492 void hv_undef _((HV* tb));
1496 Returns a boolean indicating whether the C C<char> is an ascii alphanumeric
1499 int isALNUM (char c)
1503 Returns a boolean indicating whether the C C<char> is an ascii alphabetic
1506 int isALPHA (char c)
1510 Returns a boolean indicating whether the C C<char> is an ascii digit.
1512 int isDIGIT (char c)
1516 Returns a boolean indicating whether the C C<char> is a lowercase character.
1518 int isLOWER (char c)
1522 Returns a boolean indicating whether the C C<char> is whitespace.
1524 int isSPACE (char c)
1528 Returns a boolean indicating whether the C C<char> is an uppercase character.
1530 int isUPPER (char c)
1534 Variable which is setup by C<xsubpp> to indicate the number of items on the
1535 stack. See L<perlxs/"Variable-length Parameter Lists">.
1539 Variable which is setup by C<xsubpp> to indicate which of an XSUB's aliases
1540 was used to invoke it. See L<perlxs/"The ALIAS: Keyword">.
1544 Closing bracket on a callback. See C<ENTER> and L<perlcall>.
1550 Stack marker variable for the XSUB. See C<dMARK>.
1554 Clear something magical that the SV represents. See C<sv_magic>.
1556 int mg_clear _((SV* sv));
1560 Copies the magic from one SV to another. See C<sv_magic>.
1562 int mg_copy _((SV *, SV *, char *, STRLEN));
1566 Finds the magic pointer for type matching the SV. See C<sv_magic>.
1568 MAGIC* mg_find _((SV* sv, int type));
1572 Free any magic storage used by the SV. See C<sv_magic>.
1574 int mg_free _((SV* sv));
1578 Do magic after a value is retrieved from the SV. See C<sv_magic>.
1580 int mg_get _((SV* sv));
1584 Report on the SV's length. See C<sv_magic>.
1586 U32 mg_len _((SV* sv));
1590 Turns on the magical status of an SV. See C<sv_magic>.
1592 void mg_magical _((SV* sv));
1596 Do magic after a value is assigned to the SV. See C<sv_magic>.
1598 int mg_set _((SV* sv));
1602 The XSUB-writer's interface to the C C<memmove> function. The C<s> is the
1603 source, C<d> is the destination, C<n> is the number of items, and C<t> is
1606 (void) Move( s, d, n, t );
1610 A variable which may be used with C<SvPV> to tell Perl to calculate the
1615 The XSUB-writer's interface to the C C<malloc> function.
1617 void * New( x, void *ptr, int size, type )
1621 The XSUB-writer's interface to the C C<malloc> function, with cast.
1623 void * Newc( x, void *ptr, int size, type, cast )
1627 The XSUB-writer's interface to the C C<malloc> function. The allocated
1628 memory is zeroed with C<memzero>.
1630 void * Newz( x, void *ptr, int size, type )
1634 Creates a new AV. The reference count is set to 1.
1636 AV* newAV _((void));
1640 Creates a new HV. The reference count is set to 1.
1642 HV* newHV _((void));
1646 Creates an RV wrapper for an SV. The reference count for the original SV is
1649 SV* newRV_inc _((SV* ref));
1651 For historical reasons, "newRV" is a synonym for "newRV_inc".
1655 Creates an RV wrapper for an SV. The reference count for the original
1656 SV is B<not> incremented.
1658 SV* newRV_noinc _((SV* ref));
1662 Creates a new SV. The C<len> parameter indicates the number of bytes of
1663 pre-allocated string space the SV should have. The reference count for the
1666 SV* newSV _((STRLEN len));
1670 Creates a new SV and copies an integer into it. The reference count for the
1673 SV* newSViv _((IV i));
1677 Creates a new SV and copies a double into it. The reference count for the
1680 SV* newSVnv _((NV i));
1684 Creates a new SV and copies a string into it. The reference count for the
1685 SV is set to 1. If C<len> is zero then Perl will compute the length.
1687 SV* newSVpv _((char* s, STRLEN len));
1691 Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
1692 it will be upgraded to one. If C<classname> is non-null then the new SV will
1693 be blessed in the specified package. The new SV is returned and its
1694 reference count is 1.
1696 SV* newSVrv _((SV* rv, char* classname));
1700 Creates a new SV which is an exact duplicate of the original SV.
1702 SV* newSVsv _((SV* old));
1706 Used by C<xsubpp> to hook up XSUBs as Perl subs.
1710 Used by C<xsubpp> to hook up XSUBs as Perl subs. Adds Perl prototypes to
1719 Null character pointer.
1735 The original stack mark for the XSUB. See C<dORIGMARK>.
1739 Allocates a new Perl interpreter. See L<perlembed>.
1741 =item perl_call_argv
1743 Performs a callback to the specified Perl sub. See L<perlcall>.
1745 I32 perl_call_argv _((char* subname, I32 flags, char** argv));
1747 =item perl_call_method
1749 Performs a callback to the specified Perl method. The blessed object must
1750 be on the stack. See L<perlcall>.
1752 I32 perl_call_method _((char* methname, I32 flags));
1756 Performs a callback to the specified Perl sub. See L<perlcall>.
1758 I32 perl_call_pv _((char* subname, I32 flags));
1762 Performs a callback to the Perl sub whose name is in the SV. See
1765 I32 perl_call_sv _((SV* sv, I32 flags));
1767 =item perl_construct
1769 Initializes a new Perl interpreter. See L<perlembed>.
1773 Shuts down a Perl interpreter. See L<perlembed>.
1777 Tells Perl to C<eval> the string in the SV.
1779 I32 perl_eval_sv _((SV* sv, I32 flags));
1783 Releases a Perl interpreter. See L<perlembed>.
1787 Returns the AV of the specified Perl array. If C<create> is set and the
1788 Perl variable does not exist then it will be created. If C<create> is not
1789 set and the variable does not exist then null is returned.
1791 AV* perl_get_av _((char* name, I32 create));
1795 Returns the CV of the specified Perl sub. If C<create> is set and the Perl
1796 variable does not exist then it will be created. If C<create> is not
1797 set and the variable does not exist then null is returned.
1799 CV* perl_get_cv _((char* name, I32 create));
1803 Returns the HV of the specified Perl hash. If C<create> is set and the Perl
1804 variable does not exist then it will be created. If C<create> is not
1805 set and the variable does not exist then null is returned.
1807 HV* perl_get_hv _((char* name, I32 create));
1811 Returns the SV of the specified Perl scalar. If C<create> is set and the
1812 Perl variable does not exist then it will be created. If C<create> is not
1813 set and the variable does not exist then null is returned.
1815 SV* perl_get_sv _((char* name, I32 create));
1819 Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
1821 =item perl_require_pv
1823 Tells Perl to C<require> a module.
1825 void perl_require_pv _((char* pv));
1829 Tells a Perl interpreter to run. See L<perlembed>.
1833 Pops an integer off the stack.
1839 Pops a long off the stack.
1845 Pops a string off the stack.
1851 Pops a double off the stack.
1857 Pops an SV off the stack.
1863 Opening bracket for arguments on a callback. See C<PUTBACK> and L<perlcall>.
1869 Push an integer onto the stack. The stack must have room for this element.
1876 Push a double onto the stack. The stack must have room for this element.
1883 Push a string onto the stack. The stack must have room for this element.
1884 The C<len> indicates the length of the string. See C<XPUSHp>.
1886 PUSHp(char *c, int len )
1890 Push an SV onto the stack. The stack must have room for this element. See
1897 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
1898 See C<PUSHMARK> and L<perlcall> for other uses.
1904 The XSUB-writer's interface to the C C<realloc> function.
1906 void * Renew( void *ptr, int size, type )
1910 The XSUB-writer's interface to the C C<realloc> function, with cast.
1912 void * Renewc( void *ptr, int size, type, cast )
1916 Variable which is setup by C<xsubpp> to hold the return value for an XSUB.
1917 This is always the proper type for the XSUB.
1918 See L<perlxs/"The RETVAL Variable">.
1922 The XSUB-writer's interface to the C C<free> function.
1926 The XSUB-writer's interface to the C C<malloc> function.
1930 The XSUB-writer's interface to the C C<realloc> function.
1934 Copy a string to a safe spot. This does not use an SV.
1936 char* savepv _((char* sv));
1940 Copy a string to a safe spot. The C<len> indicates number of bytes to
1941 copy. This does not use an SV.
1943 char* savepvn _((char* sv, I32 len));
1947 Opening bracket for temporaries on a callback. See C<FREETMPS> and
1954 Stack pointer. This is usually handled by C<xsubpp>. See C<dSP> and
1959 Re-fetch the stack pointer. Used after a callback. See L<perlcall>.
1965 Used to access elements on the XSUB's stack.
1971 Test two strings to see if they are equal. Returns true or false.
1973 int strEQ( char *s1, char *s2 )
1977 Test two strings to see if the first, C<s1>, is greater than or equal to the
1978 second, C<s2>. Returns true or false.
1980 int strGE( char *s1, char *s2 )
1984 Test two strings to see if the first, C<s1>, is greater than the second,
1985 C<s2>. Returns true or false.
1987 int strGT( char *s1, char *s2 )
1991 Test two strings to see if the first, C<s1>, is less than or equal to the
1992 second, C<s2>. Returns true or false.
1994 int strLE( char *s1, char *s2 )
1998 Test two strings to see if the first, C<s1>, is less than the second,
1999 C<s2>. Returns true or false.
2001 int strLT( char *s1, char *s2 )
2005 Test two strings to see if they are different. Returns true or false.
2007 int strNE( char *s1, char *s2 )
2011 Test two strings to see if they are equal. The C<len> parameter indicates
2012 the number of bytes to compare. Returns true or false.
2014 int strnEQ( char *s1, char *s2 )
2018 Test two strings to see if they are different. The C<len> parameter
2019 indicates the number of bytes to compare. Returns true or false.
2021 int strnNE( char *s1, char *s2, int len )
2025 Marks an SV as mortal. The SV will be destroyed when the current context
2028 SV* sv_2mortal _((SV* sv));
2032 Blesses an SV into a specified package. The SV must be an RV. The package
2033 must be designated by its stash (see C<gv_stashpv()>). The reference count
2034 of the SV is unaffected.
2036 SV* sv_bless _((SV* sv, HV* stash));
2040 Concatenates the string onto the end of the string which is in the SV.
2042 void sv_catpv _((SV* sv, char* ptr));
2046 Concatenates the string onto the end of the string which is in the SV. The
2047 C<len> indicates number of bytes to copy.
2049 void sv_catpvn _((SV* sv, char* ptr, STRLEN len));
2053 Concatenates the string from SV C<ssv> onto the end of the string in SV
2056 void sv_catsv _((SV* dsv, SV* ssv));
2060 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
2061 string in C<sv1> is less than, equal to, or greater than the string in
2064 I32 sv_cmp _((SV* sv1, SV* sv2));
2068 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
2069 string in C<sv1> is less than, equal to, or greater than the string in
2072 I32 sv_cmp _((SV* sv1, SV* sv2));
2076 Returns the length of the string which is in the SV. See C<SvLEN>.
2082 Set the length of the string which is in the SV. See C<SvCUR>.
2084 SvCUR_set (SV* sv, int val )
2088 Auto-decrement of the value in the SV.
2090 void sv_dec _((SV* sv));
2094 Auto-decrement of the value in the SV.
2096 void sv_dec _((SV* sv));
2100 Returns a pointer to the last character in the string which is in the SV.
2101 See C<SvCUR>. Access the character as
2107 Returns a boolean indicating whether the strings in the two SVs are
2110 I32 sv_eq _((SV* sv1, SV* sv2));
2114 Expands the character buffer in the SV. Calls C<sv_grow> to perform the
2115 expansion if necessary. Returns a pointer to the character buffer.
2117 char * SvGROW( SV* sv, int len )
2121 Expands the character buffer in the SV. This will use C<sv_unref> and will
2122 upgrade the SV to C<SVt_PV>. Returns a pointer to the character buffer.
2127 Auto-increment of the value in the SV.
2129 void sv_inc _((SV* sv));
2133 Returns a boolean indicating whether the SV contains an integer.
2139 Unsets the IV status of an SV.
2145 Tells an SV that it is an integer.
2151 Tells an SV that it is an integer and disables all other OK bits.
2157 Tells an SV that it is an integer and disables all other OK bits.
2163 Returns a boolean indicating whether the SV contains an integer. Checks the
2164 B<private> setting. Use C<SvIOK>.
2170 Returns a boolean indicating whether the SV is blessed into the specified
2171 class. This does not know how to check for subtype, so it doesn't work in
2172 an inheritance relationship.
2174 int sv_isa _((SV* sv, char* name));
2178 Returns the integer which is in the SV.
2184 Returns a boolean indicating whether the SV is an RV pointing to a blessed
2185 object. If the SV is not an RV, or if the object is not blessed, then this
2188 int sv_isobject _((SV* sv));
2192 Returns the integer which is stored in the SV.
2198 Returns the size of the string buffer in the SV. See C<SvCUR>.
2204 Returns the length of the string in the SV. Use C<SvCUR>.
2206 STRLEN sv_len _((SV* sv));
2210 Returns the length of the string in the SV. Use C<SvCUR>.
2212 STRLEN sv_len _((SV* sv));
2216 Adds magic to an SV.
2218 void sv_magic _((SV* sv, SV* obj, int how, char* name, I32 namlen));
2222 Creates a new SV which is a copy of the original SV. The new SV is marked
2225 SV* sv_mortalcopy _((SV* oldsv));
2229 Returns a boolean indicating whether the value is an SV.
2235 Creates a new SV which is mortal. The reference count of the SV is set to 1.
2237 SV* sv_newmortal _((void));
2241 This is the C<false> SV. See C<sv_yes>. Always refer to this as C<&sv_no>.
2245 Returns a boolean indicating whether the SV contains a number, integer or
2252 Unsets the NV/IV status of an SV.
2258 Returns a boolean indicating whether the SV contains a number, integer or
2259 double. Checks the B<private> setting. Use C<SvNIOK>.
2261 int SvNIOKp (SV* SV)
2265 Returns a boolean indicating whether the SV contains a double.
2271 Unsets the NV status of an SV.
2277 Tells an SV that it is a double.
2283 Tells an SV that it is a double and disables all other OK bits.
2289 Tells an SV that it is a double and disables all other OK bits.
2295 Returns a boolean indicating whether the SV contains a double. Checks the
2296 B<private> setting. Use C<SvNOK>.
2302 Returns the double which is stored in the SV.
2304 double SvNV (SV* sv);
2308 Returns the double which is stored in the SV.
2310 double SvNVX (SV* sv);
2314 Returns a boolean indicating whether the SV contains a character string.
2320 Unsets the PV status of an SV.
2326 Tells an SV that it is a string.
2332 Tells an SV that it is a string and disables all other OK bits.
2338 Tells an SV that it is a string and disables all other OK bits.
2344 Returns a boolean indicating whether the SV contains a character string.
2345 Checks the B<private> setting. Use C<SvPOK>.
2351 Returns a pointer to the string in the SV, or a stringified form of the SV
2352 if the SV does not contain a string. If C<len> is C<na> then Perl will
2353 handle the length on its own.
2355 char * SvPV (SV* sv, int len )
2359 Returns a pointer to the string in the SV. The SV must contain a string.
2361 char * SvPVX (SV* sv)
2365 Returns the value of the object's reference count.
2367 int SvREFCNT (SV* sv);
2371 Decrements the reference count of the given SV.
2373 void SvREFCNT_dec (SV* sv)
2377 Increments the reference count of the given SV.
2379 void SvREFCNT_inc (SV* sv)
2383 Tests if the SV is an RV.
2389 Unsets the RV status of an SV.
2395 Tells an SV that it is an RV.
2401 Dereferences an RV to return the SV.
2407 Copies an integer into the given SV.
2409 void sv_setiv _((SV* sv, IV num));
2413 Copies a double into the given SV.
2415 void sv_setnv _((SV* sv, double num));
2419 Copies a string into an SV. The string must be null-terminated.
2421 void sv_setpv _((SV* sv, char* ptr));
2425 Copies a string into an SV. The C<len> parameter indicates the number of
2428 void sv_setpvn _((SV* sv, char* ptr, STRLEN len));
2432 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
2433 argument will be upgraded to an RV. That RV will be modified to point to
2434 the new SV. The C<classname> argument indicates the package for the
2435 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
2436 will be returned and will have a reference count of 1.
2438 SV* sv_setref_iv _((SV *rv, char *classname, IV iv));
2442 Copies a double into a new SV, optionally blessing the SV. The C<rv>
2443 argument will be upgraded to an RV. That RV will be modified to point to
2444 the new SV. The C<classname> argument indicates the package for the
2445 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
2446 will be returned and will have a reference count of 1.
2448 SV* sv_setref_nv _((SV *rv, char *classname, double nv));
2452 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
2453 argument will be upgraded to an RV. That RV will be modified to point to
2454 the new SV. If the C<pv> argument is NULL then C<sv_undef> will be placed
2455 into the SV. The C<classname> argument indicates the package for the
2456 blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
2457 will be returned and will have a reference count of 1.
2459 SV* sv_setref_pv _((SV *rv, char *classname, void* pv));
2461 Do not use with integral Perl types such as HV, AV, SV, CV, because those
2462 objects will become corrupted by the pointer copy process.
2464 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
2468 Copies a string into a new SV, optionally blessing the SV. The length of the
2469 string must be specified with C<n>. The C<rv> argument will be upgraded to
2470 an RV. That RV will be modified to point to the new SV. The C<classname>
2471 argument indicates the package for the blessing. Set C<classname> to
2472 C<Nullch> to avoid the blessing. The new SV will be returned and will have
2473 a reference count of 1.
2475 SV* sv_setref_pvn _((SV *rv, char *classname, char* pv, I32 n));
2477 Note that C<sv_setref_pv> copies the pointer while this copies the string.
2481 Copies the contents of the source SV C<ssv> into the destination SV C<dsv>.
2482 The source SV may be destroyed if it is mortal.
2484 void sv_setsv _((SV* dsv, SV* ssv));
2488 Returns the stash of the SV.
2490 HV * SvSTASH (SV* sv)
2494 Integer type flag for scalars. See C<svtype>.
2498 Pointer type flag for scalars. See C<svtype>.
2502 Type flag for arrays. See C<svtype>.
2506 Type flag for code refs. See C<svtype>.
2510 Type flag for hashes. See C<svtype>.
2514 Type flag for blessed scalars. See C<svtype>.
2518 Double type flag for scalars. See C<svtype>.
2522 Returns a boolean indicating whether Perl would evaluate the SV as true or
2523 false, defined or undefined.
2529 Returns the type of the SV. See C<svtype>.
2531 svtype SvTYPE (SV* sv)
2535 An enum of flags for Perl types. These are found in the file B<sv.h> in the
2536 C<svtype> enum. Test these flags with the C<SvTYPE> macro.
2540 Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to perform
2541 the upgrade if necessary. See C<svtype>.
2543 bool SvUPGRADE _((SV* sv, svtype mt));
2547 Upgrade an SV to a more complex form. Use C<SvUPGRADE>. See C<svtype>.
2551 This is the C<undef> SV. Always refer to this as C<&sv_undef>.
2555 Unsets the RV status of the SV, and decrements the reference count of
2556 whatever was being referenced by the RV. This can almost be thought of
2557 as a reversal of C<newSVrv>. See C<SvROK_off>.
2559 void sv_unref _((SV* sv));
2563 Tells an SV to use C<ptr> to find its string value. Normally the string is
2564 stored inside the SV but sv_usepvn allows the SV to use an outside string.
2565 The C<ptr> should point to memory that was allocated by C<malloc>. The
2566 string length, C<len>, must be supplied. This function will realloc the
2567 memory pointed to by C<ptr>, so that pointer should not be freed or used by
2568 the programmer after giving it to sv_usepvn.
2570 void sv_usepvn _((SV* sv, char* ptr, STRLEN len));
2574 This is the C<true> SV. See C<sv_no>. Always refer to this as C<&sv_yes>.
2578 Variable which is setup by C<xsubpp> to designate the object in a C++ XSUB.
2579 This is always the proper type for the C++ object. See C<CLASS> and
2580 L<perlxs/"Using XS With C++">.
2584 Converts the specified character to lowercase.
2586 int toLOWER (char c)
2590 Converts the specified character to uppercase.
2592 int toUPPER (char c)
2596 This is the XSUB-writer's interface to Perl's C<warn> function. Use this
2597 function the same way you use the C C<printf> function. See C<croak()>.
2601 Push an integer onto the stack, extending the stack if necessary. See
2608 Push a double onto the stack, extending the stack if necessary. See
2615 Push a string onto the stack, extending the stack if necessary. The C<len>
2616 indicates the length of the string. See C<PUSHp>.
2618 XPUSHp(char *c, int len)
2622 Push an SV onto the stack, extending the stack if necessary. See C<PUSHs>.
2628 Macro to declare an XSUB and its C parameter list. This is handled by
2633 Return from XSUB, indicating number of items on the stack. This is usually
2634 handled by C<xsubpp>.
2638 =item XSRETURN_EMPTY
2640 Return an empty list from an XSUB immediately.
2646 Return an integer from an XSUB immediately. Uses C<XST_mIV>.
2652 Return C<&sv_no> from an XSUB immediately. Uses C<XST_mNO>.
2658 Return an double from an XSUB immediately. Uses C<XST_mNV>.
2664 Return a copy of a string from an XSUB immediately. Uses C<XST_mPV>.
2666 XSRETURN_PV(char *v);
2668 =item XSRETURN_UNDEF
2670 Return C<&sv_undef> from an XSUB immediately. Uses C<XST_mUNDEF>.
2676 Return C<&sv_yes> from an XSUB immediately. Uses C<XST_mYES>.
2682 Place an integer into the specified position C<i> on the stack. The value is
2683 stored in a new mortal SV.
2685 XST_mIV( int i, IV v );
2689 Place a double into the specified position C<i> on the stack. The value is
2690 stored in a new mortal SV.
2692 XST_mNV( int i, NV v );
2696 Place C<&sv_no> into the specified position C<i> on the stack.
2702 Place a copy of a string into the specified position C<i> on the stack. The
2703 value is stored in a new mortal SV.
2705 XST_mPV( int i, char *v );
2709 Place C<&sv_undef> into the specified position C<i> on the stack.
2711 XST_mUNDEF( int i );
2715 Place C<&sv_yes> into the specified position C<i> on the stack.
2721 The version identifier for an XS module. This is usually handled
2722 automatically by C<ExtUtils::MakeMaker>. See C<XS_VERSION_BOOTCHECK>.
2724 =item XS_VERSION_BOOTCHECK
2726 Macro to verify that a PM module's $VERSION variable matches the XS module's
2727 C<XS_VERSION> variable. This is usually handled automatically by
2728 C<xsubpp>. See L<perlxs/"The VERSIONCHECK: Keyword">.
2732 The XSUB-writer's interface to the C C<memzero> function. The C<d> is the
2733 destination, C<n> is the number of items, and C<t> is the type.
2735 (void) Zero( d, n, t );
2741 Jeff Okamoto <okamoto@corp.hp.com>
2743 With lots of help and suggestions from Dean Roehrich, Malcolm Beattie,
2744 Andreas Koenig, Paul Hudson, Ilya Zakharevich, Paul Marquess, Neil
2745 Bowers, Matthew Green, Tim Bunce, Spider Boardman, and Ulrich Pfeifer.
2747 API Listing by Dean Roehrich <roehrich@cray.com>.
2751 Version 31: 1997/1/27