
mhx
Enthusiast
Jul 31, 2001, 1:14 AM
Post #2 of 6
(1110 views)
|
|
Re: HELP: seperate HTML and perl
[In reply to]
|
Can't Post
|
|
Use a pattern for all the values you want to substitute that can be easily distinguished from being HTML. This is not for Perl, mainly for the one who has to edit the file. Let's assume you have the following 'extended' HTML file:
<html> <head><title>Welcome, [=USER=]</title><head> <body><p> Hello [=USER=],<BR> Your last logon was [=LOGON=]<BR> </p></body> </html> The [=...=] pattern was arbitrarily chosen, if there's any reason you don't like it, take whatever you want. If this file was called test.html, you could use the following script for displaying the HTML with substitutions:
#!/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use strict; my $file = 'test.html'; open FILE, $file or die "cannot open $file: $!\n"; my $html = do { local $/=undef; <FILE> }; close FILE; my %values = ( USER => 'Marcus', LOGON => scalar localtime time, ); $html =~ s/\[=(\w+)=\]/$values{$1}/g; print header, $html; As you can see, I've used a hash table to store the values for USER and LOGON. This way, the whole substution fits into a short line:
$html =~ s/\[=(\w+)=\]/$values{$1}/g; There may be better solutions than this, but I don't do a lot of CGI scripting. So if anyone could come up with a better idea, please do. Hope this helps. -- Marcus
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= ($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"
|