in reply to filtering data and while loop problem

When you do

last unless $line =~ m/^\d/;

in your get_values subroutine, it keeps reading all the lines and stops when it finds a line which is not starting with a digit. This line is

variableStep chrom=chr2

in your __DATA__ section. Now when the code flow go back to

while ( defined(my $line= <DATA> ) and !@values )

in foreach loop, the $line gets the line

9837    0.090

which, I think, is not what you are expecting.