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

📄 hash::util::fieldhash.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 3 页
字号:
\&        package Name_id;\&        use Hash::Util::FieldHash qw(id);\&\&        my (%first, %last);\&\&        sub init {\&            my $obj = shift;\&            my ($first, $last) = @_;\&            # create an object if called as class method\&            $obj = bless \e my $o, $obj unless ref $obj;\&            $first{ id $obj} = $first;\&            $last{ id $obj} = $last;\&            $obj;\&        }\&\&        sub first { $first{ id shift()} }\&        sub last { $last{ id shift()} }\&\&        sub name {\&            my $n = shift;\&            join \*(Aq \*(Aq => $n\->first, $n\->last;\&        }\&\&        sub DESTROY {\&            my $id = id shift;\&            delete $first{ $id};\&            delete $last{ $id};\&        }\&\&    }\&\&    {\&        package Name_idhash;\&        use Hash::Util::FieldHash;\&\&        Hash::Util::FieldHash::idhashes( \e my (%first, %last) );\&\&        sub init {\&            my $obj = shift;\&            my ($first, $last) = @_;\&            # create an object if called as class method\&            $obj = bless \e my $o, $obj unless ref $obj;\&            $first{ $obj} = $first;\&            $last{ $obj} = $last;\&            $obj;\&        }\&\&        sub first { $first{ shift()} }\&        sub last { $last{ shift()} }\&\&        sub name {\&            my $n = shift;\&            join \*(Aq \*(Aq => $n\->first, $n\->last;\&        }\&\&        sub DESTROY {\&            my $n = shift;\&            delete $first{ $n};\&            delete $last{ $n};\&        }\&\&    }\&\&    {\&        package Name_id_reg;\&        use Hash::Util::FieldHash qw(id register);\&\&        my (%first, %last);\&\&        sub init {\&            my $obj = shift;\&            my ($first, $last) = @_;\&            # create an object if called as class method\&            $obj = bless \e my $o, $obj unless ref $obj;\&            register( $obj, \e (%first, %last) );\&            $first{ id $obj} = $first;\&            $last{ id $obj} = $last;\&            $obj;\&        }\&\&        sub first { $first{ id shift()} }\&        sub last { $last{ id shift()} }\&\&        sub name {\&            my $n = shift;\&            join \*(Aq \*(Aq => $n\->first, $n\->last;\&        }\&    }\&\&    {\&        package Name_idhash_reg;\&        use Hash::Util::FieldHash qw(register);\&\&        Hash::Util::FieldHash::idhashes \e my (%first, %last);\&\&        sub init {\&            my $obj = shift;\&            my ($first, $last) = @_;\&            # create an object if called as class method\&            $obj = bless \e my $o, $obj unless ref $obj;\&            register( $obj, \e (%first, %last) );\&            $first{ $obj} = $first;\&            $last{ $obj} = $last;\&            $obj;\&        }\&\&        sub first { $first{ shift()} }\&        sub last { $last{ shift()} }\&\&        sub name {\&            my $n = shift;\&            join \*(Aq \*(Aq => $n\->first, $n\->last;\&        }\&    }\&\&    {\&        package Name_fieldhash;\&        use Hash::Util::FieldHash;\&\&        Hash::Util::FieldHash::fieldhashes \e my (%first, %last);\&\&        sub init {\&            my $obj = shift;\&            my ($first, $last) = @_;\&            # create an object if called as class method\&            $obj = bless \e my $o, $obj unless ref $obj;\&            $first{ $obj} = $first;\&            $last{ $obj} = $last;\&            $obj;\&        }\&\&        sub first { $first{ shift()} }\&        sub last { $last{ shift()} }\&\&        sub name {\&            my $n = shift;\&            join \*(Aq \*(Aq => $n\->first, $n\->last;\&        }\&    }\&\&    1;.Ve.PPTo exercise the various implementations the script below canbe used..PPIt sets up a class \f(CW\*(C`Name\*(C'\fR that is a mirror of one of the implementationclasses \f(CW\*(C`Name_hash\*(C'\fR, \f(CW\*(C`Name_id\*(C'\fR, ..., \f(CW\*(C`Name_fieldhash\*(C'\fR.  That determineswhich implementation is run..PPThe script first verifies the function of the \f(CW\*(C`Name\*(C'\fR class..PPIn the second step, the free inheritability of the implementation(or lack thereof) is demonstrated.  For this purpose it constructsa class called \f(CW\*(C`NamedFile\*(C'\fR which is a common subclass of \f(CW\*(C`Name\*(C'\fR andthe standard class \f(CW\*(C`IO::File\*(C'\fR.  This puts inheritability to the testbecause objects of \f(CW\*(C`IO::File\*(C'\fR \fImust\fR be globrefs.  Objects of \f(CW\*(C`NamedFile\*(C'\fRshould behave like a file opened for reading and also support the \f(CW\*(C`name()\*(C'\fRmethod.  This class juncture works with exception of the \f(CW\*(C`Name_hash\*(C'\fRimplementation, where object initialization fails because of theincompatibility of object bodies..Sh "Example 2".IX Subsection "Example 2".Vb 1\&    use strict; use warnings; $| = 1;\&\&    use Example;\&\&    {\&        package Name;\&        use base \*(AqName_id\*(Aq;      # define here which implementation to run\&    }\&\&\&    # Verify that the base package works\&    my $n = Name\->init(qw(Albert Einstein));\&    print $n\->name, "\en";\&    print "\en";\&\&    # Create a named file handle (See definition below)\&    my $nf = NamedFile\->init(qw(/tmp/x Filomena File));\&    # use as a file handle...\&    for ( 1 .. 3 ) {\&        my $l = <$nf>;\&        print "line $_: $l";\&    }\&    # ...and as a Name object\&    print "...brought to you by ", $nf\->name, "\en";\&    exit;\&\&\&    # Definition of NamedFile\&    package NamedFile;\&    use base \*(AqName\*(Aq;\&    use base \*(AqIO::File\*(Aq;\&\&    sub init {\&        my $obj = shift;\&        my ($file, $first, $last) = @_;\&        $obj = $obj\->IO::File::new() unless ref $obj;\&        $obj\->open($file) or die "Can\*(Aqt read \*(Aq$file\*(Aq: $!";\&        $obj\->Name::init($first, $last);\&    }\&    _\|_END_\|_.Ve.SH "GUTS".IX Header "GUTS"To make \f(CW\*(C`Hash::Util::FieldHash\*(C'\fR work, there were two changes to\&\fIperl\fR itself.  \f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR was made avalaible for hashes,and weak references now call uvar \f(CW\*(C`get\*(C'\fR magic after a weakref has beencleared.  The first feature is used to make field hashes intercepttheir keys upon access.  The second one triggers garbage collection..ie n .Sh "The ""PERL_MAGIC_uvar"" interface for hashes".el .Sh "The \f(CWPERL_MAGIC_uvar\fP interface for hashes".IX Subsection "The PERL_MAGIC_uvar interface for hashes"\&\f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR \fIget\fR magic is called from \f(CW\*(C`hv_fetch_common\*(C'\fR and\&\f(CW\*(C`hv_delete_common\*(C'\fR through the function \f(CW\*(C`hv_magic_uvar_xkey\*(C'\fR, whichdefines the interface.  The call happens for hashes with \*(L"uvar\*(R" magicif the \f(CW\*(C`ufuncs\*(C'\fR structure has equal values in the \f(CW\*(C`uf_val\*(C'\fR and \f(CW\*(C`uf_set\*(C'\fRfields.  Hashes are unaffected if (and as long as) these fieldshold different values..PPUpon the call, the \f(CW\*(C`mg_obj\*(C'\fR field will hold the hash key to be accessed.Upon return, the \f(CW\*(C`SV*\*(C'\fR value in \f(CW\*(C`mg_obj\*(C'\fR will be used in place of theoriginal key in the hash access.  The integer index value in the firstparameter will be the \f(CW\*(C`action\*(C'\fR value from \f(CW\*(C`hv_fetch_common\*(C'\fR, or \-1if the call is from \f(CW\*(C`hv_delete_common\*(C'\fR..PPThis is a template for a function suitable for the \f(CW\*(C`uf_val\*(C'\fR field ina \f(CW\*(C`ufuncs\*(C'\fR structure for this call.  The \f(CW\*(C`uf_set\*(C'\fR and \f(CW\*(C`uf_index\*(C'\fRfields are irrelevant..PP.Vb 10\&    IV watch_key(pTHX_ IV action, SV* field) {\&        MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);\&        SV* keysv = mg\->mg_obj;\&        /* Do whatever you need to.  If you decide to\&           supply a different key newkey, return it like this\&        */\&        sv_2mortal(newkey);\&        mg\->mg_obj = newkey;\&        return 0;\&    }.Ve.Sh "Weakrefs call uvar magic".IX Subsection "Weakrefs call uvar magic"When a weak reference is stored in an \f(CW\*(C`SV\*(C'\fR that has \*(L"uvar\*(R" magic, \f(CW\*(C`set\*(C'\fRmagic is called after the reference has gone stale.  This hook can beused to trigger further garbage-collection activities associated withthe referenced object..Sh "How field hashes work".IX Subsection "How field hashes work"The three features of key hashes, \fIkey replacement\fR, \fIthread support\fR,and \fIgarbage collection\fR are supported by a data structure calledthe \fIobject registry\fR.  This is a private hash where every objectis stored.  An \*(L"object\*(R" in this sense is any reference (blessed orunblessed) that has been used as a field hash key..PPThe object registry keeps track of references that have been used asfield hash keys.  The keys are generated from the reference addresslike in a field hash (though the registry isn't a field hash).  Eachvalue is a weak copy of the original reference, stored in an \f(CW\*(C`SV\*(C'\fR thatis itself magical (\f(CW\*(C`PERL_MAGIC_uvar\*(C'\fR again).  The magical structureholds a list (another hash, really) of field hashes that the referencehas been used with.  When the weakref becomes stale, the magic isactivated and uses the list to delete the reference from all fieldhashes it has been used with.  After that, the entry is removed fromthe object registry itself.  Implicitly, that frees the magic structureand the storage it has been using..PPWhenever a reference is used as a field hash key, the object registryis checked and a new entry is made if necessary.  The field hash isthen added to the list of fields this reference has used..PPThe object registry is also used to repair a field hash after threadcloning.  Here, the entire object registry is processed.  For everyreference found there, the field hashes it has used are visited andthe entry is updated..Sh "Internal function Hash::Util::FieldHash::_fieldhash".IX Subsection "Internal function Hash::Util::FieldHash::_fieldhash".Vb 2\&    # test if %hash is a field hash\&    my $result = _fieldhash \e %hash, 0;\&\&    # make %hash a field hash\&    my $result = _fieldhash \e %hash, 1;.Ve.PP\&\f(CW\*(C`_fieldhash\*(C'\fR is the internal function used to create field hashes.It takes two arguments, a hashref and a mode.  If the mode is booleanfalse, the hash is not changed but tested if it is a field hash.  Ifthe hash isn't a field hash the return value is boolean false.  If itis, the return value indicates the mode of field hash.  When called witha boolean true mode, it turns the given hash into a field hash of thismode, returning the mode of the created field hash.  \f(CW\*(C`_fieldhash\*(C'\fRdoes not erase the given hash..PPCurrently there is only one type of field hash, and only the booleanvalue of the mode makes a difference, but that may change..SH "AUTHOR".IX Header "AUTHOR"Anno Siegel (\s-1ANNO\s0) wrote the xs code and the changes in perl properJerry Hedden (\s-1JDHEDDEN\s0) made it faster.SH "COPYRIGHT AND LICENSE".IX Header "COPYRIGHT AND LICENSE"Copyright (C) 2006\-2007 by (Anno Siegel).PPThis library is free software; you can redistribute it and/or modifyit under the same terms as Perl itself, either Perl version 5.8.7 or,at your option, any later version of Perl 5 you may have available.

⌨️ 快捷键说明

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