A
recent posting by
MrCromeDome showed a quick
CUFP-type script to generate random silliness, presumably to badger cow-orkers. Being in a
refactoring mood, I thought that it might be better if you could describe your "remark" in a text file. This would be used as a template for generating messages, not unlike your typical
"mad-lib". Since it's just a text file, it can be edited by
mere mortals without fear of compilation errors.
In this file, there would be two kinds of replacements: Automatic, where a word is picked from a list of possibilities, and prompted, where the user has to answer a question. Here's an example which incorporates both types using a very basic tagging system:
When I was walking down the [street|boulevard|avenue] a large
[squirrel|frog|lemur|llama] [leaped|jumped|ran|crawled] out and
[bit|mauled|chewed|spit at] me!
You'd never believe what [Name?] said!
When this is run, you'd see something that looks like this, plus or minus input and random factors:
% perl madlib sample.madlib
Name? George Jetson
When I was walking down the avenue a large llama jumped out
and bit me!
You'd never believe what George Jetson said!
If you're probably thinking this is a couple of simple regexes, you're not entirely wrong. However, to spice things up, there's a few "rules" about how the program runs:
- It reads in any template(s) provided on the command line (i.e. @ARGV). All files specified must exist. No error handling required.
- Prompt the user for their input before sending any output. Note that there should be a space after the question mark, even though it's not explicitly in the file.
- Prompts are sent to STDERR, output to STDOUT. This way you get a clean copy of the output with no prompts when you redirect.
- No need for strict or -w compliance. Hack mode is on.
As food for thought, here's my first take at 159 characters, not including linebreaks to suppress the red plus marks:
select STDERR;$|++;select STDOUT;$_=join('',<>);
s#\[([^\]]*\?)\]#print STDERR"$1 ";chomp($i=<STDIN>);$i#ge;
s#\[([^\]]*)\]#@f=split(/\|/,$1);$f[rand@f]#ge;print
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.