in reply to Annoying 'Use of uninitialized value in concatenation' warning

If you want to make the undef value obvious in the output then you could:

use warnings; use strict; use 5.010; my @some_array = (1, 2, undef, 4, 5); for (my $ii = 0; $ii < scalar(@some_array); $ii++) { print "Value at position $ii: @{[$some_array[$ii] // '--undef--']} +\n"; }

Prints:

Value at position 0: 1 Value at position 1: 2 Value at position 2: --undef-- Value at position 3: 4 Value at position 4: 5
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Annoying 'Use of uninitialized value in concatenation' warning
by NoobForever (Initiate) on Feb 04, 2015 at 17:36 UTC
    another option without concatenation:
    print "Value at position $ii: ", $some_array[$ii] || '--undef--',"\n";

      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:  <%-(-(-(-<