Bismark has asked for the wisdom of the Perl Monks concerning the following question:

This may seem to be a real foolish question, but here it is. In the following code I would like the user to have a second chance to enter in the part number, I thouhg redo would do this. Is it possible or am I way off base to start with? Thanks so much for any help.
print "This program sends a query to the First Article Inspection Data +Base folder using the Part Number and Revision given by the user. NOTE - the user must have already logged in via the passsword entry screen, which sets the permission. The database query will be read from standard input.\n\n"; #This is where the user will enter the Part Number and Revision Level print "What is the Part Number that you would like to look up?\n"; my $PartNumber = <STDIN>; if ($PartNumber eq "\n") { print "There was no input"; } else { print "What is the Revision level for $PartNumber"; my $Revision = <STDIN>; redo }
credo quia absurdum

Replies are listed 'Best First'.
Re: using redo in an if/else statement
by davorg (Chancellor) on Jan 14, 2003 at 17:31 UTC

    You're not running with use warnings are you? If you were, you would see the message:

    "Can't 'redo' outside a loop block"

    If you then turned on use diagnostics you'd see even more useful information.

    "A 'redo' statement was executed to restart the current block, but there isn't a current block. Note that an 'if' or 'else' block doesn't count as a 'loopish' block, as doesn't a block given to sort(), map() or grep(). You can usually double the curlies to get the same effect though, because the inner curlies will be considered a block that loops once. See perlfunc/redo."

    So, to summarise - you can't do that :)

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: using redo in an if/else statement
by Fletch (Bishop) on Jan 14, 2003 at 17:29 UTC

    redo works inside loops. An if isn't a loop. Wrap the whole thing in something along the lines of:

    while( 1 ) { ## prompt user if( $whatever eq 'done' ) { last; } if( $whatever eq 'try again' ) { next; } ## deal with other cases }
Re: using redo in an if/else statement
by thatguy (Parson) on Jan 14, 2003 at 17:36 UTC
    Maybe you cannot do it with redo but you can with while
    #!/usr/bin/perl use strict; print "What is the Part Number that you would like to look up?\n"; my $PartNumber = <STDIN>; chomp($PartNumber); while (!$PartNumber){ ## while $PartNumber is empty keep asking print "PartNumber? "; $PartNumber = <STDIN>; chomp($PartNumber); } print "What is the Revision level for $PartNumber?\n"; my $Revision = <STDIN>; chomp($Revision); while (!$Revision){ ## while $Revision is empty keep asking print "Revision? "; $Revision = <STDIN>; chomp($Revision); } print "PartNumber: $PartNumber\n"; print "Revision: $Revision\n";

    -phill
      Thank you for the hints to the right direction! As you can see, I am a bit of a novice with this great language. With help like I have received at the Monastery I will soon be on my way.
      Thanks again!

      Kerry

      credo quia absurdum
Re: using redo in an if/else statement
by Zaxo (Archbishop) on Jan 14, 2003 at 17:57 UTC

    Yes, redo is good for that, but the if{}else{} control is less good. How about factoring the message out of an input sub?

    sub get_input { { print @_; my $input = <STDIN>; chomp $input; redo unless $input; $input; } } my $PartNumber = get_input 'What is the Part Number that you would like to look up?', $/; my $Revision = get_input 'What is the Revision level for ', $PartNumber, '?', $/; # go on to verify that those make sense, redo if they don't

    After Compline,
    Zaxo

Re: using redo in an if/else statement
by pg (Canon) on Jan 14, 2003 at 21:23 UTC
    I was trying to reply this post, but then...I myself become really confused.

    I first wrote "redo works with all 'iterators', like for, while, do", then I started to coding and test, however I was stuck with this piece of code:
    use strict; my $i = 1; my $flag = 1; do { print "$i "; if (($i ++ == 5) && $flag) { $flag = !$flag; #redo; } } until ($i >= 10);
    If I uncomment that redo, perl gave me "can't redo outside a loop block". (Perl was fine with for and while)

    Did I miss something obvious, or that is just the way perl goes, (if that is the way perl goes, I don't buy their logic at all, "do {} unitl" is not a loop?)

      I was bitten by that one too a while ago. do {...} until (...); looks like a loop but isn't one since until plays the role of a modifier and isn't a statement. Have a look at this for official confirmation.

      Hope this helps, -gjb-

Re: using redo in an if/else statement
by JamesNC (Chaplain) on Jan 15, 2003 at 03:49 UTC
    It is clearer to use LABELS with redo and next... it only is used in the context of BLOCKS vs what you were trying to do inside a single statement. Get a copy of Randal Schwartz's Perl Book It will help loads.
    THAT: { # Ask for input print "What is the Part Number that you would like to look up?\n"; # Get it my $PartNumber = <STDIN>; # See if it's empty print "There was no input" if ($PartNumber eq "\n"); # Execute next Block THIS: { print "What is the Revision level for $PartNumber"; my $Revision = <STDIN>; # Pattern match or test until finished redo THIS if($Revision =~/rev/g); last THAT if($Revision=~/bored/g); # or Repeat the part look-up again redo THAT; } }
    I left using next to your own discovery...