简体   繁体   中英

perl subroutine from reading STDIN (“Press enter to reset”) only works the first time

I have made a little perl program for a kids demonstration and I'm trying to keep the screen clean and simple with a clear subroutine, but it's only working the first time, and then will not respond to input, only clearing the screen, and I can't work out why. I'm also sure that there's a simpler way to do this in general, so maybe the whole thing should be scrapped.

There is a prompt for a query from STDIN, then once it's checked the database it calls the restart sub.

What I have:

while (my $seq = <STDIN>){
  chomp($seq);
  if (exists $references{$seq}){
    ... blah blah blah
    sleep(2);
    &restart;
  }else{
    print "\nOops! This  doesn't exist!\n";
    sleep(2);
    &restart;
  }
}
sub restart {
  print "\nPress Enter to restart\n";
  while (my $in = <STDIN>){
   if($in eq "\n"){
     system("clear");
     print $prompt;
   }
  }
}

Thanks for looking!

In sub restart you are looping on <STDIN> again. This will continue to loop, without returning to the main loop, until STDIN receives an EOF. You could put a return after print $prompt; to return to the main loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM