my $suffix = 'z'; my $sequence = 0; my $maxseq = 1000; my $input = shift @ARGV or die "usage"; open my $ifh, $input or die "$!: $input\n"; my $ofh; while( <$ifh> ) { /\AINPUT\sSEQUENCE/ and SwitchFile( $input, \$ofh, \$suffix, \$sequence, $maxseq ); $ofh or die "Unexpected prelude: $_"; print $ofh $_; } close $ofh; sub SwitchFile { my ( $input, $oref, $sref, $qref, $max ) = @_; if ( defined( $$oref ) ) ( ++$$qref < $max ) and return; $$qref = 0; close $$oref; } my $newfile = "$input." . ++$$sref; open my $ofh, ">$newfile" or die "$!: $newfile"; $$oref = $ofh; }