Programming Troubles

They have: 2 posts

Joined: Dec 1999

Hi,
Well I hear this is a good place to go with programming troubles. I am having a problem with one script. The script is being used to track a referring urls for orders and it works great except now the guy I made it for wants templates so he can easily edit. Here is my base for the new files.

#!/usr/bin/perl
$ref=$ENV{'QUERY_STRING'};
open (FILE,"colo.txt");
@form=<FILE>;
close(FILE);
print "Content-type: text/html\n\n";
print<<EOF;
@form
EOF
exit;

That is the page code and it does work but when it opens the text file it prints
$ref (I actually put the $ref in the text files)
not the actual referring url.
Any ideas?
Thanks

----------
Clueless in cyberspace Smiling

They have: 850 posts

Joined: Jul 1999

What does each line of the text file look like?

------------------
The longest recorded flight of a chicken is 13 seconds.

They have: 15 posts

Joined: Jun 2000

Hi,
You mention writing $ref to the file?? The code doesn't seem to do that.

------------------

They have: 161 posts

Joined: Dec 1999

If I understand you correctly, you have a text file containing something like:

<html>
<body>
Query string is $ref
</body>
</html>

Your program sets the $ref variable, and when you print the contents of the file, you want the '$ref' in the file to be interpolated as the $ref variable's value.

It would be quite efficient like so:

#!/usr/bin/perl -w

use CGI::Carp qw( fatalsToBrowser );
use strict;
my $ref = $ENV{QUERY_STRING};

print "Content-type: text/html\n\n";

open FILE, "filename.txt" or
die "can't open filename.txt: $!";
while (<FILE> ) {
s/\$ref/$ref/g;
print;
}
close FILE;

CGI::Carp::fatalsToBrowser sends error messages to the browser, like the die() message, if the file can't be opened.

The regular expression, s/\$ref/$ref/g, changes every occurrence of '$ref' to the value of the $ref variable for the current line of text (that what the /g does -- makes the substitution global).

print without any arguments prints the $_ variable, which is set to each line of FILE from the while-loop.

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:

perldoc perlre

perldoc -q text strings
# or
perldoc perlfaq4

------------------
--
MIDN 4/C PINYAN, NROTCURPI, USNR

They have: 1,587 posts

Joined: Mar 1999

here's some very simple code that may do what u want.

code:

#!/usr/bin/perl 
$ref=$ENV{'QUERY_STRING'}; 
open (FILE,"colo.txt");
print FILE "$ref";
close(FILE);
exit;
[/code]



------------------
Get paid to surf, email, and everything else online! 75 hrs a month at 55+ cents an hour! Beats AllAdvantage Easily. CLICK 4 DETAILS
 

Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?

They have: 161 posts

Joined: Dec 1999

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't write $ref to it), and it doesn't accomplish what was first asked.

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'll have something like:

code:

Hello, %%name%%.  Today is %%date%%.
You've accessed this quiz %%num%% times.[/code]

Then, in my Perl program, I'll have the following code:

code:
#!/usr/bin/perl -w

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

my $q = new CGI;

open TPL, "templates/welcome.tpl" or
  die "can't open templates/welcome.tpl: $!";
while (<TPL> ) {
  s/%%([^%]+)%%/$q->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 %%'s, I can separate them and be sure I'm replacing ONLY the text I want.  The regular expression of s/%%([^%]+)%%/$q->param($1)/eg does quite a bit of work.  The left hand side means "match two %'s, then any number of NON-% characters, and then two %'s; store the non-% characters in $1."  The right hand side gets the value of this template key, "name", "date", or "num", for instance, and gets its value from the CGI query.

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, USNR 

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.