Close.
last OUTER;
should be
last OUTER unless $line = <$FH>;
You only want to do it when the end of the file is reached.
Update: Fixed these problems: $FH indicated a file handle but contained a filename, $FH was used as a file handle even though FILE was the file handle, $batch was not initialized, and $line was uselessly checked for "go" twice.
foreach my $filename (@files) {
open(my $fh, '<', $filename)
or die("Unable to open input file $filename: $!\n");
my $batch = '';
OUTER: while (my $line = <$fh>) {
while (not $line =~ m/^\s*go\s*$/i) {
$batch .= $line;
last OUTER unless $line = <$fh>;
}
$dbh->do($batch);
}
}
or without nested loops:
foreach my $filename (@files) {
open(my $fh, '<', $filename)
or die("Unable to open input file $filename: $!\n");
my $batch = '';
while (my $line = <$fh>) {
if (not $line =~ m/^\s*go\s*$/i) {
$batch .= $line;
next; # Go read next line.
}
$dbh->do($batch);
$batch = '';
}
}
|