perl cgi

可以簡化一些語法,但還是需要花時間研究一下

#!/usr/bin/perl
use strict;
my $head_title = 'i am a cgi';
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header(-charset => 'utf-8'); # 定義表頭,並給utf8編碼
print start_html(-title=>"$head_title",
	-head  => [
		meta( {-http_equiv  => 'Refresh',      -content => '300'} ),
		meta( {-http_equiv => 'Cache-Control',-content => 'no-cache'} ),
		meta( {-http_equiv => 'Pragma',       -content => 'no-cache'} ),
	],
);
print h1({-align=>'center'},$head_title);
# 假如連結是 http://xxx/cgi-bin/xx.cgi?name=1&v=1
# 把連結作成 hash,得到 name=1 與 v=1
my $query_string = $ENV{'QUERY_STRING'};
my %query_string_hash;
foreach (split/&/,$query_string) {
	my ($key,$val) = split/=/,$_;
	$query_string_hash{$key} = $val;
}
# 得到 xx.cgi
my $script_name = $ENV{'SCRIPT_NAME'};
# 秀出環境變數,如果我帶 http://xxx/cgi-bin/xx.cgi?v=1
if ($query_string_hash{'v'} == 1) {
	foreach my $key (sort(keys(%ENV))) {
		print "$key = $ENV{$key}
\n";
	}
}
# 定義 form
print startform;
print textfield(-name=>'textN',  # 可以讓 param();帶出值的參數
	-default=>"i am a text", # 框框裡可以給它預設內容
	-style=>"width:120px;", # 框框的寬度
	-maxlength=>20,	# 限制可以輸入的字串數
);
my @abcd = ('a','b','c','d');
my %abcd_label = (a=>1,b=>2,c=>3,d=>4);
my @nopq = ('n','o','p','q');
# 下拉選單
print popup_menu(-name=>'groupN',
	-style=>"width:200px;",
	-values=>[
		# 給群組
		optgroup(-name=>'abcd',
				 -values => [@abcd],
				 -labels=>\%abcd_label, # 讓顯示變成不同,但輸出值是原來的
		),
		optgroup(-name=>'nopq',
				  -values=>[@nopq],
				),
	],
	-default=>'b',
);
print submit(-value=>'i am a submit',
			 -style=>"width:100px;",
);
print endform;
# 按 submit 後顯示
my @texts = param('textN');
my @groups = param('groupN');
print br,@texts;
print br,@groups;
# 表格
print table({-border=>0,-align=>'center',-cellpadding=>2,-cellspacing=>1,-bgcolor=>''},
	Tr({-align=>'center',-valign=>'top'},
	[
		td([ h3('wow1')]) . td([ h3('wow2')]) . td([ h3('wow3')]),
	]
	),
	Tr({-align=>'center',-valign=>'top'},
	[
		td(["111"]) . td(["222"]) . td(["333"]),
	]
	),
);
print end_html;	# 結速 html
Related posts 相關文章
2020 年迎來了 PHP 8 與 Perl 7
More...
安裝 perl 5.8.1 版本 – 草記
More...
perl 備註
More...
perl 隨機產生 password 密碼
More...

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。