<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1000872" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1000872</link>
    <description></description>
    <language>en</language>
          <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/programming-troubles#comment-1003711</link>
    <description> &lt;p&gt;The aim of this thread is to discuss how to expand variables in templates; fairhousing, your reply opens the file for READING (and thus, you can&#039;t write $ref to it), and it doesn&#039;t accomplish what was first asked.&lt;/p&gt;
&lt;p&gt;There are several Perl modules available that allow for text templates to have variables expanded in them.  The method I usually use is this:  in my text file, I&#039;ll have something like:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;code:&lt;/p&gt;
&lt;pre&gt;Hello, %%name%%.  Today is %%date%%.
You&#039;ve accessed this quiz %%num%% times.[/code]

Then, in my Perl program, I&#039;ll have the following code:

&lt;BLOCKQUOTE&gt;code:&lt;pre&gt;#!/usr/bin/perl -w

use CGI;
use CGI::Carp qw( fatalsToBrowser );
use strict;

my $q = new CGI;

open TPL, &quot;templates/welcome.tpl&quot; or
  die &quot;can&#039;t open templates/welcome.tpl: $!&quot;;
while (&amp;lt;TPL&amp;gt; ) {
  s/%%([^%]+)%%/$q-&amp;gt;param($1)/eg;
  print;
}
close TPL;[/code]

Let me explain the finer points of this example.  By placing the sections of the template I want replaced between %%&#039;s, I can separate them and be sure I&#039;m replacing ONLY the text I want.  The regular expression of s/%%([^%]+)%%/$q-&amp;gt;param($1)/eg does quite a bit of work.  The left hand side means &quot;match two %&#039;s, then any number of NON-% characters, and then two %&#039;s; store the non-% characters in $1.&quot;  The right hand side gets the value of this template key, &quot;name&quot;, &quot;date&quot;, or &quot;num&quot;, for instance, and gets its value from the CGI query.

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, USNR &lt;/pre&gt;&lt;/blockquote&gt;&lt;/pre&gt;&lt;/blockquote&gt;</description>
     <pubDate>Mon, 13 Dec 1999 08:23:00 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1003711 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/programming-troubles#comment-1003710</link>
    <description> &lt;p&gt;here&#039;s some very simple code that may do what u want.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;code:&lt;/p&gt;
&lt;pre&gt;
#!/usr/bin/perl 
$ref=$ENV{&#039;QUERY_STRING&#039;}; 
open (FILE,&quot;colo.txt&quot;);
print FILE &quot;$ref&quot;;
close(FILE);
exit;
[/code]



------------------
&lt;strong&gt;Get paid to surf, email, and everything else online! 75 hrs a month at 55+ cents an hour! Beats AllAdvantage Easily. &lt;a href=&quot;http://www.getpaid4.com?fair&quot; class=&quot;bb-url&quot;&gt;CLICK 4 DETAILS&lt;/a&gt;
&lt;/strong&gt; &lt;/pre&gt;&lt;/blockquote&gt;</description>
     <pubDate>Mon, 13 Dec 1999 05:53:00 +0000</pubDate>
 <dc:creator>fairhousing</dc:creator>
 <guid isPermaLink="false">comment 1003710 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/programming-troubles#comment-1003709</link>
    <description> &lt;p&gt;If I understand you correctly, you have a text file containing something like:&lt;/p&gt;
&lt;p&gt;  &amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;body&amp;gt;&lt;br /&gt;
  Query string is $ref&lt;br /&gt;
  &amp;lt;/body&amp;gt;&lt;br /&gt;
  &amp;lt;/html&amp;gt;&lt;/p&gt;
&lt;p&gt;Your program sets the $ref variable, and when you print the contents of the file, you want the &#039;$ref&#039; in the file to be interpolated as the $ref variable&#039;s value.&lt;/p&gt;
&lt;p&gt;It would be quite efficient like so:&lt;/p&gt;
&lt;p&gt;  #!/usr/bin/perl -w&lt;/p&gt;
&lt;p&gt;  use CGI::Carp qw( fatalsToBrowser );&lt;br /&gt;
  use strict;&lt;br /&gt;
  my $ref = $ENV{QUERY_STRING};&lt;/p&gt;
&lt;p&gt;  print &quot;Content-type: text/html\n\n&quot;;&lt;/p&gt;
&lt;p&gt;  open FILE, &quot;filename.txt&quot; or&lt;br /&gt;
    die &quot;can&#039;t open filename.txt: $!&quot;;&lt;br /&gt;
  while (&amp;lt;FILE&amp;gt; ) {&lt;br /&gt;
    s/\$ref/$ref/g;&lt;br /&gt;
    print;&lt;br /&gt;
  }&lt;br /&gt;
  close FILE;&lt;/p&gt;
&lt;p&gt;CGI::Carp::fatalsToBrowser sends error messages to the browser, like the die() message, if the file can&#039;t be opened.&lt;/p&gt;
&lt;p&gt;The regular expression, s/\$ref/$ref/g, changes every occurrence of &#039;$ref&#039; to the value of the $ref variable for the current line of text (that what the /g does -- makes the substitution global).&lt;/p&gt;
&lt;p&gt;print without any arguments prints the $_ variable, which is set to each line of FILE from the while-loop.&lt;/p&gt;
&lt;p&gt;For more info, look at the following documentation:  perlre (for regular expressions), perlfaq4 (for expanding variables in text strings).  That information can also be accessed in the following ways:&lt;/p&gt;
&lt;p&gt;  perldoc perlre&lt;/p&gt;
&lt;p&gt;  perldoc -q text strings&lt;br /&gt;
  # or&lt;br /&gt;
  perldoc perlfaq4&lt;/p&gt;
&lt;p&gt;------------------&lt;br /&gt;
--&lt;br /&gt;
MIDN 4/C PINYAN, NROTCURPI, USNR&lt;/p&gt;
 </description>
     <pubDate>Mon, 13 Dec 1999 04:10:00 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1003709 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/programming-troubles#comment-1003708</link>
    <description> &lt;p&gt;Hi,&lt;br /&gt;
You mention writing $ref to the file?? The code doesn&#039;t seem to do that.&lt;/p&gt;
&lt;p&gt;------------------&lt;/p&gt;
 </description>
     <pubDate>Sun, 05 Dec 1999 13:58:00 +0000</pubDate>
 <dc:creator>sumengen</dc:creator>
 <guid isPermaLink="false">comment 1003708 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/programming-troubles#comment-1003707</link>
    <description> &lt;p&gt;What does each line of the text file look like?&lt;/p&gt;
&lt;p&gt;------------------&lt;br /&gt;
&lt;strong&gt;The longest recorded flight of a chicken is 13 seconds.&lt;/strong&gt;&lt;/p&gt;
 </description>
     <pubDate>Sat, 04 Dec 1999 18:06:00 +0000</pubDate>
 <dc:creator>Rob Pengelly</dc:creator>
 <guid isPermaLink="false">comment 1003707 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
