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

How to make script very interactive, Script purpose, Get user entered number and find mod, if it is 1 than say "good " and exit the script. if it returns 0, Than ask user to enter odd number,

Validation: 1. accept only numbers, no character,no decimal,no special characters.
2. do not end the script until user has entered the odd number,

Following code works fine, it follows above validation point 1 rules, but it is not very interactive as per our requirement law, when above rules fails, script goes exit mode. and need to execute script again. How make following code very interactive.

$ip_no = $ARGV[0]; if ($ip_no!="") { $get_mod_op = get_mod_val($ip_no); if ($ip_no =~ /\D/){ print "Entered number only....."; exit; }else{ if ($get_mod_op==1) { print "Good odd number(${get_mod_op}): ${ip_no} "; exit; }else{ print "Good even number(${get_mod_op}): ${ip_no} "; exit; } } }else{ print "Enter number."; exit; }
Thank to all,

finally i completed the script, here i am pasting my updated current script, Let me know if you found any bug in this code,

if ($ARGV[0] != "") { $user_ip = $ARGV[0]; }elsif (($ARGV[0] =~ /\D/ ) || ($ARGV[0] eq "") ){ print "Enter odd number only: "; $user_ip = <STDIN>; }else{ $user_ip = <STDIN>; } do{ #get mod value, if mod_off is 1 then entered number is odd otherwi +se ask user to enter the odd number $mod_off = find_mod_val($user_ip); if (($user_ip == "") || ($user_ip eq "") || ($user_ip eq "") || ($ +mod_off == 0)) { print "Enter odd number only: "; $user_ip = <STDIN>; } }until($mod_off == 1); print "Good odd number ${user_ip}"; __END__

Replies are listed 'Best First'.
Re: reprompt user input, until validation pass
by davido (Cardinal) on Oct 17, 2011 at 05:22 UTC

    Surely your teacher wouldn't have given you this assignment without first discussing 'while' loops. See perlsyn.

    And why all the 'exit' calls?


    Dave

      $ip_no = $ARGV[0]; chomp($ip_no); print "$ip_no\n"; $flag=0; while(!$flag) { if($ip_no =~ /\D/ || $ip_no=~ //) { print "enter number\n"; $ip_no=<STDIN>; chomp($ip_no); } else { if ($ip_no%2 ==0) { print "well done bye bye\n"; $flag=1; } else { print "u r expected to enter an odd number\n"; $ip_no=<STDIN>; chomp($ip_no); } } }
        if($ip_no =~ /\D/ || $ip_no=~ //)

        That is the same as saying:

        if($ip_no =~ /\D/ || $ip_no=~ /\D/)

        Because an empty match operator uses the last successfully matched regular expression.

        snippet keep on asking enter number, not continuing further, even i tried to pass argument while executing script.
Re: reprompt user input, until validation pass
by GrandFather (Saint) on Oct 17, 2011 at 05:20 UTC

    You probably want a while loop and to prompt for the $ip_no value rather than pass it on the command line. You can prompt using print of course, and you can read a line from stdin using <>:

    my $ip_no = <>; chomp $ip_no;

    Note the use of chomp to remove the line end character from $ip_no.

    Most of your existing code then simply gets wrapped up in the while loop. You need to decide at each place you currently use exit if you are done (the two good cases) in which case you replace the exit with last. For the cases where you want to prompt for another number replace exit with next.

    Note too that you should always use strictures (use strict; use warnings;).

    True laziness is hard work