1
BEGIN {
   use FindBin;
   $scriptsDir = $FindBin::RealBin;
}

 sub print_log {
  ($log, $msg) = ($_[0], $_[1]);
  print $log $msg;
     }


$opt_rh_gsr = "path_to_file";
open(FO, "$opt_rh_gsr") || die "-F-: Can not open file \n";
while(<FO>) {
  if(/vdd_nets/) {
          $vdd_net = 1;
          $vdd_string = "VDD_NETS \{ \n";
               }
  if(/gnd_nets/) {
          $gnd_net = 1;
 }
     if(($gnd_net == 1)) {
     chomp();
     $new_line = $_;
     @split_new_line = split(":", $new_line);
     }
   if(($gnd_net == 1) && /\}/) {
      $gnd_net = 0;
      $gnd_string .= "\} \n";
      exit;
       }
  if($vdd_net) {
    if(/^\s*\S+\s+\S+\s+{/) {
      $paren++;
    }
    if (0 != $paren && /^\s*(\w+)\s*$/) {
     $vdd_nets{$1} = $parenvolt;
      next;
    }
    if(/^\s*}\s*$/ || /^\s+$/) {
      if (0 == $paren) {
        $vdd_net = 0; next;
      }
      else {
        $paren--;  next;
      }
    }
    chomp();
    if(/\s*\}\s*$/ && ($vdd_net == 1)){
    s/\'//g;
    $vdd_net = 0;
    @_ = split(":");
    $vdd_string .= "$_[0]  $_[1] \n";
    $vdd_string .=  "\} \n";
    next;
    }
      
  if($gnd_net) {
    if(/^\s*\}\s+$/ || /^\s+$/) {
      $gnd_net = 0;
      next;
    }
    #chomp();
    if(/\s*\}\s*$/ && ($gnd_net == 1)){
    s/\'//g;
    $gnd_net = 0;
    }
    @_ = split();
    $GNDNET = $_[0];
    if ($_[0] =~ /^\w+$/) {
      $groundnets{$_[0]} = 1;
     
    }
  }
}
}
print " done reading \n";
close(FO); 

print "closed file \n";

The above is not printing the last 2 print statement (before and after the close of file handle). I tried print STDOUT, that didn't work. I also tried to flush, that didn't work either.

The script is exiting after executing, so it is not stuck in a infinite loop anywhere. I tries using perl5.6 and 5.8, but both of them have the same problem.

  • Next time you ask a question, please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). I got lucky here and spotted the `exit` quickly, but even then, I can't be 100% sure that this is the error, because I don't know what your input file is. Moreover, your code is 144 lines long. Next time, try to reduce your code. For instance, you should have removed some `if`s from the loop until the issue was gone. By doing that, you probably would have noticed that removing the `if` with the `exit` was solving your problem. – Dada Mar 23 '22 at 07:34
  • That code does not look very healthy. For one thing, doesn't use `use strict; use warnings`, does not even use lexical variables. Includes a *lot* of modules but does not seem to use many of them. The `Switch` module I believe is not a recommended one. And those Perl versions are 20 years old. You should seriously consider rewriting this code. – TLP Mar 23 '22 at 09:28

1 Answers1

3

To exit a loop, you should use the keyword last instead of exit (which exits the whole program). This if:

if(($gnd_net == 1) && /\}/) {
    $gnd_net = 0;
    $gnd_string .= "\} \n";
    print "exiting loop $gnd_string \n";
    exit;
}

Should thus be:

if(($gnd_net == 1) && /\}/) {
    $gnd_net = 0;
    $gnd_string .= "\} \n";
    print "exiting loop $gnd_string \n";
    last;
}

(unless you actually wanted to exit the program, in which case the print should rather have been print "exiting program...")


A few tips:

  • Always add use strict and use warnings at the beginning of your scripts. It will catch many mistakes and save you a lot of time.

  • Use 3-operand open to open files (ie, open FILEHANDLE,MODE,EXPR instead of open FILEHANDLE,EXPR), and lexical filehandles (ie, $FO instead of FO). Your open should thus have been: open my $FO, '<', $opt_rh_gsr instead of open(FO, "$opt_rh_gsr").

  • Adding || die "-F-: Can not open file \n" after open is a good idea, but 1) you should do or die instead of || die (in this specific case it doesn't matter, but with or rather than ||, you can omit the parenthesis around open's arguments), and 2) you should add the name of the file you were trying to open (in that case, you'd print die "-F-: Can not open file '$opt_rh_gsr'). 3) add $! to the die to have the error message (die "-F-: Can not open file '$opt_rh_gsr': $!). And 4), as suggested by TLP, don't add a newline at the end of a die string.

  • sub print_log { ($log, $msg) = ($_[0], $_[1]); ... could have been sub print_log { ($log, $msg) = @_;; it's a bit more idiomatic and concise.

  • Indent properly your code. It's possible that indentation was lost in the copy-paste, but, if it's not the case, then you should indent better your code. This will save you a lot of time when writing/reading your code, and will save other people even more time when they'll read your code. Most IDEs have indentation features that can help you indent the code.

Dada
  • 6,313
  • 7
  • 24
  • 43
  • 1
    `...die "Cannot open '$foo' for reading: $!"` There's more to add there: 3) Don't use newline at the end of a `die` string unless you want to suppress the line number of the statement, 4) you should almost certainly always include the error `$!`. Also, you may wish to be specific about the open mode. – TLP Mar 23 '22 at 11:14
  • 1
    @TLP Right thanks, I definitely wanted to add `$!` but probably got lost in my thoughts by the end of writing this sentence. – Dada Mar 23 '22 at 11:25