in reply to Can't quit my until loop
Consider your code with some indentation added
#!/usr/bin/perl -w use strict ; use diagnostics; use LWP::Simple ; print "This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop:\n\n" ; my $answer ; my $quit = 0 ; until ($quit) { getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; print "Do you want to continue? " ; $answer = <STDIN> ; chomp $answer ; } if ($answer ne 'y') { print "OK\n" ; } else { $quit = 1 ; die "OK!\n\n" ; } print "Good-bye!\n\n" ;
I have indented your code as I normally would. The problem as you can see is that you test the conditional outside the until loop. No test == No exit. You could also simplify a lot to this:
#!/usr/bin/perl -w use strict ; use diagnostics; use LWP::Simple ; print "This will get the time from an NIST clock 5 times and then ask you if you want to continue. Use 'y' to continue and any other key to stop:\n\n" ; my $answer = 'y' ; until ($answer !~ /^y$/i ) { getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; getprint("http://132.163.4.101:13") ; print "Do you want to continue? " ; $answer = <STDIN>; chomp $answer; } print "Good-bye!\n\n" ;
I like this structure a lot more as it is simple and skips extra vars. Less code == Less bugs. By using a regex match we allow a little more flexibility with lower case or capital 'Y' acceptable.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|