📄 perlguts.1
字号:
\&\& int (*svt_copy)(SV *sv, MAGIC* mg, SV *nsv, const char *name, int namlen);\& int (*svt_dup)(MAGIC *mg, CLONE_PARAMS *param);\& int (*svt_local)(SV *nsv, MAGIC *mg);.Ve.PPThis \s-1MGVTBL\s0 structure is set at compile-time in \fIperl.h\fR and there arecurrently 19 types (or 21 with overloading turned on). These differentstructures contain pointers to various routines that perform additionalactions depending on which function is being called..PP.Vb 7\& Function pointer Action taken\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\& svt_get Do something before the value of the SV is retrieved.\& svt_set Do something after the SV is assigned a value.\& svt_len Report on the SV\*(Aqs length.\& svt_clear Clear something the SV represents.\& svt_free Free any extra storage associated with the SV.\&\& svt_copy copy tied variable magic to a tied element\& svt_dup duplicate a magic structure during thread cloning\& svt_local copy magic to local value during \*(Aqlocal\*(Aq.Ve.PPFor instance, the \s-1MGVTBL\s0 structure called \f(CW\*(C`vtbl_sv\*(C'\fR (which correspondsto an \f(CW\*(C`mg_type\*(C'\fR of \f(CW\*(C`PERL_MAGIC_sv\*(C'\fR) contains:.PP.Vb 1\& { magic_get, magic_set, magic_len, 0, 0 }.Ve.PPThus, when an \s-1SV\s0 is determined to be magical and of type \f(CW\*(C`PERL_MAGIC_sv\*(C'\fR,if a get operation is being performed, the routine \f(CW\*(C`magic_get\*(C'\fR iscalled. All the various routines for the various magical types beginwith \f(CW\*(C`magic_\*(C'\fR. \s-1NOTE:\s0 the magic routines are not considered part ofthe Perl \s-1API\s0, and may not be exported by the Perl library..PPThe last three slots are a recent addition, and for source codecompatibility they are only checked for if one of the three flagsMGf_COPY, MGf_DUP or MGf_LOCAL is set in mg_flags. This means that mostcode can continue declaring a vtable as a 5\-element value. These three arecurrently used exclusively by the threading code, and are highly subjectto change..PPThe current kinds of Magic Virtual Tables are:.PP.Vb 10\& mg_type\& (old\-style char and macro) MGVTBL Type of magic\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- \-\-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\& \e0 PERL_MAGIC_sv vtbl_sv Special scalar variable\& 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\& (@+ 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 (\*(Aqcompiled\*(Aq format)\& g PERL_MAGIC_regex_global vtbl_mglob m//g target / study()ed string\& H PERL_MAGIC_hints vtbl_sig %^H hash\& h PERL_MAGIC_hintselem vtbl_hintselem %^H hash element\& I PERL_MAGIC_isa vtbl_isa @ISA array\& i PERL_MAGIC_isaelem vtbl_isaelem @ISA array element\& k PERL_MAGIC_nkeys vtbl_nkeys scalar(keys()) lvalue\& L PERL_MAGIC_dbfile (none) Debugger %_<filename\& l PERL_MAGIC_dbline vtbl_dbline Debugger %_<filename element\& o PERL_MAGIC_collxfrm vtbl_collxfrm Locale collate transformation\& P PERL_MAGIC_tied vtbl_pack Tied array or hash\& p PERL_MAGIC_tiedelem vtbl_packelem Tied array or hash element\& q PERL_MAGIC_tiedscalar vtbl_packelem Tied scalar or handle\& r PERL_MAGIC_qr vtbl_qr precompiled qr// regex\& S PERL_MAGIC_sig vtbl_sig %SIG hash\& s PERL_MAGIC_sigelem vtbl_sigelem %SIG hash element\& t PERL_MAGIC_taint vtbl_taint Taintedness\& U PERL_MAGIC_uvar vtbl_uvar Available for use by extensions\& v PERL_MAGIC_vec vtbl_vec vec() lvalue\& V PERL_MAGIC_vstring (none) v\-string scalars\& w PERL_MAGIC_utf8 vtbl_utf8 UTF\-8 length+offset cache\& x PERL_MAGIC_substr vtbl_substr substr() lvalue\& y PERL_MAGIC_defelem vtbl_defelem Shadow "foreach" iterator\& variable / smart parameter\& vivification\& # PERL_MAGIC_arylen vtbl_arylen Array length ($#ary)\& . PERL_MAGIC_pos vtbl_pos pos() lvalue\& < PERL_MAGIC_backref vtbl_backref back pointer to a weak ref \& ~ PERL_MAGIC_ext (none) Available for use by extensions\& : PERL_MAGIC_symtab (none) hash used as symbol table\& % PERL_MAGIC_rhash (none) hash used as restricted hash\& @ PERL_MAGIC_arylen_p vtbl_arylen_p pointer to $#a from @a.Ve.PPWhen an uppercase and lowercase letter both exist in the table, then theuppercase letter is typically used to represent some kind of composite type(a list or a hash), and the lowercase letter is used to represent an elementof that composite type. Some internals code makes use of this caserelationship. However, 'v' and 'V' (vec and v\-string) are in no way related..PPThe \f(CW\*(C`PERL_MAGIC_ext\*(C'\fR and \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR magic types are definedspecifically for use by extensions and will not be used by perl itself.Extensions can use \f(CW\*(C`PERL_MAGIC_ext\*(C'\fR magic to 'attach' private informationto variables (typically objects). This is especially useful becausethere is no way for normal perl code to corrupt this private information(unlike using extra elements of a hash object)..PPSimilarly, \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR magic can be used much like \fItie()\fR to call aC function any time a scalar's value is used or changed. The \f(CW\*(C`MAGIC\*(C'\fR's\&\f(CW\*(C`mg_ptr\*(C'\fR field points to a \f(CW\*(C`ufuncs\*(C'\fR structure:.PP.Vb 5\& struct ufuncs {\& I32 (*uf_val)(pTHX_ IV, SV*);\& I32 (*uf_set)(pTHX_ IV, SV*);\& IV uf_index;\& };.Ve.PPWhen the \s-1SV\s0 is read from or written to, the \f(CW\*(C`uf_val\*(C'\fR or \f(CW\*(C`uf_set\*(C'\fRfunction will be called with \f(CW\*(C`uf_index\*(C'\fR as the first arg and a pointer tothe \s-1SV\s0 as the second. A simple example of how to add \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fRmagic is shown below. Note that the ufuncs structure is copied bysv_magic, so you can safely allocate it on the stack..PP.Vb 10\& void\& Umagic(sv)\& SV *sv;\& PREINIT:\& struct ufuncs uf;\& CODE:\& uf.uf_val = &my_get_fn;\& uf.uf_set = &my_set_fn;\& uf.uf_index = 0;\& sv_magic(sv, 0, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));.Ve.PPAttaching \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR to arrays is permissible but has no effect..PPFor hashes there is a specialized hook that gives control over hashkeys (but not values). This hook calls \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR 'get' magicif the \*(L"set\*(R" function in the \f(CW\*(C`ufuncs\*(C'\fR structure is \s-1NULL\s0. The hookis activated whenever the hash is accessed with a key specified asan \f(CW\*(C`SV\*(C'\fR through the functions \f(CW\*(C`hv_store_ent\*(C'\fR, \f(CW\*(C`hv_fetch_ent\*(C'\fR,\&\f(CW\*(C`hv_delete_ent\*(C'\fR, and \f(CW\*(C`hv_exists_ent\*(C'\fR. Accessing the key as a stringthrough the functions without the \f(CW\*(C`..._ent\*(C'\fR suffix circumvents thehook. See \*(L"Guts\*(R" in Hash::Util::Fieldhash for a detailed description..PPNote that because multiple extensions may be using \f(CW\*(C`PERL_MAGIC_ext\*(C'\fRor \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR magic, it is important for extensions to takeextra care to avoid conflict. Typically only using the magic onobjects blessed into the same class as the extension is sufficient.For \f(CW\*(C`PERL_MAGIC_ext\*(C'\fR magic, it may also be appropriate to add an I32\&'signature' at the top of the private data area and check that..PPAlso note that the \f(CW\*(C`sv_set*()\*(C'\fR and \f(CW\*(C`sv_cat*()\*(C'\fR functions describedearlier do \fBnot\fR invoke 'set' magic on their targets. This mustbe done by the user either by calling the \f(CW\*(C`SvSETMAGIC()\*(C'\fR macro aftercalling these functions, or by using one of the \f(CW\*(C`sv_set*_mg()\*(C'\fR or\&\f(CW\*(C`sv_cat*_mg()\*(C'\fR functions. Similarly, generic C code must call the\&\f(CW\*(C`SvGETMAGIC()\*(C'\fR macro to invoke any 'get' magic if they use an \s-1SV\s0obtained from external sources in functions that don't handle magic.See perlapi for a description of these functions.For example, calls to the \f(CW\*(C`sv_cat*()\*(C'\fR functions typically need to befollowed by \f(CW\*(C`SvSETMAGIC()\*(C'\fR, but they don't need a prior \f(CW\*(C`SvGETMAGIC()\*(C'\fRsince their implementation handles 'get' magic..Sh "Finding Magic".IX Subsection "Finding Magic".Vb 1\& MAGIC* mg_find(SV*, int type); /* Finds the magic pointer of that type */.Ve.PPThis routine returns a pointer to the \f(CW\*(C`MAGIC\*(C'\fR structure stored in the \s-1SV\s0.If the \s-1SV\s0 does not have that magical feature, \f(CW\*(C`NULL\*(C'\fR is returned. Also,if the \s-1SV\s0 is not of type SVt_PVMG, Perl may core dump..PP.Vb 1\& int mg_copy(SV* sv, SV* nsv, const char* key, STRLEN klen);.Ve.PPThis routine checks to see what types of magic \f(CW\*(C`sv\*(C'\fR has. If the mg_typefield is an uppercase letter, then the mg_obj is copied to \f(CW\*(C`nsv\*(C'\fR, butthe mg_type field is changed to be the lowercase letter..Sh "Understanding the Magic of Tied Hashes and Arrays".IX Subsection "Understanding the Magic of Tied Hashes and Arrays"Tied hashes and arrays are magical beasts of the \f(CW\*(C`PERL_MAGIC_tied\*(C'\fRmagic type..PP\&\s-1WARNING:\s0 As of the 5.004 release, proper usage of the array and hashaccess functions requires understanding a few caveats. Someof these caveats are actually considered bugs in the \s-1API\s0, to be fixedin later releases, and are bracketed with [\s-1MAYCHANGE\s0] below. Ifyou find yourself actually applying such information in this section, beaware that the behavior may change in the future, umm, without warning..PPThe perl tie function associates a variable with an object that implementsthe various \s-1GET\s0, \s-1SET\s0, etc methods. To perform the equivalent of the perltie function from an \s-1XSUB\s0, you must mimic this behaviour. The code belowcarries out the necessary steps \- firstly it creates a new hash, and thencreates a second hash which it blesses into the class which will implementthe tie methods. Lastly it ties the two hashes together, and returns areference to the new tied hash. Note that the code below does \s-1NOT\s0 call the\&\s-1TIEHASH\s0 method in the MyTie class \-see \*(L"Calling Perl Routines from within C Programs\*(R" for details on howto do this..PP.Vb 10\& SV*\& mytie()\& PREINIT:\& HV *hash;\& HV *stash;\& SV *tie;\& CODE:\& hash = newHV();\& tie = newRV_noinc((SV*)newHV());\& stash = gv_stashpv("MyTie", GV_ADD);\& sv_bless(tie, stash);\& hv_magic(hash, (GV*)tie, PERL_MAGIC_tied);\& RETVAL = newRV_noinc(hash);\& OUTPUT:\& RETVAL.Ve.PPThe \f(CW\*(C`av_store\*(C'\fR function, when given a tied array argument, merelycopies the magic of the array onto the value to be \*(L"stored\*(R", using\&\f(CW\*(C`mg_copy\*(C'\fR. It may also return \s-1NULL\s0, indicating that the value did notactually need to be stored in the array. [\s-1MAYCHANGE\s0] After a call to\&\f(CW\*(C`av_store\*(C'\fR on a tied array, the caller will usually need to call\&\f(CW\*(C`mg_set(val)\*(C'\fR to actually invoke the perl level \*(L"\s-1STORE\s0\*(R" method on the\&\s-1TIEARRAY\s0 object. If \f(CW\*(C`av_store\*(C'\fR did return \s-1NULL\s0, a call to\&\f(CW\*(C`SvREFCNT_dec(val)\*(C'\fR will also be usually necessary to avoid a memoryleak. [/MAYCHANGE].PPThe previous paragraph is applicable verbatim to tied hash access using the\&\f(CW\*(C`hv_store\*(C'\fR and \f(CW\*(C`hv_store_ent\*(C'\fR functions as well..PP\&\f(CW\*(C`av_fetch\*(C'\fR and the corresponding hash functions \f(CW\*(C`hv_fetch\*(C'\fR and\&\f(CW\*(C`hv_fetch_ent\*(C'\fR actually return an undefined mortal value whose magichas been initialized using \f(CW\*(C`mg_copy\*(C'\fR. Note the value so returned does notneed to be deallocated, as it is already mortal. [\s-1MAYCHANGE\s0] But you willneed to call \f(CW\*(C`mg_get()\*(C'\fR on the returned value in order to actually invokethe perl level \*(L"\s-1FETCH\s0\*(R" method on the underlying \s-1TIE\s0 object. Similarly,you may also call \f(CW\*(C`mg_set()\*(C'\fR on the return value after possibly assigninga suitable value to it using \f(CW\*(C`sv_setsv\*(C'\fR, which will invoke the \*(L"\s-1STORE\s0\*(R"method on the \s-1TIE\s0 object. [/MAYCHANGE].PP[\s-1MAYCHANGE\s0]In other words, the array or hash fetch/store functions don't reallyfetch and store actual values in the case of tied arrays and hashes. Theymerely call \f(CW\*(C`mg_copy\*(C'\fR to attach magic to the values that were meant to be\&\*(L"stored\*(R" or \*(L"fetched\*(R". Later calls to \f(CW\*(C`mg_get\*(C'\fR and \f(CW\*(C`mg_set\*(C'\fR actuallydo the job of invoking the \s-1TIE\s0 methods on the underlying objects. Thusthe magic mechanism currently implements a kind of lazy access to arraysand hashes..PPCurrently (as of perl version 5.004), use of the hash and array accessfunctions requires the user to be aware of whether they are operating on\&\*(L"normal\*(R" hashes and arrays, or on their tied variants. The \s-1API\s0 may bechanged to provide more transparent access to both tied and normal datatypes in future versions.[/MAYCHANGE].PPYou would do well to understand that the \s-1TIEARRAY\s0 and \s-1TIEHASH\s0 interfacesare mere sugar to invoke some perl method calls while using the uniform hashand array syntax. The use of this sugar imposes some overhead (typicallyabout two to four extra opcodes per \s-1FETCH/STORE\s0 operation, in addition tothe creation of all the mortal variables required to invoke the methods).This overhead will be comparatively small if the \s-1TIE\s0 methods are themselvessubstantial, but if they are only a few statements long, the overheadwill not be insignificant..Sh "Localizing changes".IX Subsection "Localizing changes"Perl has a very handy construction.PP.Vb 4\& {\& local $var = 2;\& ...\& }.Ve.PPThis construction is \fIapproximately\fR equivalent to.PP.Vb 6\& {\& my $oldvar = $var;\& $var = 2;\& ...\& $var = $oldvar;\& }.Ve.PPThe biggest difference is that the first construction wouldreinstate the initial value of \f(CW$var\fR, irrespective of how control exitsthe block: \f(CW\*(C`goto\*(C'\fR, \f(CW\*(C`return\*(C'\fR, \f(CW\*(C`die\*(C'\fR/\f(CW\*(C`eval\*(C'\fR, etc. It is a little bitmore efficient as well..PPThere is a way to achieve a similar task from C via Perl \s-1API:\s0 create a\&\fIpseudo-block\fR, and arrange for some changes to be automaticallyundone at the end of it, either explicit, or via a non-local exit (via\&\fIdie()\fR). A \fIblock\fR\-like construct is created by a pair of\&\f(CW\*(C`ENTER\*(C'\fR/\f(CW\*(C`LEAVE\*(C'\fR macros (see \*(L"Returning a Scalar\*(R" in perlcall).Such a construct may be created specially for some important
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -