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