user1581917 user1581917 - 1 year ago 72
Perl Question

Use of uninitialized value in concatenation (.) or string at line 63

I have written a code and I am not sure what the error is, i am getting the error

Use of uninitialized value in concatenation (.) or string at line 63
and my code is as follows

for(my $j = 0; $j < $num2; $j++) {
print {$out} "$destination[$j]|$IP_one_1[$j]|$IP_one_2[$j]|$reached[$j]|$IP_two_1[$j]|$IP_two_2[$j]\n";`

Answer Source

What it means is that one of the elements of either @destination, @IP_one_1, @IP_one_2, or @reached has not been defined, or has been assigned a value of undef. You either need to detect (and prevent) undefined values at the source, or expect and deal with them later on. Since you have warnings enabled (which is a good thing), Perl is reminding you that your code is trying to concatenate a string where one of the values being concatenated is undefined.

An example of where you might investigate is one or more of the arrays is of a different total size than the others. For example, if @IP_one_2 has fewer elements than the others, or if $num2 is a value greater than the number of elements in any of the arrays.

Place the following near the top of your script and run it again:

use diagnostics;

When I run the following one-liner under warnings and diagnostics:

$ perl -wMdiagnostics -e '$a=$a; print "$a\n"'

I get the following output, and you will get something similar if you add use diagnostics;... a very helpful tool when you're first learning Perl's warnings.

Use of uninitialized value $a in concatenation (.) or string at -e line 1 (#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 will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program anid 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download