emilford has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm a bit confused why this isn't working the way it should. I'm probably just tired and making a careless mistake, but I just don't see it. Suppose the following:
# the following message will be sent to the specified recipient in ord +er # to promote the site. the following markers may be used anywhere in # the message # # <RECP_NAME> - will be replaced with the recipient's name # <SEND_NAME> - will be replaced with the sender's name # <SITE_URL> - will be replaced with the site url variable from above # my $message2 = ' Hey <RECP_NAME>, I was surfing the web and I stopped by a pretty cool site that I think + you might be interested in. It is filled with cool information, good link +s, funny pics, and more, so be sure to check it out! Here is the address of the site: <SITE_URL>. <SEND_NAME> <SEND_EMAIL> ';
Now if I had the following lines of code, shouldn't the appropriate replacements be made?
$message1 =~ s/<RECP_NAME>/$$formdata{'recp_name'}/ig; $message1 =~ s/<SEND_NAME>/$$formdata{'send_name'}/ig; $message1 =~ s/<SITE_URL>/$site_url/ig;
Everything gets replaced by the <SITE_URL>; that gets left for some reason. I do something similar to another string and half of the cases get replaced, while half of them don't. Does anyone see what I'm doing wrong?

Replies are listed 'Best First'.
•Re: string substitution help
by merlyn (Sage) on Sep 23, 2002 at 02:54 UTC
    Instead of inventing Yet Another Templating Solution, you might also consider Template Toolkit. Here's something I whipped up in 5 minutes from your source. Note the alternative provided for sender email:
    #!/usr/bin/perl -w use strict; $|++; use Template; my $result = Template->new ( START_TAG => '<', END_TAG => '>', )->process(\*DATA, { recp_name => 'Recipient name', send_name => 'Sender name', ## send_email => 'sender@example.com', site_url => 'http://example.com/some/place', }) || die Template->error; __END__ Hey <recp_name>, I was surfing the web and I stopped by a pretty cool site that I think + you might be interested in. It is filled with cool information, good link +s, funny pics, and more, so be sure to check it out! Here is the address of the site: <site_url>. <send_name> <IF send_email; send_email; ELSE; "[no email specified]"; END>

    -- Randal L. Schwartz, Perl hacker

      Hey Merlyn

      Thanks for the reply; I figured out what my problem was.

      If there's one thing I've learned from frequenting PM.org, it's to always use strict and -w. The snippet of code I posted was just that...a snippet. The rest of the code does contain use strict and -w.

      Either way, I found my careless error. Thanks for the post.
•Re: string substitution help
by merlyn (Sage) on Sep 23, 2002 at 02:43 UTC