Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: how to enable, disable strict

by elsiddik (Scribe)
on Apr 25, 2007 at 10:20 UTC ( #611957=note: print w/replies, xml ) Need Help??

in reply to Re: how to enable, disable strict
in thread how to enable, disable strict

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,

Replies are listed 'Best First'.
Re^3: how to enable, disable strict
by GrandFather (Saint) on Apr 25, 2007 at 10:37 UTC

    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
      its working perfectly now ;) thx dude.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2023-12-04 15:53 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (25 votes). Check out past polls.