As other point out there are better ways to do it.
Your error is
my @data = $_[0]..$_[scalar(@_)-2];
It means take the first argument from @_ and the second last one. Then fill the new array @data with 1..9 and sort that.
What you want is my @data = @_[0..scalar(@_)-2];