You can add -n to the first line of your script and set all needed variables in the BEGIN{} block (which is not looped like all other code when using -n):
#!/usr/bin/perl -wn
BEGIN{
use strict;
my $outFile= "output.txt";
my $inFile= "infile.txt";
$main::start=17; # 'my' in this scope will result in uninitialized va
+lues outside the BEGIN{} block
$main::end=30;
close STDOUT; close STDIN;
open STDIN,"<",$inFile || die $!;
open STDOUT,">",$outFile || die $!;
}
print if $. >= $start && $. <= $end;
Sorry if my advice was wrong.