Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: do until loop breaks before meeting the condition

by Laurent_R (Parson)
on Jun 08, 2014 at 15:40 UTC ( #1089193=note: print w/ replies, xml ) Need Help??


in reply to do until loop breaks before meeting the condition

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.


Comment on Re: do until loop breaks before meeting the condition
Select or Download Code
Re^2: do until loop breaks before meeting the condition
by LanX (Canon) on Jun 08, 2014 at 16:28 UTC
    ehm ... he's using goto as loop control, last, next and redo are not much different.

    Than you don't print the $msg before first run like he did.

    Considering "cleanest" and "DRYest" flow control , I'd rather try this

    > perl $max_length=5; OUTER: { print "Input max is $max_length!!!\n"; while (1) { chomp( $input = <STDIN> ); last OUTER if lc( $input ) eq "end"; redo OUTER if length( $input ) > $max_length; print "- $input is OK\n"; } } __END__ Input max is 5!!! 1 - 1 is OK 12345 - 12345 is OK 123456 Input max is 5!!! 123 - 123 is OK end >

    YMMV! :)

    update

    please note that the more verbose

    OUTER: while(1) { ...

    works equally well and might be less surprising for some maintainers. :)

    Cheers Rolf

    (addicted to the Perl Programming Language)

      To: Lanx,

      Thanks, I was not even aware of: last, next and redo.

      Hmmm based on second look your solution looks simple and I ques better than mine. I think I will follow it.

      Again thank you for your time and all this information that you shared.

      Seeking for Perl wisdom...on the process...not there...yet!
      Well, I am not an anti-goto fanatic, the comment on gotos was just one out of 3 comments. I still think it is much better to use regular loop control statements (last, next, etc.), and it is most of the time best to avoid gotos altogether (except for the special goto &name construct).
Re^2: do until loop breaks before meeting the condition
by thanos1983 (Monk) on Jun 08, 2014 at 18:33 UTC

    To: Laurent_R,

    Thanks for the information I was not aware that goto is bad coding.

    I removed it and followed your example.

    P.S.: The MAXBYTES = 5 is just for fast implementation purposes.

    P.S.2 About the $| = 1; I thought that it would be good to flush the buffer also at the output. Maybe I was just over doing it.

    Again thank you for your time and effort.

    Seeking for Perl wisdom...on the process...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1089193]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-09-18 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls