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

Viewed 76954

I have written some code, and I am not sure what the error is. I am getting the error:

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

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";`
}
3 Answers

Maybe my example will be useful to someone. Suppose variable $x is initialized from a database. It may contain an undefined value, and this is normal. We need to display its value on the console. As responsible programmers, we decided to use "use warnings FATAL => "all";". In this case, the script will fail.

perl -e 'use strict; use warnings FATAL => "all"; my $x; print("x=$x\n"); print("DONE\n");'

Returns:

Use of uninitialized value $x in concatenation (.) or string at -e line 1.

In this case, you can use

if(defined($x)){...}else{...}

But this is not pretty if just want to print a value.

perl -e 'use strict; use warnings FATAL => "all"; my $x; print("x=".($x//"null")."\n"); print("DONE\n");'

Prints:

x=null
DONE

Because the expression $x//"null" checks whether what comes before // is defined and if it is not defined returns what comes after //.

Related