in reply to Re^3: readline() on unopened filehandle
in thread readline() on unopened filehandle

This code hangs up after the bit that prints 'I am here'. When I say 'hangs up' I mean it just keeps on running without finishing the program. I just want it to run the SPROC $FH.
while (<*.sql>) { next if $_ eq 'Activate.sql'; $file = $_; open (FILE, "$file"); my $line_number = 0; my $run; LINE: while (<FILE>){ my $line = $_; $line_number = $line_number +1; $run = 1; if ($line_number == 1){ $run = 0 if $line !~ m/^ALTER\sPROCEDURE\s/; last LINE; } else { $run = 1; } last LINE; } if ($run == 1){ push (@files, $file); } else { #Do nothing } } print "@files"; foreach (@files) { my $FH = $_; open (FILE, "<$FH"); OUTER: while ((<FILE>)) { my $line_B = $_; print "\n\nI am here\n"; last if eof; my $batch; do { $batch .= $line_B; } until ($line_B =~ m/^\s*go\s*$/i); $dbh->do($batch) if ($line_B =~ m/^\s*go\s*$/i); last if ($line_B =~ m/^\s*go\s*$/i); } }

Replies are listed 'Best First'.
Re^5: readline() on unopened filehandle
by fishbot_v2 (Chaplain) on Oct 07, 2005 at 14:26 UTC
    do { $batch .= $line_B; } until ( $line_B =~ m/^\s*go\s*$/i );

    I suspect that your program would eventually die of memory exhaustion, assuming $line_B contains something.

    Let's do a bit of detective work:

    • How would this loop ever terminate, unless your file started with "go"? hint: it won't
    • What does this node have to do with the one that it replies to?
      Your right. I should think about code that is given to me before implementing it. Thanks. That bit now looks like this:

      foreach (@files) { $FH = $_; open (FILE, "<$FH"); OUTER: while ((<FILE>)) { $line_B = $_; $batch .= $line_B; last if eof; last if ($line_B =~ m/^\s*go\s*$/i); } $dbh->do($batch) if ($line_B =~ m/^\s*go\s*$/i); }

        What about:

        foreach my $filename ( @files ) { open ( FILE, "<", $filename ) or die( "Couldn't open $filename: $!" ); my $batch = ""; INNER: while ( <FILE> ) { $batch .= $_; last INNER if eof || m/^\s*go\s*$/i; } $dbh->do( $batch ) if ( $batch =~ m/^\s*go\s*$/msi ); }

        It has the advantage of resetting $batch each time, rather than executing the batch from the first file for every subsequent file.

        And what do you know? This looks an awful lot like ikegami's code in Re^4: ALTER SPROC activation, which was the answer you got the last time you asked this question.

        A reply falls below the community's threshold of quality. You may see it by logging in.