in reply to Re: Annoying 'Use of uninitialized value in concatenation' warning
in thread Annoying 'Use of uninitialized value in concatenation' warning

another option without concatenation:
print "Value at position $ii: ", $some_array[$ii] || '--undef--',"\n";
  • Comment on Re^2: Annoying 'Use of uninitialized value in concatenation' warning
  • Download Code

Replies are listed 'Best First'.
Re^3: Annoying 'Use of uninitialized value in concatenation' warning
by AnomalousMonk (Archbishop) on Feb 04, 2015 at 18:00 UTC

    The problem with using the  || (logical-or) operator (see perlop) in this case is that the undefined value is false, as are 0, '0' and '' (the empty string), and  || does not distinguish. If your Perl version is 5.10+, you can use the  // (defined-or) operator; otherwise, you're stuck with syntax that's a bit more messy:

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my @ra = (0, 0, 0); ;; my $i = 1; print qq{value at index $i: }, $ra[$i] || 'undef'; print qq{value at index $i: }, $ra[$i] // 'undef'; print qq{value at index $i: }, defined($ra[$i]) ? $ra[$i] : 'undef' " value at index 1: undef value at index 1: 0 value at index 1: 0


    Give a man a fish:  <%-(-(-(-<