⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlguts.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 5 页
字号:
\&  Had to create <varname> unexpectedly.Ve.Spif the variable did not exist before the function was called..PPIf you do not specify a package name, the variable is created in the currentpackage..Sh "Reference Counts and Mortality".IX Subsection "Reference Counts and Mortality"Perl uses a reference count-driven garbage collection mechanism. SVs,AVs, or HVs (xV for short in the following) start their life with areference count of 1.  If the reference count of an xV ever drops to 0,then it will be destroyed and its memory made available for reuse..PPThis normally doesn't happen at the Perl level unless a variable isundef'ed or the last variable holding a reference to it is changed oroverwritten.  At the internal level, however, reference counts can bemanipulated with the following macros:.PP.Vb 3\&    int SvREFCNT(SV* sv);\&    SV* SvREFCNT_inc(SV* sv);\&    void SvREFCNT_dec(SV* sv);.Ve.PPHowever, there is one other function which manipulates the referencecount of its argument.  The \f(CW\*(C`newRV_inc\*(C'\fR function, you will recall,creates a reference to the specified argument.  As a side effect,it increments the argument's reference count.  If this is not whatyou want, use \f(CW\*(C`newRV_noinc\*(C'\fR instead..PPFor example, imagine you want to return a reference from an \s-1XSUB\s0 function.Inside the \s-1XSUB\s0 routine, you create an \s-1SV\s0 which initially has a referencecount of one.  Then you call \f(CW\*(C`newRV_inc\*(C'\fR, passing it the just-created \s-1SV\s0.This returns the reference as a new \s-1SV\s0, but the reference count of the\&\s-1SV\s0 you passed to \f(CW\*(C`newRV_inc\*(C'\fR has been incremented to two.  Now youreturn the reference from the \s-1XSUB\s0 routine and forget about the \s-1SV\s0.But Perl hasn't!  Whenever the returned reference is destroyed, thereference count of the original \s-1SV\s0 is decreased to one and nothing happens.The \s-1SV\s0 will hang around without any way to access it until Perl itselfterminates.  This is a memory leak..PPThe correct procedure, then, is to use \f(CW\*(C`newRV_noinc\*(C'\fR instead of\&\f(CW\*(C`newRV_inc\*(C'\fR.  Then, if and when the last reference is destroyed,the reference count of the \s-1SV\s0 will go to zero and it will be destroyed,stopping any memory leak..PPThere are some convenience functions available that can help with thedestruction of xVs.  These functions introduce the concept of \*(L"mortality\*(R".An xV that is mortal has had its reference count marked to be decremented,but not actually decremented, until \*(L"a short time later\*(R".  Generally theterm \*(L"short time later\*(R" means a single Perl statement, such as a call toan \s-1XSUB\s0 function.  The actual determinant for when mortal xVs have theirreference count decremented depends on two macros, \s-1SAVETMPS\s0 and \s-1FREETMPS\s0.See perlcall and perlxs for more details on these macros..PP\&\*(L"Mortalization\*(R" then is at its simplest a deferred \f(CW\*(C`SvREFCNT_dec\*(C'\fR.However, if you mortalize a variable twice, the reference count willlater be decremented twice..PP\&\*(L"Mortal\*(R" SVs are mainly used for SVs that are placed on perl's stack.For example an \s-1SV\s0 which is created just to pass a number to a called subis made mortal to have it cleaned up automatically when it's popped offthe stack. Similarly, results returned by XSUBs (which are pushed on thestack) are often made mortal..PPTo create a mortal variable, use the functions:.PP.Vb 3\&    SV*  sv_newmortal()\&    SV*  sv_2mortal(SV*)\&    SV*  sv_mortalcopy(SV*).Ve.PPThe first call creates a mortal \s-1SV\s0 (with no value), the second converts an existing\&\s-1SV\s0 to a mortal \s-1SV\s0 (and thus defers a call to \f(CW\*(C`SvREFCNT_dec\*(C'\fR), and thethird creates a mortal copy of an existing \s-1SV\s0.Because \f(CW\*(C`sv_newmortal\*(C'\fR gives the new \s-1SV\s0 no value,it must normally be given onevia \f(CW\*(C`sv_setpv\*(C'\fR, \f(CW\*(C`sv_setiv\*(C'\fR, etc. :.PP.Vb 2\&    SV *tmp = sv_newmortal();\&    sv_setiv(tmp, an_integer);.Ve.PPAs that is multiple C statements it is quite common so see this idiom instead:.PP.Vb 1\&    SV *tmp = sv_2mortal(newSViv(an_integer));.Ve.PPYou should be careful about creating mortal variables.  Strange thingscan happen if you make the same value mortal within multiple contexts,or if you make a variable mortal multiple times. Thinking of \*(L"Mortalization\*(R"as deferred \f(CW\*(C`SvREFCNT_dec\*(C'\fR should help to minimize such problems.For example if you are passing an \s-1SV\s0 which you \fIknow\fR has high enough \s-1REFCNT\s0to survive its use on the stack you need not do any mortalization.If you are not sure then doing an \f(CW\*(C`SvREFCNT_inc\*(C'\fR and \f(CW\*(C`sv_2mortal\*(C'\fR, ormaking a \f(CW\*(C`sv_mortalcopy\*(C'\fR is safer..PPThe mortal routines are not just for SVs \*(-- AVs and HVs can bemade mortal by passing their address (type-casted to \f(CW\*(C`SV*\*(C'\fR) to the\&\f(CW\*(C`sv_2mortal\*(C'\fR or \f(CW\*(C`sv_mortalcopy\*(C'\fR routines..Sh "Stashes and Globs".IX Subsection "Stashes and Globs"A \fBstash\fR is a hash that contains all variables that are definedwithin a package.  Each key of the stash is a symbolname (shared by all the different types of objects that have the samename), and each value in the hash table is a \s-1GV\s0 (Glob Value).  This \s-1GV\s0in turn contains references to the various objects of that name,including (but not limited to) the following:.PP.Vb 6\&    Scalar Value\&    Array Value\&    Hash Value\&    I/O Handle\&    Format\&    Subroutine.Ve.PPThere is a single stash called \f(CW\*(C`PL_defstash\*(C'\fR that holds the items that existin the \f(CW\*(C`main\*(C'\fR package.  To get at the items in other packages, append thestring \*(L"::\*(R" to the package name.  The items in the \f(CW\*(C`Foo\*(C'\fR package are inthe stash \f(CW\*(C`Foo::\*(C'\fR in PL_defstash.  The items in the \f(CW\*(C`Bar::Baz\*(C'\fR package arein the stash \f(CW\*(C`Baz::\*(C'\fR in \f(CW\*(C`Bar::\*(C'\fR's stash..PPTo get the stash pointer for a particular package, use the function:.PP.Vb 2\&    HV*  gv_stashpv(const char* name, I32 flags)\&    HV*  gv_stashsv(SV*, I32 flags).Ve.PPThe first function takes a literal string, the second uses the string storedin the \s-1SV\s0.  Remember that a stash is just a hash table, so you get back an\&\f(CW\*(C`HV*\*(C'\fR.  The \f(CW\*(C`flags\*(C'\fR flag will create a new package if it is set to \s-1GV_ADD\s0..PPThe name that \f(CW\*(C`gv_stash*v\*(C'\fR wants is the name of the package whose symbol tableyou want.  The default package is called \f(CW\*(C`main\*(C'\fR.  If you have multiply nestedpackages, pass their names to \f(CW\*(C`gv_stash*v\*(C'\fR, separated by \f(CW\*(C`::\*(C'\fR as in the Perllanguage itself..PPAlternately, if you have an \s-1SV\s0 that is a blessed reference, you can findout the stash pointer by using:.PP.Vb 1\&    HV*  SvSTASH(SvRV(SV*));.Ve.PPthen use the following to get the package name itself:.PP.Vb 1\&    char*  HvNAME(HV* stash);.Ve.PPIf you need to bless or re-bless an object you can use the followingfunction:.PP.Vb 1\&    SV*  sv_bless(SV*, HV* stash).Ve.PPwhere the first argument, an \f(CW\*(C`SV*\*(C'\fR, must be a reference, and the secondargument is a stash.  The returned \f(CW\*(C`SV*\*(C'\fR can now be used in the same wayas any other \s-1SV\s0..PPFor more information on references and blessings, consult perlref..Sh "Double-Typed SVs".IX Subsection "Double-Typed SVs"Scalar variables normally contain only one type of value, an integer,double, pointer, or reference.  Perl will automatically convert theactual scalar data from the stored type into the requested type..PPSome scalar variables contain more than one type of scalar data.  Forexample, the variable \f(CW$!\fR contains either the numeric value of \f(CW\*(C`errno\*(C'\fRor its string equivalent from either \f(CW\*(C`strerror\*(C'\fR or \f(CW\*(C`sys_errlist[]\*(C'\fR..PPTo force multiple data values into an \s-1SV\s0, you must do two things: use the\&\f(CW\*(C`sv_set*v\*(C'\fR routines to add the additional scalar type, then set a flagso that Perl will believe it contains more than one type of data.  Thefour macros to set the flags are:.PP.Vb 4\&        SvIOK_on\&        SvNOK_on\&        SvPOK_on\&        SvROK_on.Ve.PPThe particular macro you must use depends on which \f(CW\*(C`sv_set*v\*(C'\fR routineyou called first.  This is because every \f(CW\*(C`sv_set*v\*(C'\fR routine turns ononly the bit for the particular type of data being set, and turns offall the rest..PPFor example, to create a new Perl variable called \*(L"dberror\*(R" that containsboth the numeric and descriptive string error values, you could use thefollowing code:.PP.Vb 2\&    extern int  dberror;\&    extern char *dberror_list;\&\&    SV* sv = get_sv("dberror", TRUE);\&    sv_setiv(sv, (IV) dberror);\&    sv_setpv(sv, dberror_list[dberror]);\&    SvIOK_on(sv);.Ve.PPIf the order of \f(CW\*(C`sv_setiv\*(C'\fR and \f(CW\*(C`sv_setpv\*(C'\fR had been reversed, then themacro \f(CW\*(C`SvPOK_on\*(C'\fR would need to be called instead of \f(CW\*(C`SvIOK_on\*(C'\fR..Sh "Magic Variables".IX Subsection "Magic Variables"[This section still under construction.  Ignore everything here.  Post nobills.  Everything not permitted is forbidden.].PPAny \s-1SV\s0 may be magical, that is, it has special features that a normal\&\s-1SV\s0 does not have.  These features are stored in the \s-1SV\s0 structure in alinked list of \f(CW\*(C`struct magic\*(C'\fR's, typedef'ed to \f(CW\*(C`MAGIC\*(C'\fR..PP.Vb 10\&    struct magic {\&        MAGIC*      mg_moremagic;\&        MGVTBL*     mg_virtual;\&        U16         mg_private;\&        char        mg_type;\&        U8          mg_flags;\&        I32         mg_len;\&        SV*         mg_obj;\&        char*       mg_ptr;\&    };.Ve.PPNote this is current as of patchlevel 0, and could change at any time..Sh "Assigning Magic".IX Subsection "Assigning Magic"Perl adds magic to an \s-1SV\s0 using the sv_magic function:.PP.Vb 1\&    void sv_magic(SV* sv, SV* obj, int how, const char* name, I32 namlen);.Ve.PPThe \f(CW\*(C`sv\*(C'\fR argument is a pointer to the \s-1SV\s0 that is to acquire a new magicalfeature..PPIf \f(CW\*(C`sv\*(C'\fR is not already magical, Perl uses the \f(CW\*(C`SvUPGRADE\*(C'\fR macro toconvert \f(CW\*(C`sv\*(C'\fR to type \f(CW\*(C`SVt_PVMG\*(C'\fR. Perl then continues by adding new magicto the beginning of the linked list of magical features.  Any prior entryof the same type of magic is deleted.  Note that this can be overridden,and multiple instances of the same type of magic can be associated with an\&\s-1SV\s0..PPThe \f(CW\*(C`name\*(C'\fR and \f(CW\*(C`namlen\*(C'\fR arguments are used to associate a string withthe magic, typically the name of a variable. \f(CW\*(C`namlen\*(C'\fR is stored in the\&\f(CW\*(C`mg_len\*(C'\fR field and if \f(CW\*(C`name\*(C'\fR is non-null then either a \f(CW\*(C`savepvn\*(C'\fR copy of\&\f(CW\*(C`name\*(C'\fR or \f(CW\*(C`name\*(C'\fR itself is stored in the \f(CW\*(C`mg_ptr\*(C'\fR field, depending onwhether \f(CW\*(C`namlen\*(C'\fR is greater than zero or equal to zero respectively.  As aspecial case, if \f(CW\*(C`(name && namlen == HEf_SVKEY)\*(C'\fR then \f(CW\*(C`name\*(C'\fR is assumedto contain an \f(CW\*(C`SV*\*(C'\fR and is stored as-is with its \s-1REFCNT\s0 incremented..PPThe sv_magic function uses \f(CW\*(C`how\*(C'\fR to determine which, if any, predefined\&\*(L"Magic Virtual Table\*(R" should be assigned to the \f(CW\*(C`mg_virtual\*(C'\fR field.See the \*(L"Magic Virtual Tables\*(R" section below.  The \f(CW\*(C`how\*(C'\fR argument is alsostored in the \f(CW\*(C`mg_type\*(C'\fR field. The value of \f(CW\*(C`how\*(C'\fR should be chosenfrom the set of macros \f(CW\*(C`PERL_MAGIC_foo\*(C'\fR found in \fIperl.h\fR. Note that beforethese macros were added, Perl internals used to directly use characterliterals, so you may occasionally come across old code or documentationreferring to 'U' magic rather than \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR for example..PPThe \f(CW\*(C`obj\*(C'\fR argument is stored in the \f(CW\*(C`mg_obj\*(C'\fR field of the \f(CW\*(C`MAGIC\*(C'\fRstructure.  If it is not the same as the \f(CW\*(C`sv\*(C'\fR argument, the referencecount of the \f(CW\*(C`obj\*(C'\fR object is incremented.  If it is the same, or ifthe \f(CW\*(C`how\*(C'\fR argument is \f(CW\*(C`PERL_MAGIC_arylen\*(C'\fR, or if it is a \s-1NULL\s0 pointer,then \f(CW\*(C`obj\*(C'\fR is merely stored, without the reference count being incremented..PPSee also \f(CW\*(C`sv_magicext\*(C'\fR in perlapi for a more flexible way to add magicto an \s-1SV\s0..PPThere is also a function to add magic to an \f(CW\*(C`HV\*(C'\fR:.PP.Vb 1\&    void hv_magic(HV *hv, GV *gv, int how);.Ve.PPThis simply calls \f(CW\*(C`sv_magic\*(C'\fR and coerces the \f(CW\*(C`gv\*(C'\fR argument into an \f(CW\*(C`SV\*(C'\fR..PPTo remove the magic from an \s-1SV\s0, call the function sv_unmagic:.PP.Vb 1\&    void sv_unmagic(SV *sv, int type);.Ve.PPThe \f(CW\*(C`type\*(C'\fR argument should be equal to the \f(CW\*(C`how\*(C'\fR value when the \f(CW\*(C`SV\*(C'\fRwas initially made magical..Sh "Magic Virtual Tables".IX Subsection "Magic Virtual Tables"The \f(CW\*(C`mg_virtual\*(C'\fR field in the \f(CW\*(C`MAGIC\*(C'\fR structure is a pointer to an\&\f(CW\*(C`MGVTBL\*(C'\fR, which is a structure of function pointers and stands for\&\*(L"Magic Virtual Table\*(R" to handle the various operations that might beapplied to that variable..PPThe \f(CW\*(C`MGVTBL\*(C'\fR has five (or sometimes eight) pointers to the followingroutine types:.PP.Vb 5\&    int  (*svt_get)(SV* sv, MAGIC* mg);\&    int  (*svt_set)(SV* sv, MAGIC* mg);\&    U32  (*svt_len)(SV* sv, MAGIC* mg);\&    int  (*svt_clear)(SV* sv, MAGIC* mg);\&    int  (*svt_free)(SV* sv, MAGIC* mg);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -