use File::Temp; my $file = shift @ARGV; my $low=1; chomp(my $high = `wc -l $file`); print "File is $high lines\n"; my %tried; while (my $next=int(($high-$low)/2)+$low) { last if $tried{$next}++; print "low: $low high: $high next: $next\n"; my $tmp = File::Temp->new(TEMPLATE => "tmp_XXXXXX", DIR=>"."); open(my $fh, $file) or die "Can't open $file: $!"; while (<$fh>) { print $tmp "__END__\n" if $. == $next; print $tmp $_; } close $fh; $tmp->close(); my $out = `perl -c $tmp 2>&1`; ( $out =~ /syntax OK/ ? $low : $high ) = $next; }