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; }; }

In reply to More Mad Libs by btrott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.