open(my $fh_in, '<', "filein.dat") or die("Unable to open filein.dat: $!\n"); my $fh_out; my $file_count = 0; while (<$fh_in>) { if ($. % 30 == 1) { open($fh_out, '>', "file${file_count}.dat") or die("Unable to create file${file_count}.dat: $!\n"); ++$file_count; } print $fh_out ($_); }