X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/70a53b35304cccc28c78bbda3a5e80f421d5a8a0..e69061dc352aead76c31e0d932d915ea7ce26512:/pod/perlguts.pod diff --git a/pod/perlguts.pod b/pod/perlguts.pod index 00df255..d8f0527 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -619,41 +619,45 @@ L for information on converting class names into stashes. /* Still under construction */ -Upgrades rv to reference if not already one. Creates new SV for rv to -point to. If C is non-null, the SV is blessed into the specified -class. SV is returned. +The following function upgrades rv to reference if not already one. +Creates a new SV for rv to point to. If C is non-null, the SV +is blessed into the specified class. SV is returned. SV* newSVrv(SV* rv, const char* classname); -Copies integer, unsigned integer or double into an SV whose reference is C. SV is blessed -if C is non-null. +The following three functions copy integer, unsigned integer or double +into an SV whose reference is C. SV is blessed if C is +non-null. SV* sv_setref_iv(SV* rv, const char* classname, IV iv); SV* sv_setref_uv(SV* rv, const char* classname, UV uv); SV* sv_setref_nv(SV* rv, const char* classname, NV iv); -Copies the pointer value (I) into an SV whose -reference is rv. SV is blessed if C is non-null. +The following function copies the pointer value (I) into an SV whose reference is rv. SV is blessed if C +is non-null. - SV* sv_setref_pv(SV* rv, const char* classname, PV iv); + SV* sv_setref_pv(SV* rv, const char* classname, void* pv); -Copies string into an SV whose reference is C. Set length to 0 to let -Perl calculate the string length. SV is blessed if C is non-null. +The following function copies string into an SV whose reference is C. +Set length to 0 to let Perl calculate the string length. SV is blessed if +C is non-null. - SV* sv_setref_pvn(SV* rv, const char* classname, PV iv, STRLEN length); + SV* sv_setref_pvn(SV* rv, const char* classname, char* pv, STRLEN length); -Tests whether the SV is blessed into the specified class. It does not -check inheritance relationships. +The following function tests whether the SV is blessed into the specified +class. It does not check inheritance relationships. int sv_isa(SV* sv, const char* name); -Tests whether the SV is a reference to a blessed object. +The following function tests whether the SV is a reference to a blessed object. int sv_isobject(SV* sv); -Tests whether the SV is derived from the specified class. SV can be either -a reference to a blessed object or a string containing a class name. This -is the function implementing the C functionality. +The following function tests whether the SV is derived from the specified +class. SV can be either a reference to a blessed object or a string +containing a class name. This is the function implementing the +C functionality. bool sv_derived_from(SV* sv, const char* name); @@ -671,11 +675,11 @@ your Perl script, use the following routines, depending on the variable type. AV* get_av("package::varname", GV_ADD); HV* get_hv("package::varname", GV_ADD); -Notice the use of TRUE as the second parameter. The new variable can now +Notice the use of GV_ADD as the second parameter. The new variable can now be set, using the routines appropriate to the data type. There are additional macros whose values may be bitwise OR'ed with the -C argument to enable certain extra features. Those bits are: +C argument to enable certain extra features. Those bits are: =over @@ -963,6 +967,12 @@ To remove the magic from an SV, call the function sv_unmagic: The C argument should be equal to the C value when the C was initially made magical. +However, note that C removes all magic of a certain C from the +C. If you want to remove only certain magic of a C based on the magic +virtual table, use C instead: + + int sv_unmagicext(SV *sv, int type, MGVTBL *vtbl); + =head2 Magic Virtual Tables The C field in the C structure is a pointer to an @@ -1025,19 +1035,28 @@ The current kinds of Magic Virtual Tables are: (old-style char and macro) MGVTBL Type of magic -------------------------- ------ ------------- \0 PERL_MAGIC_sv vtbl_sv Special scalar variable + # PERL_MAGIC_arylen vtbl_arylen Array length ($#ary) + % PERL_MAGIC_rhash (none) extra data for restricted + hashes + . PERL_MAGIC_pos vtbl_pos pos() lvalue + : PERL_MAGIC_symtab (none) extra data for symbol tables + < PERL_MAGIC_backref vtbl_backref for weak ref data + @ PERL_MAGIC_arylen_p (none) to move arylen out of XPVAV A PERL_MAGIC_overload vtbl_amagic %OVERLOAD hash a PERL_MAGIC_overload_elem vtbl_amagicelem %OVERLOAD hash element - c PERL_MAGIC_overload_table (none) Holds overload table (AMT) - on stash - B PERL_MAGIC_bm vtbl_bm Boyer-Moore (fast string search) - D PERL_MAGIC_regdata vtbl_regdata Regex match position data + B PERL_MAGIC_bm vtbl_regexp Boyer-Moore + (fast string search) + c PERL_MAGIC_overload_table vtbl_ovrld Holds overload table + (AMT) on stash + D PERL_MAGIC_regdata vtbl_regdata Regex match position data (@+ and @- vars) d PERL_MAGIC_regdatum vtbl_regdatum Regex match position data element E PERL_MAGIC_env vtbl_env %ENV hash e PERL_MAGIC_envelem vtbl_envelem %ENV hash element - f PERL_MAGIC_fm vtbl_fm Formline ('compiled' format) - g PERL_MAGIC_regex_global vtbl_mglob m//g target / study()ed string + f PERL_MAGIC_fm vtbl_regdata Formline ('compiled' format) + G PERL_MAGIC_study vtbl_regdata study()ed string + g PERL_MAGIC_regex_global vtbl_mglob m//g target H PERL_MAGIC_hints vtbl_hints %^H hash h PERL_MAGIC_hintselem vtbl_hintselem %^H hash element I PERL_MAGIC_isa vtbl_isa @ISA array @@ -1045,29 +1064,28 @@ The current kinds of Magic Virtual Tables are: k PERL_MAGIC_nkeys vtbl_nkeys scalar(keys()) lvalue L PERL_MAGIC_dbfile (none) Debugger %_ through the functions C, C, C, and C. Accessing the key as a string through the functions without the C<..._ent> suffix circumvents the -hook. See L for a detailed description. +hook. See L for a detailed description. Note that because multiple extensions may be using C or C magic, it is important for extensions to take @@ -1128,16 +1146,16 @@ objects blessed into the same class as the extension is sufficient. For C magic, it is usually a good idea to define an C, even if all its fields will be C<0>, so that individual C pointers can be identified as a particular kind of magic -using their C field. +using their magic virtual table. C provides an easy way +to do that: STATIC MGVTBL my_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 }; MAGIC *mg; - for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { - if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &my_vtbl) { - /* this is really ours, not another module's PERL_MAGIC_ext */ - my_priv_data_t *priv = (my_priv_data_t *)mg->mg_ptr; - } + if ((mg = mg_findext(sv, PERL_MAGIC_ext, &my_vtbl))) { + /* this is really ours, not another module's PERL_MAGIC_ext */ + my_priv_data_t *priv = (my_priv_data_t *)mg->mg_ptr; + ... } Also note that the C and C functions described @@ -1154,11 +1172,18 @@ since their implementation handles 'get' magic. =head2 Finding Magic - MAGIC* mg_find(SV*, int type); /* Finds the magic pointer of that type */ + MAGIC *mg_find(SV *sv, int type); /* Finds the magic pointer of that type */ + +This routine returns a pointer to a C structure stored in the SV. +If the SV does not have that magical feature, C is returned. If the +SV has multiple instances of that magical feature, the first one will be +returned. C can be used to find a C structure of an SV +based on both it's magic type and it's magic virtual table: + + MAGIC *mg_findext(SV *sv, int type, MGVTBL *vtbl); -This routine returns a pointer to the C structure stored in the SV. -If the SV does not have that magical feature, C is returned. Also, -if the SV is not of type SVt_PVMG, Perl may core dump. +Also, if the SV passed to C or C is not of type +SVt_PVMG, Perl may core dump. int mg_copy(SV* sv, SV* nsv, const char* key, STRLEN klen); @@ -1450,6 +1475,8 @@ L): PUSHs(sv_2mortal(newSVuv(an_unsigned_integer))) PUSHs(sv_2mortal(newSVnv(a_double))) PUSHs(sv_2mortal(newSVpv("Some String",0))) + /* Although the last example is better written as the more efficient: */ + PUSHs(newSVpvs_flags("Some String", SVs_TEMP)) And now the Perl program calling C, the two values will be assigned as in: @@ -2346,7 +2373,7 @@ usually called within the sources as C. This does not need a interpreter context, so the definition has no C, and it follows that callers don't use C. (See -L.) +L.) =item r