X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlguts.pod;h=aa5de9f74ffa65f10f25e89d173d16965d2aa48a;hb=0c4128adc5f62cd332ae4fa5bc8999c95d611d36;hp=dad868a71218a01782c6f3b40cc3452e80712486;hpb=a7486cbbe7de2a5d93376a3ce396434afeb67f8a;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlguts.pod b/pod/perlguts.pod index dad868a..aa5de9f 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -77,7 +77,7 @@ important. Note that this function requires you to specify the length of the format. STRLEN is an integer type (Size_t, usually defined as size_t in -config.h) guaranteed to be large enough to represent the size of +config.h) guaranteed to be large enough to represent the size of any string that perl can handle. The C functions are not generic enough to operate on values @@ -224,7 +224,7 @@ actually removing the characters, C sets the flag C (offset OK) to signal to other functions that the offset hack is in effect, and it puts the number of bytes chopped off into the IV field of the SV. It then moves the PV pointer (called C) forward that -many bytes, and adjusts C and C. +many bytes, and adjusts C and C. Hence, at this point, the start of the buffer that we allocated lives at C in memory and the PV pointer is pointing @@ -274,6 +274,14 @@ pointer in an SV, you can use the following three macros instead: These will tell you if you truly have an integer, double, or string pointer stored in your SV. The "p" stands for private. +The are various ways in which the private and public flags may differ. +For example, a tied SV may have a valid underlying value in the IV slot +(so SvIOKp is true), but the data should be accessed via the FETCH +routine rather than directly, so SvIOK is false. Another is when +numeric conversion has occured and precision has been lost: only the +private flag is set on 'lossy' values. So when an NV is converted to an +IV with loss, SvIOKp, SvNOKp and SvNOK will be set, while SvIOK wont be. + In general, though, it's best to use the C macros. =head2 Working with AVs @@ -571,7 +579,7 @@ is the function implementing the C functionality. bool sv_derived_from(SV* sv, const char* name); -To check if you've got an object derived from a specific class you have +To check if you've got an object derived from a specific class you have to write: if (sv_isobject(sv) && sv_derived_from(sv, class)) { ... } @@ -650,9 +658,11 @@ See L and L for more details on these macros. However, if you mortalize a variable twice, the reference count will later be decremented twice. -You should be careful about creating mortal variables. Strange things -can happen if you make the same value mortal within multiple contexts, -or if you make a variable mortal multiple times. +"Mortal" SVs are mainly used for SVs that are placed on perl's stack. +For example an SV which is created just to pass a number to a called sub +is made mortal to have it cleaned up automatically when stack is popped. +Similarly results returned by XSUBs (which go in the stack) are often +made mortal. To create a mortal variable, use the functions: @@ -660,9 +670,28 @@ To create a mortal variable, use the functions: SV* sv_2mortal(SV*) SV* sv_mortalcopy(SV*) -The first call creates a mortal SV, the second converts an existing +The first call creates a mortal SV (with no value), the second converts an existing SV to a mortal SV (and thus defers a call to C), and the third creates a mortal copy of an existing SV. +Because C gives the new SV no value,it must normally be given one +via C, C etc. : + + SV *tmp = sv_newmortal(); + sv_setiv(tmp, an_integer); + +As that is multiple C statements it is quite common so see this idiom instead: + + SV *tmp = sv_2mortal(newSViv(an_integer)); + + +You should be careful about creating mortal variables. Strange things +can happen if you make the same value mortal within multiple contexts, +or if you make a variable mortal multiple times. Thinking of "Mortalization" +as deferred C should help to minimize such problems. +For example if you are passing an SV which you I has high enough REFCNT +to survive its use on the stack you need not do any mortalization. +If you are not sure then doing an C and C, or +making a C is safer. The mortal routines are not just for SVs -- AVs and HVs can be made mortal by passing their address (type-casted to C) to the @@ -810,13 +839,17 @@ copy of the name is stored in C field. The sv_magic function uses C to determine which, if any, predefined "Magic Virtual Table" should be assigned to the C field. See the "Magic Virtual Table" section below. The C argument is also -stored in the C field. +stored in the C field. The value of C should be chosen +from the set of macros C found perl.h. Note that before +these macros were added, perl internals used to directly use character +literals, so you may occasionally come across old code or documentation +referrring to 'U' magic rather than C for example. The C argument is stored in the C field of the C structure. If it is not the same as the C argument, the reference count of the C object is incremented. If it is the same, or if -the C argument is "#", or if it is a NULL pointer, then C is -merely stored, without the reference count being incremented. +the C argument is C", or if it is a NULL pointer, +then C is merely stored, without the reference count being incremented. There is also a function to add magic to an C: @@ -860,67 +893,76 @@ actions depending on which function is being called. svt_free Free any extra storage associated with the SV. For instance, the MGVTBL structure called C (which corresponds -to an C of '\0') contains: +to an C of C) contains: { magic_get, magic_set, magic_len, 0, 0 } -Thus, when an SV is determined to be magical and of type '\0', if a get -operation is being performed, the routine C is called. All -the various routines for the various magical types begin with C. -NOTE: the magic routines are not considered part of the Perl API, and may -not be exported by the Perl library. +Thus, when an SV is determined to be magical and of type C, +if a get operation is being performed, the routine C is +called. All the various routines for the various magical types begin +with C. NOTE: the magic routines are not considered part of +the Perl API, and may not be exported by the Perl library. The current kinds of Magic Virtual Tables are: - mg_type MGVTBL Type of magic - ------- ------ ---------------------------- - \0 vtbl_sv Special scalar variable - A vtbl_amagic %OVERLOAD hash - a vtbl_amagicelem %OVERLOAD hash element - c (none) Holds overload table (AMT) on stash - B vtbl_bm Boyer-Moore (fast string search) - D vtbl_regdata Regex match position data (@+ and @- vars) - d vtbl_regdatum Regex match position data element - E vtbl_env %ENV hash - e vtbl_envelem %ENV hash element - f vtbl_fm Formline ('compiled' format) - g vtbl_mglob m//g target / study()ed string - I vtbl_isa @ISA array - i vtbl_isaelem @ISA array element - k vtbl_nkeys scalar(keys()) lvalue - L (none) Debugger %_'s +that composite type. Some internals code makes use of this case +relationship. + +The C and C magic types are defined +specifically for use by extensions and will not be used by perl itself. +Extensions can use C magic to 'attach' private information +to variables (typically objects). This is especially useful because +there is no way for normal perl code to corrupt this private information +(unlike using extra elements of a hash object). + +Similarly, C magic can be used much like tie() to call a +C function any time a scalar's value is used or changed. The C's C field points to a C structure: struct ufuncs { @@ -930,8 +972,8 @@ C field points to a C structure: }; When the SV is read from or written to, the C or C -function will be called with C as the first arg and a -pointer to the SV as the second. A simple example of how to add 'U' +function will be called with C as the first arg and a pointer to +the SV as the second. A simple example of how to add C magic is shown below. Note that the ufuncs structure is copied by sv_magic, so you can safely allocate it on the stack. @@ -944,14 +986,14 @@ sv_magic, so you can safely allocate it on the stack. uf.uf_val = &my_get_fn; uf.uf_set = &my_set_fn; uf.uf_index = 0; - sv_magic(sv, 0, 'U', (char*)&uf, sizeof(uf)); + sv_magic(sv, 0, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf)); -Note that because multiple extensions may be using '~' or 'U' magic, -it is important for extensions to take extra care to avoid conflict. -Typically only using the magic on objects blessed into the same class -as the extension is sufficient. For '~' magic, it may also be -appropriate to add an I32 'signature' at the top of the private data -area and check that. +Note that because multiple extensions may be using C +or C magic, it is important for extensions to take +extra care to avoid conflict. Typically only using the magic on +objects blessed into the same class as the extension is sufficient. +For C magic, it may also be appropriate to add an I32 +'signature' at the top of the private data area and check that. Also note that the C and C functions described earlier do B invoke 'set' magic on their targets. This must @@ -981,7 +1023,8 @@ the mg_type field is changed to be the lowercase letter. =head2 Understanding the Magic of Tied Hashes and Arrays -Tied hashes and arrays are magical beasts of the 'P' magic type. +Tied hashes and arrays are magical beasts of the C +magic type. WARNING: As of the 5.004 release, proper usage of the array and hash access functions requires understanding a few caveats. Some @@ -1012,7 +1055,7 @@ to do this. tie = newRV_noinc((SV*)newHV()); stash = gv_stashpv("MyTie", TRUE); sv_bless(tie, stash); - hv_magic(hash, tie, 'P'); + hv_magic(hash, (GV*)tie, PERL_MAGIC_tied); RETVAL = newRV_noinc(hash); OUTPUT: RETVAL @@ -1127,8 +1170,20 @@ and back. =item C The refcount of C would be decremented at the end of -I. This is similar to C, which should (?) be -used instead. +I. This is similar to C in that it is also a +mechanism for doing a delayed C. However, while C +extends the lifetime of C until the beginning of the next statement, +C extends it until the end of the enclosing scope. These +lifetimes can be wildly different. + +Also compare C. + +=item C + +Just like C, but mortalizes C at the end of the current +scope instead of decrementing its reference count. This usually has the +effect of keeping C alive until the statement that called the currently +live scope has finished executing. =item C @@ -1172,7 +1227,7 @@ at the end of I. The following API list contains functions, thus one needs to provide pointers to the modifiable data explicitly (either C pointers, -or Perlish Cs). Where the above macros take C, a similar +or Perlish Cs). Where the above macros take C, a similar function takes C. =over 4 @@ -1241,13 +1296,12 @@ extended using the macro: where C is the macro that represents the local copy of the stack pointer, and C is the number of elements the stack should be extended by. -Now that there is room on the stack, values can be pushed on it using the -macros to push IVs, doubles, strings, and SV pointers respectively: +Now that there is room on the stack, values can be pushed on it using C +macro. The values pushed will often need to be "mortal" (See L). - PUSHi(IV) - PUSHn(double) - PUSHp(char*, I32) - PUSHs(SV*) + PUSHs(sv_2mortal(newSViv(an_integer))) + PUSHs(sv_2mortal(newSVpv("Some String",0))) + PUSHs(sv_2mortal(newSVnv(3.141592))) And now the Perl program calling C, the two values will be assigned as in: @@ -1255,16 +1309,16 @@ as in: ($standard_abbrev, $summer_abbrev) = POSIX::tzname; An alternate (and possibly simpler) method to pushing values on the stack is -to use the macros: +to use the macro: - XPUSHi(IV) - XPUSHn(double) - XPUSHp(char*, I32) XPUSHs(SV*) -These macros automatically adjust the stack for you, if needed. Thus, you +This macro automatically adjust the stack for you, if needed. Thus, you do not need to call C to extend the stack. -However, see L + +Despite their suggestions in earlier versions of this document the macros +C, C and C are I suited to XSUBs which return +multiple results, see L. For more information, consult L and L. @@ -1409,7 +1463,7 @@ which bypasses C. On a related note, if you do use C<(X)PUSH[npi]>, then you're going to need a C in your variable declarations so that the C<*PUSH*> -macros can make use of the local variable C. +macros can make use of the local variable C. =head2 Scratchpads @@ -1621,7 +1675,7 @@ functions which produce formatted output of internal data structures. The most commonly used of these functions is C; it's used for dumping SVs, AVs, HVs, and CVs. The C module calls C to produce debugging output from Perl-space, so users of that -module should already be familiar with its format. +module should already be familiar with its format. C can be used to dump an C structure or any of its derivatives, and produces output similiar to C; in fact, @@ -1678,13 +1732,13 @@ the Perl source (as it does in so many other situations) makes heavy use of macros and subroutine naming conventions. First problem: deciding which functions will be public API functions and -which will be private. All functions whose names begin C are private +which will be private. All functions whose names begin C are private (think "S" for "secret" or "static"). All other functions begin with "Perl_", but just because a function begins with "Perl_" does not mean it is -part of the API. (See L.) The easiest way to be B a -function is part of the API is to find its entry in L. -If it exists in L, it's part of the API. If it doesn't, and you -think it should be (i.e., you need it for your extension), send mail via +part of the API. (See L.) The easiest way to be B a +function is part of the API is to find its entry in L. +If it exists in L, it's part of the API. If it doesn't, and you +think it should be (i.e., you need it for your extension), send mail via L explaining why you think it should be. Second problem: there must be a syntax so that the same subroutine @@ -1994,7 +2048,7 @@ The argument list should end with C<...>, like this: =item M -This function is part of the experimental development API, and may change +This function is part of the experimental development API, and may change or disappear without notice. =item o