| 1 | #--------------------------------------------------------------------- | ||||||||||
| 2 | package Tie::CPHash; | ||||||||||
| 3 | # | ||||||||||
| 4 | # Copyright 1997-2012 Christopher J. Madsen | ||||||||||
| 5 | # | ||||||||||
| 6 | # Author: Christopher J. Madsen <perl@cjmweb.net> | ||||||||||
| 7 | # Created: 08 Nov 1997 | ||||||||||
| 8 | # | ||||||||||
| 9 | # This program is free software; you can redistribute it and/or modify | ||||||||||
| 10 | # it under the same terms as Perl itself. | ||||||||||
| 11 | # | ||||||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the | ||||||||||
| 15 | # GNU General Public License or the Artistic License for more details. | ||||||||||
| 16 | # | ||||||||||
| 17 | # ABSTRACT: Case preserving but case insensitive hash table | ||||||||||
| 18 | #--------------------------------------------------------------------- | ||||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | #===================================================================== | ||||||||||
| 25 | # Package Global Variables: | ||||||||||
| 26 | |||||||||||
| 27 | 2 | our $VERSION = '2.000'; | |||||||||
| 28 | # This file is part of Tie-CPHash 2.000 (January 17, 2015) | ||||||||||
| 29 | |||||||||||
| 30 | #===================================================================== | ||||||||||
| 31 | # Tied Methods: | ||||||||||
| 32 | #--------------------------------------------------------------------- | ||||||||||
| 33 | # TIEHASH classname | ||||||||||
| 34 | # The method invoked by the command `tie %hash, classname'. | ||||||||||
| 35 | # Associates a new hash instance with the specified class. | ||||||||||
| 36 | |||||||||||
| 37 | sub TIEHASH | ||||||||||
| 38 | 3 | ▶ | { | ||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=6.9
| |||||||||||
| 39 | 3 | my $self = bless {}, shift; | |||||||||
| 40 | |||||||||||
| 41 | 3 | ▶ | $self->add(\@_) if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 3 | return $self; | |||||||||
| 44 | } # end TIEHASH | ||||||||||
| 45 | |||||||||||
| 46 | #--------------------------------------------------------------------- | ||||||||||
| 47 | # STORE this, key, value | ||||||||||
| 48 | # Store datum *value* into *key* for the tied hash *this*. | ||||||||||
| 49 | |||||||||||
| 50 | sub STORE | ||||||||||
| 51 | 3 | ▶ | { | ||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 3 | $_[0]->{lc $_[1]} = [ $_[1], $_[2] ]; | |||||||||
| 53 | } # end STORE | ||||||||||
| 54 | |||||||||||
| 55 | #--------------------------------------------------------------------- | ||||||||||
| 56 | # FETCH this, key | ||||||||||
| 57 | # Retrieve the datum in *key* for the tied hash *this*. | ||||||||||
| 58 | |||||||||||
| 59 | sub FETCH | ||||||||||
| 60 | 7 | ▶ | { | ||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||
| 61 | 7 | my $v = $_[0]->{lc $_[1]}; | |||||||||
| 62 | 7 | ▶ | ($v ? $v->[1] : undef); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } # end FETCH | ||||||||||
| 64 | |||||||||||
| 65 | #--------------------------------------------------------------------- | ||||||||||
| 66 | # FIRSTKEY this | ||||||||||
| 67 | # Return the (key, value) pair for the first key in the hash. | ||||||||||
| 68 | |||||||||||
| 69 | sub FIRSTKEY | ||||||||||
| 70 | 1 | ▶ | { | ||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 1 | my $a = scalar keys %{$_[0]}; | |||||||||
| 72 | 1 | &NEXTKEY; | |||||||||
| 73 | } # end FIRSTKEY | ||||||||||
| 74 | |||||||||||
| 75 | #--------------------------------------------------------------------- | ||||||||||
| 76 | # NEXTKEY this, lastkey | ||||||||||
| 77 | # Return the next (key, value) pair for the hash. | ||||||||||
| 78 | |||||||||||
| 79 | sub NEXTKEY | ||||||||||
| 80 | 2 | ▶ | { | ||||||||
|
Subroutine
NEXTKEY: called CC=2 SCAR=6.9
| |||||||||||
| 81 | 2 | my $v = (each %{$_[0]})[1]; | |||||||||
| 82 | 2 | ▶ | ($v ? $v->[0] : undef ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | } # end NEXTKEY | ||||||||||
| 84 | |||||||||||
| 85 | #--------------------------------------------------------------------- | ||||||||||
| 86 | # SCALAR this | ||||||||||
| 87 | # Return bucket usage information for the hash (0 if empty). | ||||||||||
| 88 | |||||||||||
| 89 | sub SCALAR | ||||||||||
| 90 | 4 | ▶ | { | ||||||||
|
Subroutine
SCALAR: called CC=1 SCAR=0.0
| |||||||||||
| 91 | 4 | scalar %{$_[0]}; | |||||||||
| 92 | } # end SCALAR | ||||||||||
| 93 | |||||||||||
| 94 | #--------------------------------------------------------------------- | ||||||||||
| 95 | # EXISTS this, key | ||||||||||
| 96 | # Verify that *key* exists with the tied hash *this*. | ||||||||||
| 97 | |||||||||||
| 98 | sub EXISTS | ||||||||||
| 99 | 2 | ▶ | { | ||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 2 | exists $_[0]->{lc $_[1]}; | |||||||||
| 101 | } # end EXISTS | ||||||||||
| 102 | |||||||||||
| 103 | #--------------------------------------------------------------------- | ||||||||||
| 104 | # DELETE this, key | ||||||||||
| 105 | # Delete the key *key* from the tied hash *this*. | ||||||||||
| 106 | # Returns the old value, or undef if it didn't exist. | ||||||||||
| 107 | |||||||||||
| 108 | sub DELETE | ||||||||||
| 109 | 2 | ▶ | { | ||||||||
|
Subroutine
DELETE: called CC=2 SCAR=6.9
| |||||||||||
| 110 | 2 | my $v = delete $_[0]->{lc $_[1]}; | |||||||||
| 111 | 2 | ▶ | ($v ? $v->[1] : undef); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | } # end DELETE | ||||||||||
| 113 | |||||||||||
| 114 | #--------------------------------------------------------------------- | ||||||||||
| 115 | # CLEAR this | ||||||||||
| 116 | # Clear all values from the tied hash *this*. | ||||||||||
| 117 | |||||||||||
| 118 | sub CLEAR | ||||||||||
| 119 | 1 | ▶ | { | ||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||
| 120 | 1 | %{$_[0]} = (); | |||||||||
| 121 | } # end CLEAR | ||||||||||
| 122 | |||||||||||
| 123 | #===================================================================== | ||||||||||
| 124 | # Other Methods: | ||||||||||
| 125 | #--------------------------------------------------------------------- | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | sub add | ||||||||||
| 129 | 3 | ▶ | { | ||||||||
|
Subroutine
add: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||
| 130 | 3 | my $self = shift; | |||||||||
| 131 | 3 | ▶ | my $list = (@_ == 1) ? shift : \@_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 3 | my $limit = $#$list; | |||||||||
| 133 | |||||||||||
| 134 | 3 | ▶ | unless ($limit % 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | require Carp; | |||||||||
| 136 | 0 | Carp::croak("Odd number of elements in CPHash add"); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 3 | ▶ | for (my $i = 0; $i < $limit; $i+=2 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 4 | $self->{lc $list->[$i]} = [ @$list[$i, $i+1] ]; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 3 | return $self; | |||||||||
| 144 | } # end add | ||||||||||
| 145 | |||||||||||
| 146 | # Aliases for Tie::IxHash users: | ||||||||||
| 147 | 2 | *Push = \&add; | |||||||||
| 148 | 2 | *Unshift = \&add; | |||||||||
| 149 | #--------------------------------------------------------------------- | ||||||||||
| 150 | |||||||||||
| 151 | |||||||||||
| 152 | sub key | ||||||||||
| 153 | 6 | ▶ | { | ||||||||
|
Subroutine
key: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 154 | 6 | my $v = $_[0]->{lc $_[1]}; | |||||||||
| 155 | 6 | ▶ | ($v ? $v->[0] : undef); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | #===================================================================== | ||||||||||
| 159 | # Package Return Value: | ||||||||||
| 160 | |||||||||||
| 161 | 2 | 1; | |||||||||
| 162 | |||||||||||
| 163 | __END__ | ||||||||||