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

📄 perltie.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 4 页
字号:
is to operate on the tied array. The \fBTie::Array\fR class can be used as abase class to implement the first five of these in terms of the basicmethods above.  The default implementations of \s-1DELETE\s0 and \s-1EXISTS\s0 in\&\fBTie::Array\fR simply \f(CW\*(C`croak\*(C'\fR..PPIn addition \s-1EXTEND\s0 will be called when perl would have pre-extendedallocation in a real array..PPFor this discussion, we'll implement an array whose elements are a fixedsize at creation.  If you try to create an element larger than the fixedsize, you'll take an exception.  For example:.PP.Vb 4\&    use FixedElem_Array;\&    tie @array, \*(AqFixedElem_Array\*(Aq, 3;\&    $array[0] = \*(Aqcat\*(Aq;  # ok.\&    $array[1] = \*(Aqdogs\*(Aq; # exception, length(\*(Aqdogs\*(Aq) > 3..Ve.PPThe preamble code for the class is as follows:.PP.Vb 3\&    package FixedElem_Array;\&    use Carp;\&    use strict;.Ve.IP "\s-1TIEARRAY\s0 classname, \s-1LIST\s0" 4.IX Xref "TIEARRAY".IX Item "TIEARRAY classname, LIST"This is the constructor for the class.  That means it is expected toreturn a blessed reference through which the new array (probably ananonymous \s-1ARRAY\s0 ref) will be accessed..SpIn our example, just to show you that you don't \fIreally\fR have to return an\&\s-1ARRAY\s0 reference, we'll choose a \s-1HASH\s0 reference to represent our object.A \s-1HASH\s0 works out well as a generic record type: the \f(CW\*(C`{ELEMSIZE}\*(C'\fR field willstore the maximum element size allowed, and the \f(CW\*(C`{ARRAY}\*(C'\fR field will hold thetrue \s-1ARRAY\s0 ref.  If someone outside the class tries to dereference theobject returned (doubtless thinking it an \s-1ARRAY\s0 ref), they'll blow up.This just goes to show you that you should respect an object's privacy..Sp.Vb 11\&    sub TIEARRAY {\&      my $class    = shift;\&      my $elemsize = shift;\&      if ( @_ || $elemsize =~ /\eD/ ) {\&        croak "usage: tie ARRAY, \*(Aq" . _\|_PACKAGE_\|_ . "\*(Aq, elem_size";\&      }\&      return bless {\&        ELEMSIZE => $elemsize,\&        ARRAY    => [],\&      }, $class;\&    }.Ve.IP "\s-1FETCH\s0 this, index" 4.IX Xref "FETCH".IX Item "FETCH this, index"This method will be triggered every time an individual element the tied arrayis accessed (read).  It takes one argument beyond its self reference: theindex whose value we're trying to fetch..Sp.Vb 5\&    sub FETCH {\&      my $self  = shift;\&      my $index = shift;\&      return $self\->{ARRAY}\->[$index];\&    }.Ve.SpIf a negative array index is used to read from an array, the indexwill be translated to a positive one internally by calling \s-1FETCHSIZE\s0before being passed to \s-1FETCH\s0.  You may disable this feature byassigning a true value to the variable \f(CW$NEGATIVE_INDICES\fR in thetied array class..SpAs you may have noticed, the name of the \s-1FETCH\s0 method (et al.) is the samefor all accesses, even though the constructors differ in names (\s-1TIESCALAR\s0vs \s-1TIEARRAY\s0).  While in theory you could have the same class servicingseveral tied types, in practice this becomes cumbersome, and it's easiestto keep them at simply one tie type per class..IP "\s-1STORE\s0 this, index, value" 4.IX Xref "STORE".IX Item "STORE this, index, value"This method will be triggered every time an element in the tied array is set(written).  It takes two arguments beyond its self reference: the index atwhich we're trying to store something and the value we're trying to putthere..SpIn our example, \f(CW\*(C`undef\*(C'\fR is really \f(CW\*(C`$self\->{ELEMSIZE}\*(C'\fR number ofspaces so we have a little more work to do here:.Sp.Vb 11\&    sub STORE {\&      my $self = shift;\&      my( $index, $value ) = @_;\&      if ( length $value > $self\->{ELEMSIZE} ) {\&        croak "length of $value is greater than $self\->{ELEMSIZE}";\&      }\&      # fill in the blanks\&      $self\->EXTEND( $index ) if $index > $self\->FETCHSIZE();\&      # right justify to keep element size for smaller elements\&      $self\->{ARRAY}\->[$index] = sprintf "%$self\->{ELEMSIZE}s", $value;\&    }.Ve.SpNegative indexes are treated the same as with \s-1FETCH\s0..IP "\s-1FETCHSIZE\s0 this" 4.IX Xref "FETCHSIZE".IX Item "FETCHSIZE this"Returns the total number of items in the tied array associated withobject \fIthis\fR. (Equivalent to \f(CW\*(C`scalar(@array)\*(C'\fR).  For example:.Sp.Vb 4\&    sub FETCHSIZE {\&      my $self = shift;\&      return scalar @{$self\->{ARRAY}};\&    }.Ve.IP "\s-1STORESIZE\s0 this, count" 4.IX Xref "STORESIZE".IX Item "STORESIZE this, count"Sets the total number of items in the tied array associated withobject \fIthis\fR to be \fIcount\fR. If this makes the array larger thenclass's mapping of \f(CW\*(C`undef\*(C'\fR should be returned for new positions.If the array becomes smaller then entries beyond count should bedeleted..SpIn our example, 'undef' is really an element containing\&\f(CW\*(C`$self\->{ELEMSIZE}\*(C'\fR number of spaces.  Observe:.Sp.Vb 10\&    sub STORESIZE {\&      my $self  = shift;\&      my $count = shift;\&      if ( $count > $self\->FETCHSIZE() ) {\&        foreach ( $count \- $self\->FETCHSIZE() .. $count ) {\&          $self\->STORE( $_, \*(Aq\*(Aq );\&        }\&      } elsif ( $count < $self\->FETCHSIZE() ) {\&        foreach ( 0 .. $self\->FETCHSIZE() \- $count \- 2 ) {\&          $self\->POP();\&        }\&      }\&    }.Ve.IP "\s-1EXTEND\s0 this, count" 4.IX Xref "EXTEND".IX Item "EXTEND this, count"Informative call that array is likely to grow to have \fIcount\fR entries.Can be used to optimize allocation. This method need do nothing..SpIn our example, we want to make sure there are no blank (\f(CW\*(C`undef\*(C'\fR)entries, so \f(CW\*(C`EXTEND\*(C'\fR will make use of \f(CW\*(C`STORESIZE\*(C'\fR to fill elementsas needed:.Sp.Vb 5\&    sub EXTEND {   \&      my $self  = shift;\&      my $count = shift;\&      $self\->STORESIZE( $count );\&    }.Ve.IP "\s-1EXISTS\s0 this, key" 4.IX Xref "EXISTS".IX Item "EXISTS this, key"Verify that the element at index \fIkey\fR exists in the tied array \fIthis\fR..SpIn our example, we will determine that if an element consists of\&\f(CW\*(C`$self\->{ELEMSIZE}\*(C'\fR spaces only, it does not exist:.Sp.Vb 7\&    sub EXISTS {\&      my $self  = shift;\&      my $index = shift;\&      return 0 if ! defined $self\->{ARRAY}\->[$index] ||\&                  $self\->{ARRAY}\->[$index] eq \*(Aq \*(Aq x $self\->{ELEMSIZE};\&      return 1;\&    }.Ve.IP "\s-1DELETE\s0 this, key" 4.IX Xref "DELETE".IX Item "DELETE this, key"Delete the element at index \fIkey\fR from the tied array \fIthis\fR..SpIn our example, a deleted item is \f(CW\*(C`$self\->{ELEMSIZE}\*(C'\fR spaces:.Sp.Vb 5\&    sub DELETE {\&      my $self  = shift;\&      my $index = shift;\&      return $self\->STORE( $index, \*(Aq\*(Aq );\&    }.Ve.IP "\s-1CLEAR\s0 this" 4.IX Xref "CLEAR".IX Item "CLEAR this"Clear (remove, delete, ...) all values from the tied array associated withobject \fIthis\fR.  For example:.Sp.Vb 4\&    sub CLEAR {\&      my $self = shift;\&      return $self\->{ARRAY} = [];\&    }.Ve.IP "\s-1PUSH\s0 this, \s-1LIST\s0" 4.IX Xref "PUSH".IX Item "PUSH this, LIST"Append elements of \fI\s-1LIST\s0\fR to the array.  For example:.Sp.Vb 7\&    sub PUSH {  \&      my $self = shift;\&      my @list = @_;\&      my $last = $self\->FETCHSIZE();\&      $self\->STORE( $last + $_, $list[$_] ) foreach 0 .. $#list;\&      return $self\->FETCHSIZE();\&    }.Ve.IP "\s-1POP\s0 this" 4.IX Xref "POP".IX Item "POP this"Remove last element of the array and return it.  For example:.Sp.Vb 4\&    sub POP {\&      my $self = shift;\&      return pop @{$self\->{ARRAY}};\&    }.Ve.IP "\s-1SHIFT\s0 this" 4.IX Xref "SHIFT".IX Item "SHIFT this"Remove the first element of the array (shifting other elements down)and return it.  For example:.Sp.Vb 4\&    sub SHIFT {\&      my $self = shift;\&      return shift @{$self\->{ARRAY}};\&    }.Ve.IP "\s-1UNSHIFT\s0 this, \s-1LIST\s0" 4.IX Xref "UNSHIFT".IX Item "UNSHIFT this, LIST"Insert \s-1LIST\s0 elements at the beginning of the array, moving existing elementsup to make room.  For example:.Sp.Vb 9\&    sub UNSHIFT {\&      my $self = shift;\&      my @list = @_;\&      my $size = scalar( @list );\&      # make room for our list\&      @{$self\->{ARRAY}}[ $size .. $#{$self\->{ARRAY}} + $size ]\&       = @{$self\->{ARRAY}};\&      $self\->STORE( $_, $list[$_] ) foreach 0 .. $#list;\&    }.Ve.IP "\s-1SPLICE\s0 this, offset, length, \s-1LIST\s0" 4.IX Xref "SPLICE".IX Item "SPLICE this, offset, length, LIST"Perform the equivalent of \f(CW\*(C`splice\*(C'\fR on the array..Sp\&\fIoffset\fR is optional and defaults to zero, negative values count back from the end of the array..Sp\&\fIlength\fR is optional and defaults to rest of the array..Sp\&\fI\s-1LIST\s0\fR may be empty..SpReturns a list of the original \fIlength\fR elements at \fIoffset\fR..SpIn our example, we'll use a little shortcut if there is a \fI\s-1LIST\s0\fR:.Sp.Vb 11\&    sub SPLICE {\&      my $self   = shift;\&      my $offset = shift || 0;\&      my $length = shift || $self\->FETCHSIZE() \- $offset;\&      my @list   = (); \&      if ( @_ ) {\&        tie @list, _\|_PACKAGE_\|_, $self\->{ELEMSIZE};\&        @list   = @_;\&      }\&      return splice @{$self\->{ARRAY}}, $offset, $length, @list;\&    }.Ve.IP "\s-1UNTIE\s0 this" 4.IX Xref "UNTIE".IX Item "UNTIE this"Will be called when \f(CW\*(C`untie\*(C'\fR happens. (See "The \f(CW\*(C`untie\*(C'\fR Gotcha" below.).IP "\s-1DESTROY\s0 this" 4.IX Xref "DESTROY".IX Item "DESTROY this"This method will be triggered when the tied variable needs to be destructed.As with the scalar tie class, this is almost never needed in alanguage that does its own garbage collection, so this time we'lljust leave it out..Sh "Tying Hashes".IX Xref "hash, tying".IX Subsection "Tying Hashes"Hashes were the first Perl data type to be tied (see \fIdbmopen()\fR).  A classimplementing a tied hash should define the following methods: \s-1TIEHASH\s0 isthe constructor.  \s-1FETCH\s0 and \s-1STORE\s0 access the key and value pairs.  \s-1EXISTS\s0reports whether a key is present in the hash, and \s-1DELETE\s0 deletes one.\&\s-1CLEAR\s0 empties the hash by deleting all the key and value pairs.  \s-1FIRSTKEY\s0and \s-1NEXTKEY\s0 implement the \fIkeys()\fR and \fIeach()\fR functions to iterate over allthe keys. \s-1SCALAR\s0 is triggered when the tied hash is evaluated in scalar context. \s-1UNTIE\s0 is called when \f(CW\*(C`untie\*(C'\fR happens, and \s-1DESTROY\s0 is called whenthe tied variable is garbage collected..PPIf this seems like a lot, then feel free to inherit from merely thestandard Tie::StdHash module for most of your methods, redefining only theinteresting ones.  See Tie::Hash for details..PPRemember that Perl distinguishes between a key not existing in the hash,and the key existing in the hash but having a corresponding value of\&\f(CW\*(C`undef\*(C'\fR.  The two possibilities can be tested with the \f(CW\*(C`exists()\*(C'\fR and\&\f(CW\*(C`defined()\*(C'\fR functions..PPHere's an example of a somewhat interesting tied hash class:  it gives youa hash representing a particular user's dot files.  You index into the hashwith the name of the file (minus the dot) and you get back that dot file'scontents.  For example:.PP.Vb 8\&    use DotFiles;\&    tie %dot, \*(AqDotFiles\*(Aq;\&    if ( $dot{profile} =~ /MANPATH/ ||\&         $dot{login}   =~ /MANPATH/ ||\&         $dot{cshrc}   =~ /MANPATH/    )\&    {\&        print "you seem to set your MANPATH\en";\&    }.Ve.PPOr here's another sample of using our tied class:.PP.Vb 5\&    tie %him, \*(AqDotFiles\*(Aq, \*(Aqdaemon\*(Aq;\&    foreach $f ( keys %him ) {\&        printf "daemon dot file %s is size %d\en",

⌨️ 快捷键说明

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