http://qs1969.pair.com?node_id=1152601

Eily has asked for the wisdom of the Perl Monks concerning the following question:

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 rubber ducks :). But I can't understand this one.

On the version of perl I have here (strawberry v5.20), this code yields two warnings:

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;
Use of uninitialized value in addition (+) at test.pl line 28
Use of uninitialized value in subtraction (-) at test.pl line 29
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.

I don't understand how with $#one == 0 and $x_max = min $#_x, $#one I find that $x_max > 0. 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).

Did I miss something obvious, or is this some weird side effect of my code?