Remember the good old times when we were trying to see who could write the shortest “guess-a-number-from-1-to-100″ game on a Casio graphics calculator?
I took the night off to read the official Perl introduction and came up with a Perl version of the game. I know it’s very icky, but I did it in 10 minutes and it’s my first time using Perl, so you can’t really expect that much:
#!/usr/bin/perl
my $ans = int(rand(100))+1;
for (my $c = 0; $c < 6; $c++) {
my $guess = <STDIN>;
die "invalid input" if ($guess !~ /^[0-9]+$/);
print "bigger\n" if ($guess < $ans);
print "smaller\n" if ($guess > $ans);
win() if ($guess == $ans);
}
print "lose\n\n";
exit 0;
sub win {
print "win\n\n";
exit 0;
}
The validation for input could probably be better if it was
/^[1-9][0-9]*$/
but in the end it’s only a minor thing.
Perl’s not as nightmarish as I thought it’d be. Sure, the sigils are a bit of a nuisance but once you get over that it’s not terribly bad (and you’re probably used to shift+4 from PHP anyway
).