I have a short script that implements "Mad Libs." The idea is to read in a file containing noun1, noun2 type tags, and replace the tags with particles supplied by the user at run time. here is the script:
#!/usr/bin/perl -w @story = <>; #filename supplied in ARGV print "give me a noun: "; chomp($noun1 = <STDIN>); print "give me a noun: "; chomp($noun2 = <STDIN>); print "give me a noun: "; chomp($noun3 = <STDIN>); print "give me a verb: "; chomp($verb1 = <STDIN>); print "give me a verb: "; chomp($verb2 = <STDIN>); print "give me a verb: "; chomp($verb3 = <STDIN>); print "give me an adjective: "; chomp($adj1 = <STDIN>); print "give me an adjective: "; chomp($adj2 = <STDIN>); print "give me an adjective: "; chomp($adj3 = <STDIN>); foreach (@story) { $_ =~ s/\[noun1\]/$noun1/g; $_ =~ s/\[noun2\]/$noun2/g; $_ =~ s/\[noun3\]/$noun3/g; $_ =~ s/\[verb1\]/$verb1/g; $_ =~ s/\[verb2\]/$verb2/g; $_ =~ s/\[verb3\]/$verb3/g; $_ =~ s/\[adj1\]/$adj1/g; $_ =~ s/\[adj2\]/$adj2/g; $_ =~ s/\[adj3\]/$adj3/g; print $_; }
As you can see, the script reads in a story from a file, and replaces up to three instances of nouns, verbs, and adjectives. The problem is that the method is grossly inelegant. I know there must be a less brutish solution, so I turn to you for guidance. Thanks

In reply to An Elegance Question by Simplicus

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.