# Code snippet for Web Programming at 2013/10/16,
# slide at http://zoro.ee.ncku.edu.tw/swd2013/res/05-cgi.pdf
################################################################################
# index.html
################################################################################
# do
#!/usr/bin/perl -w
use CGI;
my $cgi = new CGI;
my $nick = $cgi->param('nick');
my $color = $cgi->param('color');
print "Content-type: text/html\n\n"; # HTTP header
print "Hello World!
"; # any valid HTML
print "$nick likes $color!\n"; # any valid HTML
################################################################################
# tmpl.cgi
#!/usr/bin/perl -w
use CGI;
my $cgi = new CGI;
my $nick = $cgi->param('nick');
my $color = $cgi->param('color');
my $name;
open FH, 'res/member' or die; # open a file
while () { # each line
chomp; # truncate the last "\n" character
my ( $_name, $_nick ) = split "\t"; # split by tab
$_nick eq $nick and $name = $_name and last; # compare
}
close FH; # close the file
$_ = `/bin/cat _hello.html`; # use Linux command
s/{name}/$name/g; # replace
s/{color}/$color/g; # replace
print "Content-type: text/html\n\n$_";
# vi:nowrap:sw=4:ts=4
################################################################################
# _hello.html
Hello World!
{name} likes {color}!