It is good practice to always include use strict; use warnings; at the start of your Perl code. They catch many types of coding errors early which make the errors easier to fix.
White space and indentation help make code easier to read by breaking it up into functional units - much like using spaces, sentences and paragraphs in prose.
Disallowing the user an easy way of exiting a program such as this is not very friendly.
substr(rand(1)*10,0,1) is better done int rand 10.
You chop the input for the first value, but not subsequent values. chomp is generally better used than chop in any case.
The block of code:
print "Guess the Code:";
$st=time();
$ip=<>;
chop($ip);
while($seed != $ip)
{
$seed=substr(rand(1)*10,0,1);
$seed++;
$ip=<>;
}
can be restructured so that the test is made last in the loop allowing the prompt and input handling code to only be written once (see below).
The if following the loop is redundant - you don't get there until the test has succeeded in the loop.
Combinging those suggestions, adding a little white space and indentation, and renaming a couple of variables to make them consistent with usage, you get something like:
use strict;
use warnings;
my $st = time ();
{
my $code = 1 + int rand 10;
print "Guess the Code:";
my $guess = <>;
chomp $guess;
redo if $code != $guess;
}
my $et = time ();
my $tt = $et - $st;
print "\n Time taken to break the Code ..: $tt seconds\n";
DWIM is Perl's answer to Gödel
|