X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTie%2FHash.pm;h=2244711669ab8dfe9011264e3da5d228979d49c6;hb=052b629eb0274721d73944a03d3c4aac89392a61;hp=161771a0ea42bc89660cd35499c75a2dcc3600a6;hpb=399f14a194513745fd160c0e4e8f6f7f718779cf;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Tie/Hash.pm b/lib/Tie/Hash.pm index 161771a..2244711 100644 --- a/lib/Tie/Hash.pm +++ b/lib/Tie/Hash.pm @@ -1,24 +1,24 @@ -package TieHash; +package Tie::Hash; =head1 NAME -TieHash, TieHash::Std - base class definitions for tied hashes +Tie::Hash, Tie::StdHash - base class definitions for tied hashes =head1 SYNOPSIS package NewHash; - require TieHash; + require Tie::Hash; - @ISA = (TieHash); + @ISA = (Tie::Hash); sub DELETE { ... } # Provides needed method sub CLEAR { ... } # Overrides inherited method package NewStdHash; - require TieHash; + require Tie::Hash; - @ISA = (TieHash::Std); + @ISA = (Tie::StdHash); # All methods provided by default, define only those needing overrides sub DELETE { ... } @@ -26,26 +26,29 @@ TieHash, TieHash::Std - base class definitions for tied hashes package main; - tie %new_hash, NewHash; - tie %new_std_hash, NewStdHash; + tie %new_hash, 'NewHash'; + tie %new_std_hash, 'NewStdHash'; =head1 DESCRIPTION This module provides some skeletal methods for hash-tying classes. See -L for a list of the functions required in order to tie a hash -to a package. The basic B package provides a C method, as well -as methods C, C and C. The B package -provides most methods required for hashes in L. It inherits from -B, and causes tied hashes to behave exactly like standard hashes, -allowing for selective overloading of methods. The B method is provided -as grandfathering in the case a class forgets to include a B method. +L for a list of the functions required in order to tie a hash +to a package. The basic B package provides a C method, as well +as methods C, C and C. The B package +provides most methods required for hashes in L. It inherits from +B, and causes tied hashes to behave exactly like standard hashes, +allowing for selective overloading of methods. The C method is provided +as grandfathering in the case a class forgets to include a C method. For developers wishing to write their own tied hashes, the required methods -are: +are briefly defined below. See the L section for more detailed +descriptive, as well as example code: + +=over =item TIEHASH classname, LIST -The method invoked by the command C. Associates a new +The method invoked by the command C. Associates a new hash instance with the specified class. C would represent additional arguments (along the lines of L and compatriots) needed to complete the association. @@ -64,12 +67,14 @@ Return the (key, value) pair for the first key in the hash. =item NEXTKEY this, lastkey -Return the next (key, value) pair for the hash. +Return the next key for the hash. =item EXISTS this, key Verify that I exists with the tied hash I. +The B implementation is a stub that simply croaks. + =item DELETE this, key Delete the key I from the tied hash I. @@ -82,23 +87,22 @@ Clear all values from the tied hash I. =head1 CAVEATS -The L documentation includes a method called C as -a necessary method for tied hashes. Neither B nor B -define a default for this method. - -The C method provided by these two packages is not listed in the -L section. +The L documentation includes a method called C as +a necessary method for tied hashes. Neither B nor B +define a default for this method. This is a standard for class packages, +but may be omitted in favor of a simple default. =head1 MORE INFORMATION -The packages relating to various DBM-related implemetations (F, +The packages relating to various DBM-related implementations (F, F, etc.) show examples of general tied hashes, as does the -L module. While these do not utilize B, they serve as +L module. While these do not utilize B, they serve as good working examples. =cut - + use Carp; +use warnings::register; sub new { my $pkg = shift; @@ -109,9 +113,8 @@ sub new { sub TIEHASH { my $pkg = shift; - if (defined &{"{$pkg}::new"}) { - carp "WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing" - if $^W; + if (defined &{"${pkg}::new"}) { + warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing"); $pkg->new(@_); } else { @@ -138,12 +141,12 @@ sub CLEAR { } } -# The TieHash::Std package implements standard perl hash behaviour. +# 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 TieHash::Std; -@ISA = qw(TieHash); +package Tie::StdHash; +@ISA = qw(Tie::Hash); sub TIEHASH { bless {}, $_[0] } sub STORE { $_[0]->{$_[1]} = $_[2] }