in reply to Error creating database file

OK, you're on a unix box, so using "chop" instead of "chomp" is not a problem (but it is depricated, IIRC). I think you made a mistake copying the code into the post -- there's a problem in the section shown below, which I have commented (but if the code really looks like this, maybe this is part of the problem):
foreach $loop (sort `cd $data_dir; ls -d *`) { chop($loop); foreach $loop2 (sort `cd $data_dir; ls -d $loop/*`) { chop($loop2); ### foreach $loop3 (sort `cd $data_dir; ls -d $loop2/*`) { ## should be: ## foreach $loop3 (sort `cd $data_dir/$loop; ls -d $loop2/*`) { ## or: foreach $loop3 (sort `cd $data_dir; ls -d $loop/$loop2/*`) { chop($loop3); ## BTW, this: $all_files[$i++]=$loop3; ## should be: push( @all_files, $loop3 ); } } }
Apart from that, you're doing  $data_path = s/[\n\r]//g; which seems unnecessary -- you already chopped it in the loop above, and it's just a line of output from "ls".

Then you're splitting $data_path on slash characters, which would never be present in $data_path given the code as posted, so $blank and $file are always empty strings, I think.

I didn't see where &get_key_field() is defined; are you confident about what it's return value is, or should you be testing that?

I'm sorry if this is all peripheral to the real problem, but I think it's worth looking into -- try "perl -d" on the script, set some breakpoints at key lines, and step through it to see whether the values of the variables match your expectations.