use Template; { package MyStash; use base qw(Template::Stash); sub undefined { my $self = shift; my @ident = shift; print "(".join("|",@ident).")\n"; if (@ident > 1) { return ''; } elsif ($ident[0] eq 'adjective') { return 'red'; } elsif ($ident[0] eq 'noun') { return 'fox'; } elsif ($ident[0] eq 'verb') { return 'jumped'; } else { return ''; } } } my $stash = MyStash->new; my $string = "The [% adjective %] [% noun %] [% verb %] over the river.\n"; Template->new(STASH => $stash)->process(\$string);