Monday, October 24, 2005

brian d foy: Dear [fname],:

Here's a little program to generate those responses to marketing messages. I'm not really complaining (I am on the press list which kinda means I asked for it), but I think they could try a little harder (yes, I'm looking at you Black Duck. How about some text with those unloaded images?).
#!/usr/bin/perl

sub prompt
{
print "$_[0] >>> ";
chomp( my $response = <STDIN> );
$response;
}

%mad_libs = (
greeting => "What's a word you use to say hello?",
shill => "What do you call a marketing guy in a suit?",
verb => "What does Nat do with a sheep?",
animal => "What's your favorite animal?",
orifice => "Name a hole in your body.",
false => "How do you say goodbye?",
name => "What's your name?",
address => "Where do you live?",
);

foreach my $key ( keys %mad_libs )
{
$input->{$key} = prompt( $mad_libs{$key} );
}

######
use Template;

my $tt = Template->new();

my $template = do { local $/; <DATA> };

$tt->process( \$template, $input ) || die $tt->error;

__DATA__
[% greeting %] [% shill %],

Go [% verb %] a [% animal %] in the [% orifice %].

[% false %],

[% name %]

P.S. Please send review copies to [% address %]
A sample run:
How do you say goodbye? >>> Ta Ta
What do you call a marketing guy in a suit? >>> Flak
What's a word you use to say hello? >>> Ahoy
What's your name? >>> brian d foy
Name a hole in your body. >>> nostril
What does Nat do with a sheep? >>> caress
What's your favorite animal? >>> weasel
Where do you live? >>> Chicago
And the output:
Ahoy Flak,

Go caress a weasel in the nostril.

Ta Ta,

brian d foy

P.S. Please send review copies to Chicago
To any marketeers out there: feel free to use this code all you like. It's in the public domain. The support contract, however, is a different matter.

(Via Planet Perl.)

No comments: