use strict; use warnings; my $num = 1; my $recLines = 2; while (!eof DATA) { ## Open the file once every iteration my $outfile = sprintf("Int_%s.txt", $num); print "---> Create file $outfile\n"; printf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s\n", "H", $outfile, "20101221", "1230", $num, $., " "); for (1 .. $recLines) { last if eof DATA; last if ! defined (my $line = ); print $line; } printf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s\n", "T", $outfile, "20101221", "1230", $num, $., " "); print "<--- Close file $outfile\n"; ++$num; } __DATA__ 1234567|HP|GASS 1234567|TP|GASS 3456789|YU|GASS 3245678|TY|GASS #### ---> Create file Int_1.txt HInt_1.txt 2010122112301 0 1234567|HP|GASS 1234567|TP|GASS TInt_1.txt 2010122112301 2 <--- Close file Int_1.txt ---> Create file Int_2.txt HInt_2.txt 2010122112302 2 3456789|YU|GASS 3245678|TY|GASS TInt_2.txt 2010122112302 4 <--- Close file Int_2.txt #### # input handle: open my $inFile, '<' $source or die "Can't open '$source' $!"; # output handle: open my $outFile, '>' $target or die "Can't open '$target' $!";