# subroutine name: readhash # Reads all the data from $datafile # Returns: reference to the HASH # File format required: \t # # Global variables used: # $no_flock - disable flock or not sub readhash { my ($datafile) = @_; my %hash; GetFileLock("$datafile.lock"); local *DATA; if (open(DATA,"<$datafile")) { flock(DATA, 1) unless ($no_flock); my @arr; while (my $line = ) { if ($line =~ m/^(.+?)\t(.*?)[\n\x0D\x0A]*$/) { my ($key,$value) = ($1, $2); push (@arr, (unsafe($key), unsafe($value)) ); } } # Sort everything at once %hash = @arr; flock(DATA, 8) unless ($no_flock); close(DATA); ReleaseFileLock("$datafile.lock"); } else { ReleaseFileLock("$datafile.lock"); die "Error in subroutine readhash: Can't open $datafile: $!"; } \%hash; }