Perl Language(VII) Web Programming
Perl Language(VII) Web Programming16. Perl and Web
CGI (Common Gateway Interface) -----> FastCGI ------> Apache mod_perl
16.1 CGI
use CGI;
my $q = CGI->new;
$h = $q->header(-charset => 'utf-8',
-type => 'text/html');
print $h . "\n";
my $user = $q->param('user');
print $user
console output:
Content-Type: text/html; charset=utf-8
my @params = $q->params();
$q->redirect('http://url.you.want/');
print $q->textfield( -name=>'field',
-default=>'default value');
error message:
if ($q->cgi_error) {
print "無法處理CGI程式";
print $q->cgi_error;
}
use CGI;
my $q = CGI->new;
my $cookie = $q->cookie('cookie_name');
$cookie = $q->cookie( -name=>'cookie_name',
-value=>'value',
-expires=>'+1h',
-domain=>'.my.domain',
-secure=>1);
print $q->header( -cookie=>$cookie);
16.2 Template
first of all, we need to install tempalte module in cpan
cpan>install Template
use Template;
my $config = {
INCLUDE_PATH => '/template/path',
EVAL_PERL => 1,
};
my $template = Template->new($config);
my $replace = "var put in template";
my $vars = {
var=> $replace,
};
my $temp_file = 'template.html';
my $output;
$template->process($temp_file, $vars, $output)
|| die $template->error();
But there is one error in apache error.log file:
malformed header from script. Bad header=<html>: Action.pl
I fix this problem:
print "Content-Type: text/html\n\n", $output;
$template->process($temp_file, $vars, $output)
|| die $template->error();
foreach
[% FOREACH grade = grades %]
Price:[% grade %]
[% END %]
Perl code
[% PERL %]
# perlcode
....
[% END %]
[% INCLUDE copyright.html %]
copyright.html file content is:
<p>here is footer by sillycat</p>
[% IF condition %]
....
[% END %]
or
[% IF condition %]
....
[% ELSE %]
....
[% END %]
[% IF condition %]
....
[% ELSIF condition2 %]
....
[% ELSE %]
....
[% END %]
[% UNLESS condition %]
....
[% END %]
16.3 Mason
references:
http://easun.org/perl/perl-toc/
http://perl.apache.org/
http://www.4webhelp.net/tutorials/cgi/env_vars.php
http://www.cgi101.com/class/ch3/text.html
页:
[1]