http://qs1969.pair.com?node_id=611938

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

hallo there,
i need to enable strict and disable it in the same code like this.


use strict; while(1) { print "please enter a guess from 1 to 100: "; chomp(my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print " sorry you gave up.the number was $secret.\n"; last; } no strict; elsif ($guess < $secret) { print "you failed \n"; }
can i use it this way?

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: how to enable, disable strict
by shmem (Chancellor) on Apr 25, 2007 at 08:41 UTC
    You've got it right in that strict is enabled with use strict and disabled with no strict - but:
    It is an error to put anything but whitespace between the if block and the elsif keyword. Maybe you want
    use strict; while(1) { print "please enter a guess from 1 to 100: "; chomp(my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print " sorry you gave up.the number was $secret.\n"; last; } elsif ($guess < $secret) { no strict; print "you failed \n"; } }

    - but why?

    Just leave it enabled. In normal coding you always want to have it; in fact, there are only few situations where some strictness must be disabled, such as hacking perl internals. As the strict documentation states, it is a "pragma to restrict unsafe constructs" - and it is always better to be on the safe side.

    BTW, had you indented your code properly, you would have seen that you missed the last right curly... ;)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Oh i missed that -- now its working thanx alot mate, and `bout the last curly guess i missed it when i typed the script in here and added <#BR#> for the html script :-)
Re: how to enable, disable strict
by marto (Cardinal) on Apr 25, 2007 at 09:31 UTC
    A couple of things in addition to the advice shmem has already given. Firstly you are not checking if the number the user has guessed is the secret number, just that it is a lower number, and you never tell the user that they have guessed correctly. Try the code below:
    use strict; use warnings; my $secret=42; my $guess; while(1) { print "please enter a guess from 1 to 100: "; chomp($guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i){ print " sorry you gave up.the number was $secret.\n"; last; } if ($guess != $secret) { print "Wrong! Guess again.\n"; }else{ print "Correct!\n"; last; } }
    In your example if the secret number was 42 and the user enters 43 they are not told that they have failed to guess correctly.
    Please read the PerlMonks FAQ and How do I post a question effectively? if you have not alreay done so.

    Hope this helps

    Martin
      now i have the code completed -- but when a user guess the right number it keeps asking for the same guess again and again , my question is : how can i restart the program automaticaly with a new secret number but without quiting . here is the code
      #!/usr/bin/perl -w use warnings; use strict; my $secret = int(1 + rand 100); while(1) { print "please enter a guess from 1 to 100: "; chomp(my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print " sorry you gave up.the number was $secret.\n"; last; } elsif ($guess < $secret) { no strict; print "you failed ,try higher\n"; } elsif ($guess > $secret) { print "you failed , try lower\n"; } elsif ($guess == $secret) { print "you got it\n"; } }
      Sorry again for the mistakes i made concerning where should i add my question , im new here and new for PERL in general. cheers,

        There are many ways to achieve that. An easy way is to repeat the code that generates of the number in the success block. Here's a slightly different way to do it using a "special" value (undef) and using an if as a statement modifier:

        #!/usr/bin/perl -w use warnings; use strict; my $secret; while (1) { $secret = int(1 + rand 100) if ! defined $secret; print "please enter a guess from 1 to 100: "; chomp (my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print "Sorry you gave up. The number was $secret.\n"; last; } if ($guess < $secret) { print "You failed, try higher.\n"; } elsif ($guess > $secret) { print "You failed, try lower.\n"; } elsif ($guess == $secret) { print "You got it!\n"; $secret = undef; } }

        DWIM is Perl's answer to Gödel