Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

More Mad Libs

by btrott (Parson)
on Apr 19, 2000 at 02:56 UTC ( #7980=snippet: print w/replies, xml ) Need Help??
Description: This is yet another rewrite of the mad libs script much discussed last week: An Elegance Question. DISCLAIMER: the point of this is not to give you code that you should use. All the same, though, it's quite fun, and if you try to figure out what's going on, it's pretty educational.

Here's a sample story file:

The [noun] [past_tense_verb] the [noun].
You don't have to predeclare anything; the code will prompt you for the parts of speech that it needs, automatically. One bad thing is that said parts of speech must be valid Perl identifiers.

But then again, this *is* just for fun. :)

#!/usr/local/bin/perl -w
use strict;

die "usage: $0 <madlib>" unless @ARGV;
my @story = <>;

# Set up the Text::Template normally.
use Text::Template;
my $t = new Text::Template(TYPE => 'ARRAY', SOURCE => [ @story ],
    DELIMITERS => ['[', ']']);

# compile the template; usually you don't need to
# call this, cause it gets called automatically.
# but we need to do some magic on its results.
$t->compile;

# This is magical. :)
# Mark-Jason Dominus probably wouldn't like me
# messing with the insides of his module. This
# extracts all of the template variables and
# creates closures for each, then sticks
# all of that into a hash.
my %hash = map { $_->[1], mk_subref($_->[1]) }
           grep $_->[0] eq "PROG",
           @{$t->{SOURCE}};

# Now we fill in the story, and we're done:
# we hand Text::Template the hash we've
# constructed. Each time it finds one of the
# template variables (in the template itself),
# it (magically) calls the closure we associated
# with it, which prompts the user and returns
# the word he/she entered; which is then filled
# into the template.
my $story = $t->fill_in(HASH => \%hash);
print $story;

# Returns a closure that prompts the user for a part
# of speech, then returns the word he/she enters
sub mk_subref {
    my $part = shift;
    return sub {
        print "Give me a $part: ";
        my $word = <STDIN>;
        chomp $word;
        $word;
    };
}
Replies are listed 'Best First'.
RE: More Mad Libs
by turnstep (Parson) on Apr 19, 2000 at 03:00 UTC
    Ah, but the original poster required the questions to be asked in the form of all nouns, then all verbs, then all adjectives. Another twist...
      I never realized what I was starting here. Thanks for the consideration.
      S
      I know. But that makes you explicitly spell out which terms you want to use in your mad lib, which isn't very efficient. My snippet rewrite is written in the spirit of plaid's code (here), which scans through the text and prompts the user for terms.
        No, you don't have to spell it out in advance, you just have to have gather them all in, then sort them by type. See my version for one way to do it.
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://7980]
help
Chatterbox?
[Corion]: ambrus: Well, for mathematical notation, I find blackboard much more convenient than a computer. But when inserting text or moving text around, the computer wins obviously
[ambrus]: But either of these is a big problem in practice, so I'd need to spend like thirty years of my life to solve (a) and five more years to solve (b)
[ambrus]: Corion: yes, CURRENTLY the blackboard is more convenient
[ambrus]: and it's not like I want to ban blackboards anyway
[ambrus]: you already have blackboards and a canvas for projector or overhead transparencies (or positive film slide projector, not used for maths) canvas in the same lecture halls today, and switch in a few minutes between presentations,
[ambrus]: they're only difficult to use together.
[ambrus]: overhead transparencies are a nice convenience by the way that mix the two slides, because you can write them in advance and edit them during the presentation easily. but they're not very much in fasion these days.
[ambrus]: you can even print them.
[Discipulus]: ambrus i'm trying out MremoteNG which wrap putty and rdp and many other things..
[Corion]: ambrus: Yes, ideally you would have the ease of overhead projection transparencies and pens drawing on them, combined with the computer generated slide text...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2017-09-26 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?