Programming Troubles
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
Rob Pengelly posted this at 18:06 — 4th December 1999.
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.
sumengen posted this at 13:58 — 5th December 1999.
They have: 15 posts
Joined: Jun 2000
Hi,
You mention writing $ref to the file?? The code doesn't seem to do that.
------------------
japhy posted this at 04:10 — 13th December 1999.
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
fairhousing posted this at 05:53 — 13th December 1999.
They have: 1,587 posts
Joined: Mar 1999
here's some very simple code that may do what u want.
Traffic-Website.com free traffic, affiliate programs, hosting, & domain names.
My Site got hacked, but i'm coming back?
japhy posted this at 08:23 — 13th December 1999.
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:
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.