The Perldoc Perlfunc Quiz test your knowledge of the builtin functions. It reads the perlfunc file, gets a random function, and offers you sentences from its documentation as hints until you can guess at it.
It's not meant to be serious, but if you're learning things from it, great!
To play: The program gives you a hint, and you must try to answer. If this hint is not enough - it often isn't - answer "?" or "hint" for another one. If you're fed up, "q" or "quit" will reveal your score. Any other response will be taken as your answer. Good luck!
Example session:
The Perldoc Perlfunc Quiz! Answer '?' for a hint, 'q' to quit. Anything else for an answer. Hint: This call is actually implemented in terms of foofrom(2) system call. ? Hint: Attempts to receive LENGTH characters of data into variable SCALAR from the specified SOCKET filehandle. recv Yes, well done! Hint: Has no effect if the variable is not tied. untie Yes, well done! Hint: If EXPR is omitted, uses $_ . ? Hint: If EXPR is omitted, uses $_ . ? Hint: Returns the value of EXPR with the first character in uppercase (titlecase in Unicode). uc No! It was `ucfirst'. q 2 answered, 3 correct = 66.6666666% correct 3 extra hints used = 1 per question Thanks for playing!
#!/usr/bin/env perl
# The Great Perldoc Perlfunc Quiz!
# by abcde.
use strict;
my $perlfunc = `perldoc -l perlfunc`; # Where is perlfunc?
my @functions = ();
my @sentences = ();
my $answered = 0;
my $correct = 0;
my $hints = 0;
$SIG{INT} = \&finale; # Show the scores when we quit
open( QS, $perlfunc ) or die("Can't open $perlfunc: $!");
while () {
last if /^=head2 Perl Functions by Category/;
}
while () {
last if /^\*/;
s/C//g; # These do not work
s/C<-I>//g; # Because of POD formatting
while (1) {
if (/C<(.*?)>/) {
push @functions, $1;
s/C<$1>//;
}
else { last; }
}
}
srand(time);
# Make the game slightly easier
@functions = grep { !/^[sg]et/ } @functions;
@functions = grep { !/^end/ } @functions;
# Now we have a list of all the functions
# So play the game!
print "The Perldoc Perlfunc Quiz!\n";
print "Answer '?' for a hint, 'q' to quit.\n";
print "Anything else for an answer.\n\n";
do {
# Pick a random function
my $function = $functions[ int rand scalar @functions ];
# Find some sentences
seek QS, 0, 0;
while () { last if /^=item $function/; }
my $sentence = "";
while () {
next if (/^\s/);
if (/^=item (.*?)\b/) {
last unless (/^=item $function/);
}
else {
chomp;
$sentence .= "$_ ";# if $_ =~ /$function/; ?
}
}
$sentence =~ s/\b$function/\033[36mfoo\033[0m/ig; # Disguisery!
$sentence =~ s/.<(.*?)>/$1/g; # Strip all POD formatting
@sentences = split /\.\b/, $sentence;
@sentences = grep ( length $_ > 3, @sentences ); # Remove silly hints
@sentences = grep ( /[a-zA-Z]/, @sentences );
while (1) {
my $hint = $sentences[ int rand scalar @sentences ];
$hint .= ")" if $hint =~ /^\(/;
print("Hint: $hint\n");
my $in = lc ;
chomp $in;
if ( $in eq "hint" or $in eq "?" ) {
$hints++;
next;
}
if ( $in eq "quit" or $in eq "q" ) {
finale();
}
if ( $in eq $function ) {
print "Yes, well done!\n\n";
$answered++;
$correct++;
last;
}
else {
print "No! It was `$function'!\n\n";
$answered++;
last;
}
}
} while (1);
sub finale {
if ( $answered == 0 ) {
print "0 answered, 0 correct = 0%\n";
print "I hope for your sake you nuked that level\n";
exit;
}
print "$answered answered, $correct correct = "
. $correct / $answered * 100
. "% correct\n";
print "$hints extra hints used = " . $hints / $answered . " per question\n";
print "Thanks for playing!\n";
close(QS);
exit;
}
& very instructive!
If you look at the perlfunc source, there's a list of functions getpwnam() to endservent() that just have a blank line for documentation, and are mentioned in a paragraph later - good for humans, bad for code. /^set/ and /^get/ are being removed because of this, but I forgot about /^end/, thanks for pointing it out.
It's a KludgeItTillItWorks fix, but blank hints should be all gone now (no, really)
Very cool!
Theres a lot of room for improvement, though.
First of all, your code is largely flat. Granted, few things get used more than once, but breaking it up into a few well-named functions could make it much easier to read, regardless.
Also, rather than grope around in the POD anew for every question, Id read all the data into a hash at the start of the script. That would make the different parts of your code much more focussed; possibly to the point of obviating the need to break it up.
Makeshifts last the longest.
Fun! A couple of minor bugs. First off, I get a lot of duplicates. You can fix this by using [cpan://List::Util]::shuffle. (In which case, you should srand; at least some versions don't do that for you.) That will make you get each thing once, if you use it right. Of course, that means that it gets less random as you go, but more interesting.
Also, the re on line 70 should have a /i modifier; [doc://close] in purticular uses a form of close at the beginning of a sentance often.
Oh, and time for me to show off:
16 answered, 15 correct = 93.75% correct 0 extra hints used = 0 per question Thanks for playing!
(But it was just a run of good luck.)
perlmonks.org content © perlmonks.org and abcde, Aristotle, chanio, theorbtwo, ysth
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03