Hi, I have just tried the following code:
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;
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".

Having said that, there are a few problems in this code. One is that the message states:

print "Please be aware maximum length 255 characters!:\n";
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:
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;
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?

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.


In reply to Re: do until loop breaks before meeting the condition by Laurent_R
in thread do until loop breaks before meeting the condition by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.