CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Problem merging template



Jan 25, 2001, 12:54 AM

Post #1 of 1 (268 views)
Problem merging template Can't Post

I'm having some trouble merging an HTML template using a recipe from the PERL Cookbook. The template page contains %%message%% at the point I want the changeout to occur. The problem is in the subroutine. I've noted it with a bold comment.

Main Program Section:

# send a welcome message to new list member if welcome checkbox was checked 
if (defined($$Input{"welcome"})) {
$msg = "You have been added to the Mailing-List...blah blah";
if ($$Input{"HTML"}) { # They've selected the HTML mailtype radio button
$fields{"message"} = $msg;
$MergeResult = &MergeTemplate("MailoutTemplate.html", \%fields);
($MergeResult)?($msg = $MergeResult):($$Input{"HTML"} = 0);
# Merge failed if Merge result is zero, so send a plain-text message just so they get something
my $MailResult = &SendHTMLEmail($$Input{"email"}, "Mailing-List Results", $$Input{"HTML"}, $msg);
if (!MailResult) {
&LogFailure("The failure logged above occured when the main program in attempted
to send a confirmation e-mail to".$$Input{"email"}); # wordwrapped for readability on this forum :)

and here's the subroutine:

# PURPOSE merges a template file replacing comment tags in source html with data provided
# EXPECTS Two strings 1) the name of the template file and 2) a pointer to a hash containing
# identifiers as keys and string to be swapped as values
# RETURNS The merged file as a single string, or
# 0 if it failed
sub MergeTemplate {
my ($TemplateFile, $Changes) = @_; my $text;
chomp($TemplateFile); chomp($Changes);
local $/; # slurp mode (undef)
local *TEMPLATE; # create local filehandle
if(!open(TEMPLATE, "<../templates/$TemplateFile")) {
&LogFailure("Unable to open the template file named $TemplateFile.");
return 0;
$text = <TEMPLATE>; # read whole file
close(TEMPLATE); # ignore retval
# replace quoted words with value in %$Changes hash
$text =~ s{ %% ( .*? ) %% } # This is where the problem is
{ exists( $Changes->{$1} ) # what is $1?
? $Changes->{$1}
: ""
return $text;

The e-mail message that is sent has the match identifier (%%message%% ) removed, so it is being identified correctly, but it's not replaced with "message". Any ideas?

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives