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

📄 hash.pm

📁 Astercon2 开源软交换 2.2.0
💻 PM
字号:
package Tie::Hash;our $VERSION = '1.02';=head1 NAMETie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes=head1 SYNOPSIS    package NewHash;    require Tie::Hash;    @ISA = (Tie::Hash);    sub DELETE { ... }		# Provides needed method    sub CLEAR { ... }		# Overrides inherited method    package NewStdHash;    require Tie::Hash;    @ISA = (Tie::StdHash);    # All methods provided by default, define only those needing overrides    # Accessors access the storage in %{$_[0]};    # TIEHASH should return a reference to the actual storage    sub DELETE { ... }    package NewExtraHash;    require Tie::Hash;    @ISA = (Tie::ExtraHash);    # All methods provided by default, define only those needing overrides    # Accessors access the storage in %{$_[0][0]};    # TIEHASH should return an array reference with the first element being    # the reference to the actual storage     sub DELETE {       $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer      delete $_[0][0]->{$_[1]};		  #  $_[0]->SUPER::DELETE($_[1])    }    package main;    tie %new_hash, 'NewHash';    tie %new_std_hash, 'NewStdHash';    tie %new_extra_hash, 'NewExtraHash',	sub {warn "Doing \U$_[1]\E of $_[2].\n"};=head1 DESCRIPTIONThis module provides some skeletal methods for hash-tying classes. SeeL<perltie> for a list of the functions required in order to tie a hashto a package. The basic B<Tie::Hash> package provides a C<new> method, as wellas methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> andB<Tie::ExtraHash> packagesprovide most methods for hashes described in L<perltie> (the exceptionsare C<UNTIE> and C<DESTROY>).  They cause tied hashes to behave exactly like standard hashes,and allow for selective overwriting of methods.  B<Tie::Hash> grandfathers theC<new> method: it is used if C<TIEHASH> is not definedin the case a class forgets to include a C<TIEHASH> method.For developers wishing to write their own tied hashes, the required methodsare briefly defined below. See the L<perltie> section for more detaileddescriptive, as well as example code:=over 4=item TIEHASH classname, LISTThe method invoked by the command C<tie %hash, classname>. Associates a newhash instance with the specified class. C<LIST> would represent additionalarguments (along the lines of L<AnyDBM_File> and compatriots) needed tocomplete the association.=item STORE this, key, valueStore datum I<value> into I<key> for the tied hash I<this>.=item FETCH this, keyRetrieve the datum in I<key> for the tied hash I<this>.=item FIRSTKEY thisReturn the first key in the hash.=item NEXTKEY this, lastkeyReturn the next key in the hash.=item EXISTS this, keyVerify that I<key> exists with the tied hash I<this>.The B<Tie::Hash> implementation is a stub that simply croaks.=item DELETE this, keyDelete the key I<key> from the tied hash I<this>.=item CLEAR thisClear all values from the tied hash I<this>.=item SCALAR thisReturns what evaluating the hash in scalar context yields.B<Tie::Hash> does not implement this method (but B<Tie::StdHash>and B<Tie::ExtraHash> do).=back=head1 Inheriting from B<Tie::StdHash>The accessor methods assume that the actual storage for the data in the tiedhash is in the hash referenced by C<tied(%tiedhash)>.  Thus overwrittenC<TIEHASH> method should return a hash reference, and the remaining methodsshould operate on the hash referenced by the first argument:  package ReportHash;  our @ISA = 'Tie::StdHash';  sub TIEHASH  {    my $storage = bless {}, shift;    warn "New ReportHash created, stored in $storage.\n";    $storage  }  sub STORE    {    warn "Storing data with key $_[1] at $_[0].\n";    $_[0]{$_[1]} = $_[2]  }=head1 Inheriting from B<Tie::ExtraHash>The accessor methods assume that the actual storage for the data in the tiedhash is in the hash referenced by C<(tied(%tiedhash))-E<gt>[0]>.  Thus overwrittenC<TIEHASH> method should return an array reference with the firstelement being a hash reference, and the remaining methods should operate on thehash C<< %{ $_[0]->[0] } >>:  package ReportHash;  our @ISA = 'Tie::ExtraHash';  sub TIEHASH  {    my $class = shift;    my $storage = bless [{}, @_], $class;    warn "New ReportHash created, stored in $storage.\n";    $storage;  }  sub STORE    {    warn "Storing data with key $_[1] at $_[0].\n";    $_[0][0]{$_[1]} = $_[2]  }The default C<TIEHASH> method stores "extra" arguments to tie() startingfrom offset 1 in the array referenced by C<tied(%tiedhash)>; this is thesame storage algorithm as in TIEHASH subroutine above.  Hence, a typicalpackage inheriting from B<Tie::ExtraHash> does not need to overwrite thismethod.=head1 C<SCALAR>, C<UNTIE> and C<DESTROY>The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,B<Tie::StdHash>, or B<Tie::ExtraHash>.  Tied hashes do not requirepresence of these methods, but if defined, the methods will be called inproper time, see L<perltie>.C<SCALAR> is only defined in B<Tie::StdHash> and B<Tie::ExtraHash>.If needed, these methods should be defined by the package inheriting fromB<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>. See L<pertie/"SCALAR">to find out what happens when C<SCALAR> does not exist.=head1 MORE INFORMATIONThe packages relating to various DBM-related implementations (F<DB_File>,F<NDBM_File>, etc.) show examples of general tied hashes, as does theL<Config> module. While these do not utilize B<Tie::Hash>, they serve asgood working examples.=cutuse Carp;use warnings::register;sub new {    my $pkg = shift;    $pkg->TIEHASH(@_);}# Grandfather "new"sub TIEHASH {    my $pkg = shift;    if (defined &{"${pkg}::new"}) {	warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");	$pkg->new(@_);    }    else {	croak "$pkg doesn't define a TIEHASH method";    }}sub EXISTS {    my $pkg = ref $_[0];    croak "$pkg doesn't define an EXISTS method";}sub CLEAR {    my $self = shift;    my $key = $self->FIRSTKEY(@_);    my @keys;    while (defined $key) {	push @keys, $key;	$key = $self->NEXTKEY(@_, $key);    }    foreach $key (@keys) {	$self->DELETE(@_, $key);    }}# The Tie::StdHash package implements standard perl hash behaviour.# It exists to act as a base class for classes which only wish to# alter some parts of their behaviour.package Tie::StdHash;# @ISA = qw(Tie::Hash);		# would inherit new() onlysub TIEHASH  { bless {}, $_[0] }sub STORE    { $_[0]->{$_[1]} = $_[2] }sub FETCH    { $_[0]->{$_[1]} }sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }sub NEXTKEY  { each %{$_[0]} }sub EXISTS   { exists $_[0]->{$_[1]} }sub DELETE   { delete $_[0]->{$_[1]} }sub CLEAR    { %{$_[0]} = () }sub SCALAR   { scalar %{$_[0]} }package Tie::ExtraHash;sub TIEHASH  { my $p = shift; bless [{}, @_], $p }sub STORE    { $_[0][0]{$_[1]} = $_[2] }sub FETCH    { $_[0][0]{$_[1]} }sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }sub NEXTKEY  { each %{$_[0][0]} }sub EXISTS   { exists $_[0][0]->{$_[1]} }sub DELETE   { delete $_[0][0]->{$_[1]} }sub CLEAR    { %{$_[0][0]} = () }sub SCALAR   { scalar %{$_[0][0]} }1;

⌨️ 快捷键说明

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