in reply to do until loop breaks before meeting the condition
and it works as expected for me: it loops when I enter strings such as "foo" or "bar" and exits the loop if I enter "end" or "END".use strict; use warnings; use constant MAXBYTES => scalar 5; my $result; $| = 1; MSG: print "Please be aware maximum length 255 characters!:\n"; do { print "Send this text to clients:\n\n"; chomp ($result = <STDIN>); if (length($result) >= MAXBYTES) { print "\nYou have reached the limit of characters ".length($result +)."!\n\n"; goto MSG; } } until ($result eq "END" or $result eq "end"); $| = 1;
Having said that, there are a few problems in this code. One is that the message states:
but MAXBYTES is set to 5. The second one is the use of goto, this is widely considered to be a very bad programming practice, especially when the goto brings you upward in your code and out of a loop, and this is pretty useless in this case. Try something like this instead:print "Please be aware maximum length 255 characters!:\n";
although this is still somewhat clunky (but I wanted to stay close to your code). One additional question: why are you setting $| twice to the same value?use strict; use warnings; use constant MAXBYTES => scalar 5; my $result; $| = 1; my $msg = "Please be aware maximum length 255 characters!:\n"; do { print "Send this text to clients:\n\n"; chomp ($result = <STDIN>); if (length($result) >= MAXBYTES) { print "\nYour input has a length of " . length($result) . "; +the limit is ". MAXBYTES, "\n\n"; print $msg; } } until (uc ($result) eq "END"); $| = 1;
On the problems with the goto function, please read this classical text: http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: do until loop breaks before meeting the condition
by LanX (Saint) on Jun 08, 2014 at 16:28 UTC | |
by thanos1983 (Parson) on Jun 08, 2014 at 18:50 UTC | |
by Laurent_R (Canon) on Jun 09, 2014 at 06:22 UTC | |
|
Re^2: do until loop breaks before meeting the condition
by thanos1983 (Parson) on Jun 08, 2014 at 18:33 UTC |