You can read the positions into an array by themsevels, and then open the second file and iterate over the array to find lines where the positions are enclosed within the ranges. That way you open each file only once
use strict;
use warnings;
open(my $fh1, "<","positions.txt") or die("could not open file $!\n");
my @positions; #hold the positions to be compared
while(my $line=<$fh1>){
chomp $line;
push @positions,$line;
}
open(my $fh2, "<","coords_orientation.txt") or die("could not open fil
+e $!\n");
while(my $line=<$fh2>){
chomp $line;
my @record=split(" ",$line); #split the coords_orientation.txt on
+white space
foreach my $pos (@positions){
if($pos > $record[0] && $pos <$record[1]){
print "$pos @record\n";
}
}
}