foreach my $FH (@files) { open my $FILE, '<', $FH or die("Cannot read $FH: $!"); my $data = join('', <$FILE>); # slurp file! # split on lines containing only 'GO' (and optionally, whitespace) foreach my $batch ( split /\n\s*go\s*\n/i, $data ) { # if you need the 'go', you'll need to do $batch.="\nGO"; here $dbh->do($batch); # error handling here } }