A short program that asks users for parts of speech and substitutes values.
%word_list = (); @story = split(/\n/, <<End_Story); Once upon a time there was a [adj1] [noun1] named Bob, who had a [adj2] [noun2]. He used it to [verb1] his [adj3] [noun3]. End_Story foreach my $part_of_speech ( 'noun', 'verb', 'adj' ) { for (my $word_idx = 1; $word_idx <= 3; $word_idx++) { $word_list{$part_of_speech}{$word_idx} = give_me_a($part_of_sp +eech); } } foreach my $line (@story) { $line =~ s/\[(noun|verb|adj)([1-3])\]/$word_list{$1}{$2}/g; print STDOUT $line, "\n"; } sub give_me_a { my ($part_of_speech) = @_; print STDOUT "Give me a $part_of_speech: "; my $word = <STDIN>; chomp($word); $word; }

In reply to Mad Libs Rewrite by stephen

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.