| 1 | package Tie::TwoLevelHash; | ||
| 2 | |||
| 3 | # $Id: TwoLevelHash.pm,v 1.2 1998/10/30 13:52:04 kmeltz Exp kmeltz $ | ||
| 4 | |||
| 5 | # $Log: TwoLevelHash.pm,v $ | ||
| 6 | # Revision 1.2 1998/10/30 13:52:04 kmeltz | ||
| 7 | # Fixed FETCH so it will return correctly when doing %foo = %bar; when using a tie to a HoH's. Still not working right for hash in the HoH's, so the GetHash method stays. | ||
| 8 | # | ||
| 9 | # | ||
| 10 | # Revision 1.1 1998/10/27 15:43:47 kmeltz | ||
| 11 | # Changed croaks to carps for Hash Invalid warning. May need to continue script, so let script die and module return undef. | ||
| 12 | # Changed CLEAR to not erase TLH file when clearing hash in HoH's, or resetting it. | ||
| 13 | # Added exported method GetHash. This allows for user to import hash values into their script, and change values before setting them to TLH file. | ||
| 14 | # Changed a bunch in the POD. | ||
| 15 | # | ||
| 16 | |||
| 17 | 1 | ▶ | use FileHandle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | @ISA = qw(Exporter); | ||
| 21 | @EXPORT = qw(); | ||
| 22 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | |||
| 24 | ($VERSION = substr(q$Revision: 1.2 $, 10)) =~ s/\s+$//; | ||
| 25 | |||
| 26 | sub TIEHASH { | ||
| 27 | my $self = shift; | ||
| 28 | my $path = shift; | ||
| 29 | my $mode = shift || 'r'; | ||
| 30 | my $oneHash = ""; | ||
| 31 | my ($comment, %tlhash, %unihash, $key); | ||
| 32 | my $single=0; | ||
| 33 | |||
| 34 | # Check to see if $path has two elements | ||
| 35 | if ($path =~ /,/) { | ||
| 36 | ($path, $oneHash) = split(/,/,$path); | ||
| 37 | } | ||
| 38 | |||
| 39 | if (@_) { | ||
| 40 | croak ("usage: tie(\%hash, \$file[, Hash name], [mode])"); | ||
| 41 | } | ||
| 42 | |||
| 43 | # Nice-ify $path: | ||
| 44 | $path =~ s#/$##; | ||
| 45 | |||
| 46 | # Can we make changes to the database? | ||
| 47 | my $clobber = ($mode eq 'rw' ? 1 : 0); | ||
| 48 | |||
| 49 | unless (-e $path) { | ||
| 50 | if ($clobber) { | ||
| 51 | # Create the tlh if it does not exist | ||
| 52 | unless (open(FH,">$path")) {croak("Can't create $path: $!");} | ||
| 53 | } else { | ||
| 54 | croak("$path does not exist"); | ||
| 55 | } | ||
| 56 | #croak "File does not exist\n"; | ||
| 57 | } | ||
| 58 | close FH; | ||
| 59 | |||
| 60 | # Get a filehandle and open the file: | ||
| 61 | my $fh = new FileHandle; | ||
| 62 | open($fh, $path) or croak("can't open $path: $!"); | ||
| 63 | |||
| 64 | my $tlh; | ||
| 65 | |||
| 66 | # Showing one hash | ||
| 67 | if ($oneHash ne "") { | ||
| 68 | $oneHash =~ s/^\s//; | ||
| 69 | ($comment, %unihash) = $self->_get_HoH($path); | ||
| 70 | #%tlhash = $self->_get_SingHash("$oneHash", $path); | ||
| 71 | foreach $key (keys %unihash) { | ||
| 72 | if ($key eq $oneHash) { | ||
| 73 | %tlhash->{$key} = $unihash{$key}; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | $single=1; | ||
| 77 | # Show HoH's | ||
| 78 | }else{ | ||
| 79 | ($comment, %tlhash) = $self->_get_HoH($path); | ||
| 80 | } | ||
| 81 | |||
| 82 | my $node = { | ||
| 83 | PATH => $path, | ||
| 84 | CLOBBER => $clobber, | ||
| 85 | HANDLE => $fh, | ||
| 86 | SINGLEHASH => $single, | ||
| 87 | }; | ||
| 88 | $node->{BIHASH} = \%tlhash; | ||
| 89 | $node->{UNIHASH} = \%unihash if defined(%unihash); | ||
| 90 | $node->{UNIHASHNAME} = $oneHash if defined(%unihash); | ||
| 91 | $node->{COMMENTS} = $comment if defined($comment); | ||
| 92 | |||
| 93 | if ($oneHash ne "" ) { $node->{SINGLEHASH} = 1; }else{$node->{SINGLEHASH} = 0;} | ||
| 94 | |||
| 95 | return bless $node => $self; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | #-------------------------------------------------------# | ||
| 100 | |||
| 101 | sub FETCH { | ||
| 102 | my $self = shift; | ||
| 103 | my $key = shift; | ||
| 104 | |||
| 105 | # If showing one hash | ||
| 106 | if ($self->{SINGLEHASH}) { | ||
| 107 | my $uniname = $self->{UNIHASHNAME}; | ||
| 108 | if ($key eq $uniname) { | ||
| 109 | my %return = $self->GetHash(); | ||
| 110 | return \%return; | ||
| 111 | } | ||
| 112 | unless (exists $self->{BIHASH}->{$uniname}->{$key}) { | ||
| 113 | carp "Hash invalid"; | ||
| 114 | return undef; | ||
| 115 | } | ||
| 116 | if (defined $self->{BIHASH}->{$uniname}->{$key}) { | ||
| 117 | return $self->{BIHASH}->{$uniname}->{$key}; | ||
| 118 | } else { | ||
| 119 | return carp("Fetch failed for $key"); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | # If showing HoH's | ||
| 124 | unless (exists $self->{BIHASH}->{$key}) { | ||
| 125 | carp "Hash invalid"; | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | if (defined $self->{BIHASH}->{$key}) { | ||
| 129 | return $self->{BIHASH}->{$key}; | ||
| 130 | } else { | ||
| 131 | return carp("Fetch failed for $key"); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | #-------------------------------------------------------# | ||
| 136 | |||
| 137 | sub STORE { | ||
| 138 | my $self = shift; | ||
| 139 | my $key = shift; | ||
| 140 | my $value;# = shift || ""; | ||
| 141 | my ($inKey); | ||
| 142 | my $file = $self->{PATH}; | ||
| 143 | my ($foo,$val,$bar,$str, %value); | ||
| 144 | my (%foo); | ||
| 145 | my $single = $self->{SINGLEHASH}; | ||
| 146 | my %bihash = %{$self->{'BIHASH'}}; | ||
| 147 | my (%unihash); | ||
| 148 | |||
| 149 | if ($single) {%unihash = %{$self->{'UNIHASH'}}; $value = shift;}else{$value=shift || "";} | ||
| 150 | |||
| 151 | # HoH's AND no value/undef | ||
| 152 | if (!$single && $value eq "") { | ||
| 153 | if (!$self->EXISTS($key)) { | ||
| 154 | carp("Tried deleting $key which doesn't exist"); | ||
| 155 | return 0; | ||
| 156 | } | ||
| 157 | |||
| 158 | if (!$self->_deleteRecord($key)) { | ||
| 159 | return 0; | ||
| 160 | } else { | ||
| 161 | return 1; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | if (!$single) { | ||
| 166 | %value = %$value; | ||
| 167 | } | ||
| 168 | |||
| 169 | unless ($self->{CLOBBER}) { | ||
| 170 | carp ("No write access for $self->{'PATH'}"); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | my $fh; | ||
| 175 | unless ($fh = new FileHandle(">$file")) { | ||
| 176 | #unless ($fh = new FileHandle("$file")) { # DEBUG | ||
| 177 | croak ("Can't open $file: $!"); | ||
| 178 | } | ||
| 179 | |||
| 180 | # Set the new value in original hash | ||
| 181 | if (!$single) { | ||
| 182 | foreach $inKey (sort keys %value) { | ||
| 183 | #print "$inKey $value{$inKey}\n"; # DEBUG | ||
| 184 | if ($inKey eq "") { | ||
| 185 | undef $bihash{$key}{$inKey}; | ||
| 186 | next; | ||
| 187 | } | ||
| 188 | $bihash{$key}{$inKey} = $value{$inKey}; | ||
| 189 | } | ||
| 190 | } else { | ||
| 191 | if (!defined($value)) { | ||
| 192 | undef $unihash{$self->{UNIHASHNAME}}{$key}; | ||
| 193 | undef $bihash{$key}; | ||
| 194 | |||
| 195 | } | ||
| 196 | |||
| 197 | $unihash{$self->{UNIHASHNAME}}{$key} = $value; | ||
| 198 | $bihash{$key} = $value; | ||
| 199 | |||
| 200 | }# endif | ||
| 201 | |||
| 202 | # If there were comments on the top, re-write them first | ||
| 203 | if (defined($self->{COMMENTS})) { | ||
| 204 | my $comment = $self->{COMMENTS}; | ||
| 205 | $str .= "$comment"; | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | # Run through HoH from memory and get out each Hash in %zog | ||
| 210 | if (!$single) { | ||
| 211 | foreach $foo (sort keys %bihash) { | ||
| 212 | $str .= "\n$foo\n"; | ||
| 213 | my $zog = $bihash{$foo}; | ||
| 214 | my %zog=%$zog; | ||
| 215 | # Get all values in %zog | ||
| 216 | foreach $bar (sort keys %zog) { | ||
| 217 | $str .= "\t$bar\: $zog{$bar}\n" unless !defined($zog{$bar}); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } else { | ||
| 221 | foreach $foo (sort keys %unihash) { | ||
| 222 | $str .= "\n$foo\n"; | ||
| 223 | my $zog = $unihash{$foo}; | ||
| 224 | my %zog=%$zog; | ||
| 225 | # Get all values in %zog | ||
| 226 | foreach $bar (sort keys %zog) { | ||
| 227 | $str .= "\t$bar\: $zog{$bar}\n" unless !defined($zog{$bar}); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | $self->{'UNIHASH'} = \%unihash if defined(%unihash); | ||
| 232 | $self->{'BIHASH'} = \%bihash; | ||
| 233 | print $fh $str; | ||
| 234 | close $fh; | ||
| 235 | return 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | #-------------------------------------------------------# | ||
| 239 | |||
| 240 | sub DELETE { | ||
| 241 | my ($self) = shift; | ||
| 242 | my ($key) = shift; | ||
| 243 | if ($self->{SINGLEHASH}) { | ||
| 244 | delete $self->{UNIHASH}->{$key}; | ||
| 245 | delete $self->{BIHASH}->{$key}; | ||
| 246 | }else{ | ||
| 247 | delete $self->{BIHASH}->{$key}; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | #-------------------------------------------------------# | ||
| 252 | |||
| 253 | sub CLEAR { | ||
| 254 | my ($self) = shift; | ||
| 255 | my ($key); | ||
| 256 | my ($file) = $self->{'PATH'}; | ||
| 257 | |||
| 258 | if ($self->{SINGLEHASH}) { | ||
| 259 | foreach $key (keys %{$self->{BIHASH}}) { | ||
| 260 | $self->DELETE($key); | ||
| 261 | } | ||
| 262 | return 1; | ||
| 263 | } | ||
| 264 | foreach $key (keys %{$self->{BIHASH}}) { | ||
| 265 | $self->DELETE($key); | ||
| 266 | } | ||
| 267 | |||
| 268 | |||
| 269 | # Erase file, since it is being cleared | ||
| 270 | |||
| 271 | unless ($self->{SINGLEHASH}) { | ||
| 272 | my $fh; | ||
| 273 | unless ($fh = new FileHandle(">$file")) { | ||
| 274 | #unless ($fh = new FileHandle("$file")) { # DEBUG | ||
| 275 | croak ("Can't open $file: $!"); | ||
| 276 | } | ||
| 277 | close $fh; | ||
| 278 | } | ||
| 279 | # File erased, if tied to HoH | ||
| 280 | |||
| 281 | } | ||
| 282 | |||
| 283 | #-------------------------------------------------------# | ||
| 284 | |||
| 285 | sub EXISTS { | ||
| 286 | my $self = shift; | ||
| 287 | my $key = shift; | ||
| 288 | |||
| 289 | return exists $self->{BIHASH}->{$key}; | ||
| 290 | } | ||
| 291 | |||
| 292 | #-------------------------------------------------------# | ||
| 293 | |||
| 294 | sub DESTROY { | ||
| 295 | my $self = shift; | ||
| 296 | my $node = {}; | ||
| 297 | } | ||
| 298 | |||
| 299 | #-------------------------------------------------------# | ||
| 300 | |||
| 301 | sub FIRSTKEY { | ||
| 302 | my $self = shift; | ||
| 303 | my $a = keys %{$self->{BIHASH}}; | ||
| 304 | each %{$self->{BIHASH}} | ||
| 305 | } | ||
| 306 | |||
| 307 | #-------------------------------------------------------# | ||
| 308 | |||
| 309 | sub NEXTKEY { | ||
| 310 | my $self = shift; | ||
| 311 | return each %{$self->{BIHASH}} | ||
| 312 | } | ||
| 313 | |||
| 314 | #-------------------------------------------------------# | ||
| 315 | |||
| 316 | sub _deleteRecord { | ||
| 317 | my $self = shift; | ||
| 318 | my $record = shift; | ||
| 319 | if (!defined($record)) { | ||
| 320 | carp("Not enough args passed to _deleteRecord"); | ||
| 321 | return 0; | ||
| 322 | } | ||
| 323 | my $file = $self->{PATH}; | ||
| 324 | my ($foo,$str,$bar); | ||
| 325 | my $fh; | ||
| 326 | unless ($fh = new FileHandle(">$file")) { | ||
| 327 | #unless ($fh = new FileHandle("$file")) { # DEBUG | ||
| 328 | croak ("can't open $file: $!"); | ||
| 329 | } | ||
| 330 | my %bihash = %{$self->{'BIHASH'}}; | ||
| 331 | $str .= $self->{'COMMENTS'} if defined($self->{'COMMENTS'}); | ||
| 332 | |||
| 333 | foreach $foo (sort keys %bihash) { | ||
| 334 | #print "$foo $bihash{$foo} d\n"; | ||
| 335 | if ($foo eq $record) { | ||
| 336 | #$bihash{$foo} = undef; | ||
| 337 | next; | ||
| 338 | } | ||
| 339 | $str .= "\n$foo\n"; | ||
| 340 | my $zog = $bihash{$foo}; | ||
| 341 | my %zog=%$zog; | ||
| 342 | # Get all values in %zog | ||
| 343 | foreach $bar (sort keys %zog) { | ||
| 344 | $str .= "\t$bar\: $zog{$bar}\n" unless !defined($zog{$bar}); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | $self->{'BIHASH'} = \%bihash; | ||
| 349 | print $fh $str; | ||
| 350 | close $fh; | ||
| 351 | return 1; | ||
| 352 | } | ||
| 353 | |||
| 354 | #-----------------------------------------------------------# | ||
| 355 | |||
| 356 | sub _get_HoH { | ||
| 357 | my ($self) = shift; | ||
| 358 | my ($slh) = shift; | ||
| 359 | my ($key, $val); | ||
| 360 | my ($name, @lines, $rec, $line); | ||
| 361 | my (%HoH); | ||
| 362 | my ($FH) = new FileHandle; | ||
| 363 | |||
| 364 | if (!open($FH,"$slh")) { | ||
| 365 | croak "Cannot open $FH $slh: $!"; | ||
| 366 | } | ||
| 367 | |||
| 368 | local $/ = ""; | ||
| 369 | |||
| 370 | my @records = <$FH>; | ||
| 371 | |||
| 372 | # Make sure comments at top of TLH file stay | ||
| 373 | my $comment; | ||
| 374 | if ($records[0] && $records[0] =~ /^#/) { | ||
| 375 | $comment = $records[0]; | ||
| 376 | chop $comment; | ||
| 377 | shift @records; | ||
| 378 | } | ||
| 379 | foreach $rec (@records) { | ||
| 380 | |||
| 381 | ($name, @lines) = split /\n[\s]*/, $rec; | ||
| 382 | |||
| 383 | foreach $line (@lines) { | ||
| 384 | ($key, $val) = split /:\s*/, $line; | ||
| 385 | $HoH{$name}->{$key} = $val; | ||
| 386 | |||
| 387 | } | ||
| 388 | } | ||
| 389 | if (!close($FH)) { | ||
| 390 | croak "Cannot close $FH: $!"; | ||
| 391 | } | ||
| 392 | |||
| 393 | return ($comment, %HoH); | ||
| 394 | } # end _get_HoH | ||
| 395 | |||
| 396 | #-----------------------------------------------------------# | ||
| 397 | |||
| 398 | sub GetHash { | ||
| 399 | my ($self) = shift; | ||
| 400 | my ($hash, %hash); | ||
| 401 | my $name = $self->{UNIHASHNAME} if ($self->{UNIHASHNAME} ne ""); | ||
| 402 | |||
| 403 | if (defined($name)) { | ||
| 404 | $hash = $self->{BIHASH}->{$name}; | ||
| 405 | }else{ | ||
| 406 | $hash = $self->{BIHASH}; | ||
| 407 | } | ||
| 408 | %hash = %$hash; | ||
| 409 | |||
| 410 | return %hash; | ||
| 411 | } | ||
| 412 | |||
| 413 | #-----------------------------------------------------------# | ||
| 414 | |||
| 415 | 1; | ||
| 416 | |||
| 417 | __END__ | ||