1 |
< |
# |
2 |
< |
# HashDB.pm |
3 |
< |
# |
4 |
< |
# Originally Written by Christopher Williams |
5 |
< |
# |
6 |
< |
# Description |
7 |
< |
# |
8 |
< |
# Interface |
9 |
< |
# --------- |
10 |
< |
# new() : A new HashDB object |
11 |
< |
# setdata(data, @keys) : set a data item to the given keys |
12 |
< |
# getdata(@keys) : return all data items that match the given keys |
13 |
< |
# deletedata(@keys) : detete all data items that match the given keys |
14 |
< |
# match(@keys) : return the full DataItem object refs that match keys |
15 |
< |
# items() : return the number of seperate items in the store |
16 |
< |
# store(filename) : dump to file |
17 |
< |
# restore(filename) : restore from file |
18 |
< |
# alias(\@refofkeys,\@aliaskeys) : attatch another set of keys to items that |
19 |
< |
# match the refopfkeys |
20 |
< |
# note that these are references to the arrays |
21 |
< |
# unalias(@aliaskeys) :remove an alias |
1 |
> |
=head1 NAME |
2 |
|
|
3 |
+ |
Utilities::HashDB - A type of database object. |
4 |
+ |
|
5 |
+ |
=head1 SYNOPSIS |
6 |
+ |
|
7 |
+ |
my $obj = Utilities::HashDB->new(); |
8 |
+ |
|
9 |
+ |
=head1 METHODS |
10 |
+ |
|
11 |
+ |
=over |
12 |
+ |
|
13 |
+ |
=cut |
14 |
+ |
|
15 |
+ |
=item C<new()> |
16 |
+ |
|
17 |
+ |
Create a new HashDB object. |
18 |
+ |
|
19 |
+ |
=item C<setdata(data, @keys)> |
20 |
+ |
|
21 |
+ |
set a data item to the given keys. |
22 |
+ |
|
23 |
+ |
=item C<getdata(@keys)> |
24 |
+ |
|
25 |
+ |
return all data items that match the given keys. |
26 |
+ |
|
27 |
+ |
=item C<deletedata(@keys)> |
28 |
+ |
|
29 |
+ |
detete all data items that match the given keys. |
30 |
+ |
|
31 |
+ |
=item C<match(@keys)> |
32 |
+ |
|
33 |
+ |
return the full DataItem object refs that match keys. |
34 |
+ |
|
35 |
+ |
=item C<items()> |
36 |
+ |
|
37 |
+ |
return the number of seperate items in the store. |
38 |
+ |
|
39 |
+ |
=item C<store(filename)> |
40 |
+ |
|
41 |
+ |
dump to file. |
42 |
+ |
|
43 |
+ |
=item C<restore(filename)> |
44 |
+ |
|
45 |
+ |
restore from file. |
46 |
+ |
|
47 |
+ |
=item C<alias(\@refofkeys,\@aliaskeys)> |
48 |
+ |
|
49 |
+ |
attatch another set of keys to items that |
50 |
+ |
match the refopfkeys (note that these are |
51 |
+ |
references to the arrays). |
52 |
+ |
|
53 |
+ |
=item unalias(@aliaskeys) |
54 |
+ |
|
55 |
+ |
remove an alias. |
56 |
+ |
|
57 |
+ |
=back |
58 |
+ |
|
59 |
+ |
=head1 AUTHOR |
60 |
+ |
|
61 |
+ |
Originally Written by Christopher Williams. |
62 |
+ |
|
63 |
+ |
=head1 MAINTAINER |
64 |
+ |
|
65 |
+ |
Shaun ASHBY |
66 |
+ |
|
67 |
+ |
=cut |
68 |
+ |
|
69 |
|
package Utilities::HashDB; |
70 |
|
use Utilities::DataItem; |
71 |
|
use FileHandle; |
72 |
< |
require 5.001; |
72 |
> |
require 5.004; |
73 |
|
|
74 |
|
sub new { |
75 |
|
my $class=shift; |
153 |
|
my $self=shift; |
154 |
|
my $filename=shift; |
155 |
|
|
156 |
+ |
use FileHandle; |
157 |
|
my $fh=FileHandle->new(); |
158 |
< |
$fh->autoflush(1); |
159 |
< |
open ($fh, ">$filename") or die "Unable to open file $filename\n $!\n"; |
158 |
> |
$fh->autoflush(1); |
159 |
> |
|
160 |
> |
open ($fh, "> $filename") or die "Unable to open file $filename\n $!\n"; |
161 |
|
foreach $object ( @{$self->{dataitems}} ) { |
162 |
|
print $fh "\n"; |
163 |
|
$object->store($fh); |
164 |
< |
} |
164 |
> |
} |
165 |
|
close $fh; |
166 |
< |
} |
166 |
> |
} |
167 |
|
|
168 |
|
sub restore { |
169 |
|
my $self=shift; |
170 |
|
my $filename=shift; |
171 |
|
my @arr=(); |
172 |
|
my $data; |
173 |
+ |
# Make $_ local so it's writable, otherwise you get errors like this: |
174 |
+ |
# Modification of a read-only value attempted at |
175 |
+ |
# /afs/cern.ch/user/s/sashby/w2/SCRAM/V1_0_3/src/Utilities/HashDB.pm line 178. |
176 |
+ |
local $_; |
177 |
|
|
178 |
|
my $fh=FileHandle->new(); |
179 |
< |
open ($fh, "<$filename") or die "Unable to open file $filename\n $!\n"; |
180 |
< |
while ( <$fh> ) { |
181 |
< |
push @{$self->{dataitems}}, Utilities::DataItem->restore($fh); |
182 |
< |
} |
179 |
> |
open ($fh, "< $filename") or die "Unable to open file $filename\n $!\n"; |
180 |
> |
while (<$fh>) |
181 |
> |
{ |
182 |
> |
push @{$self->{dataitems}}, Utilities::DataItem->restore($fh); |
183 |
> |
} |
184 |
> |
|
185 |
|
close $fh; |
186 |
|
} |
187 |
|
|