Compare and contrast:

# use this form of foreach instead of 'my $FH = $_' foreach my $FH (@files) { # use 3-arg form of open() for safety, and deal with # being unable to open the file. open my $FILE, '<', $FH or die("Cannot read $FH: $!"); # deal with each line of the file. while loop exits # when EOF is reached. my $batch; OUTER: while (<FILE>) { $batch .= $_; # cat this line to $batch # if we just read a line that's 'GO', run the batch and # reset the variable $batch to be empty if (m/^\s*go\s*$/i) { $dbh->do($batch); # you should check for and deal with DBI errors here $batch = ''; } } # if we didn't reach a 'GO', there will be stuff in $batch, so # we should report that fact. warn "File '$FH' contained unexecuted commands:\n$batch\n"; }

Once you get that down, it becomes much easier to explore other algorithms, like this one:

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 } }
<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re: readline() on unopened filehandle by radiantmatrix
in thread readline() on unopened filehandle by Win

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.