perlquestion
Eily
<p>Hello fellow monks. This is actually the first time I end up posting here, because until now, I always found my answer while writing my question, or just before clicking "create". You were all my [https://en.wikipedia.org/wiki/Rubber_duck_debugging|rubber ducks] :). But I can't understand this one.</p>
<p>On the version of perl I have here (strawberry v5.20), this code yields two warnings:
<c>use strict;
use warnings;
use List::Util qw( min );
use Data::Dump qw( pp );
my (@_x, @_y);
my @one = 1;
my @two = 0.5;
pp { one => $#one, two => $#two };
pp @one, @two;
for my $_in (1..5)
{
push @_x, "$_in";
my $_out = 0;
#pp "$#_x", "$#one", min $#_x, $#one;
#pp "$#_y", "$#two", min $#_y, $#two;
my $x_max = min $#_x, $#one;
my $y_max = min $#_y, $#two;
pp { x => $x_max, y => $y_max };
$_out += $one[$_] for 0..$x_max;
$_out -= $two[$_] for 0..$y_max;
push @_y, $_out;
}
pp { one => $#one, two => $#two };
pp @_y;
</c>
<blockquote>Use of uninitialized value in addition (+) at test.pl line 28<br/>
Use of uninitialized value in subtraction (-) at test.pl line 29</blockquote>
Unless I'm mistaken, this means I'm reaching for elements outside of @one and @two, even though $x_max and $y_max should not exceed the highest index.
</p>
<p>I don't understand how with <c>$#one == 0</c> and <c>$x_max = min $#_x, $#one</c> I find that <c>$x_max > 0</c>. What surprises me even more is that the warning disappears when I add the two commented lines (removing the quotes shows that min returns an alias and not a copy of the value).</p>
<p>Did I miss something obvious, or is this some weird side effect of my code?</p>