#!/usr/bin/perl -w # simple-template # # a simple way of filling in a template file (e.g. the master letter # for a mail merge) with various values. # # 1999-04-18 -- tkil@scrye.com # use strict; # here's some sample data. in a real application, you could pull this # data from a CGI form or from a database. my @info = ( { name => 'Tkil', email => 'tkil@scrye.com', bday => 'February 23rd', fee => '$100.00' }, { name => 'Bob', email => 'nobody@nowhere.com', bday => 'January 1st', fee => '$50.00' } ); # i'll just get the data in from the end of this program. in a real # templatizing program, you'd probably want to read it in from an # external file. don't forget to check return values! my $master_text = do { local $/; }; # we'll do one round of output for each batch of info. for my $info_ref (@info) { # copy the master text my $text = $master_text; # do the actual substitution $text =~ s/::(\w+)::/ exists $info_ref->{lc $1} ? $info_ref->{lc $1} : "UNKNOWN"/egx; # try to do a function substitution. DANGER DANGER DANGER this is a # really dangerous way to do this, but hey, it's not my box: $text =~ s/&<(.*?)>&/$1/ee; # print it out print "-" x 70, "\n", $text; } print "-" x 70, "\n"; exit; __END__ Dear ::NAME:: (&&): It's nice to know that your birthday is ::BDAY:: and that your e-mail address is ::EMAIL::. You'll be receiving our bill for ::FEE:: shortly. Thank you.