Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

The Perldoc Perlfunc Quiz

by abcde (Scribe)
on Jan 02, 2006 at 20:05 UTC ( #520443=CUFP: print w/ replies, xml ) Need Help??

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 SCAL +AR 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 qu +it open( QS, $perlfunc ) or die("Can't open $perlfunc: $!"); while (<QS>) { last if /^=head2 Perl Functions by Category/; } while (<QS>) { last if /^\*/; s/C<sub\*>//g; # These do not work s/C<-I<X>>//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 (<QS>) { last if /^=item $function/; } my $sentence = ""; while (<QS>) { 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 <STDIN>; 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 qu +estion\n"; print "Thanks for playing!\n"; close(QS); exit; }
~abseed
Edited by demerphq, added readmore.

Comment on The Perldoc Perlfunc Quiz
Select or Download Code
Re: The Perldoc Perlfunc Quiz
by chanio (Priest) on Jan 02, 2006 at 22:04 UTC
Re: The Perldoc Perlfunc Quiz
by ysth (Canon) on Jan 02, 2006 at 23:52 UTC
    I'm getting blank hints still (for ord and endpwent, if it helps).

      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)

      ~abseed
Re: The Perldoc Perlfunc Quiz
by Aristotle (Chancellor) on Jan 03, 2006 at 14:00 UTC

    Very cool!

    There’s 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, I’d 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.

Re: The Perldoc Perlfunc Quiz
by theorbtwo (Prior) on Jan 03, 2006 at 18:25 UTC

    Fun! A couple of minor bugs. First off, I get a lot of duplicates. You can fix this by using 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; 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.)


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://520443]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-10-20 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls