When you get an error that you don't understand, it's always a good idea to add "use diagnostics" to your program so you get a more detailed error message.

In your case it will say something like this:

Use of uninitialized value in hash element at ./foo.pl line xx (#1)

(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.

So what it means is that the value $fields[$_] is returning "undef" which is being translated to "" when used as a hash key. This probably means that the @fields array doesn't have the data in it that you think there is.

You're only looking at half of the problem. Let's look at the problem logically. Here's a quick lesson in debugging code.

You have the following code:

Code

my $output;

# stuff missing that doesn't effect $output

$output = check_dirs(\@path,$r_setup);

$output .= check_cat_datafile(); # "undef" error here.

So, when the line that gives the error is executed, either $output is "undef" or check_cat_datafile() returns "undef". you need to investigate both of these possibilities.

The only way that $output could be "undef" is if check_dir(\@path, $r_setup) returns "undef". You need to see what circumstances would allow that subroutine to return "undef" and see how you can prevent that. It might be enough to check the value returned like this:

The other option is that check_cat_datafile() returns "undef". You've shown use the code for that subroutine so we can see what might be the problem. The code basically looks like this:

Code

sub check_cat_datafile {

my $text; my $cat_file = "$cgidir/cat.setup";

unless (-e $cat_file) { # give $text a value }

return $text; }

So can this subroutine return "undef"? Yes, of course it can. If the cat.setup file is not in the right place, then $text will never be given a value and the subroutine will return "undef". You need to work out how you're going to deal with that. Should your program die if the setup file isn't found? Or should $text be set to the empty string?

Basically, your code is making far too many assumptions about things going right and it's not handling exceptions well. You either need to tighten up your programming style or pay a programmer to do the job for you.