CGI cont n Perl v man perl v

  • Slides: 71
Download presentation

CGI 及其環境簡介(cont) n Perl的相關資料要上哪兒找? v man perl v perldoc perl v http: //www. perl.

CGI 及其環境簡介(cont) n Perl的相關資料要上哪兒找? v man perl v perldoc perl v http: //www. perl. com/perl/info/documentation. html 2022/1/21 國立中央大學電算中心 陳慶彥 9

Perl程式設計-執行環境 n server 通常是設定成去執行放在 ``cgi-bin‘’ 目錄底下的 CGI 程式。不過, server 管理者同時也可以在設定檔 中設定 aliases,讓所有含某些副檔名(如. cgi、. pl)的

Perl程式設計-執行環境 n server 通常是設定成去執行放在 ``cgi-bin‘’ 目錄底下的 CGI 程式。不過, server 管理者同時也可以在設定檔 中設定 aliases,讓所有含某些副檔名(如. cgi、. pl)的 scripts 都能執行 <Directory /home/httpd/cgi-bin> Allow. Override None Options Exec. CGI </Directory> <Directory /home/*/public_html> Options Indexes Includes Exec. CGI Allow. Override None order allow, deny </Directory> n 什麼是檔案使用權限?怎樣改? 2022/1/21 國立中央大學電算中心 陳慶彥 10

Perl程式設計-Lab 1. 您的第一個程式 選擇編輯器 vi , joe , ee , pe 2或是在client端將編輯好 了的檔案上傳到server n

Perl程式設計-Lab 1. 您的第一個程式 選擇編輯器 vi , joe , ee , pe 2或是在client端將編輯好 了的檔案上傳到server n 您的第一個程式hello. cgi #! /usr/bin/perl print “hello world! n”; n Chmod 755 hello. cgi n 執行hello. cgi n 2022/1/21 國立中央大學電算中心 陳慶彥 11

Perl程式設計-資料型態(cont)-Lab 2. Stg 1. cgi #! /usr/bin/perl $name = "Eric C. Herrmann"; print "The

Perl程式設計-資料型態(cont)-Lab 2. Stg 1. cgi #! /usr/bin/perl $name = "Eric C. Herrmann"; print "The owner of this program is $namen"; $first. Name = "Eric "; $middle. Initial = "C. "; $last. Name = "Herrmann"; print "The owner of this program is $first. Name $middle. Initial"; print "$last. Namen"; $f. Name = "Eric"; $m. Initial = "C. "; $l. Name = "Herrmann"; print "The owner of this program is $f. Name $m. Initial $l. Namen"; 2022/1/21 國立中央大學電算中心 陳慶彥 18

Perl程式設計-資料型態(cont) Type Notation Example Integer NN 12 Floating point NN. NN 342. 176 Scientific

Perl程式設計-資料型態(cont) Type Notation Example Integer NN 12 Floating point NN. NN 342. 176 Scientific NN. NNENN 42. 04 E-6 Big number NN_NNN 6_000 Hex 0 x. NNNN 0 x. FFD 3 Octal 0 NNN 0374 2022/1/21 國立中央大學電算中心 陳慶彥 19

Perl程式設計-資料型態(cont)-Lab 3 print "Integer t $integer. Valuen"; Lab 3. value 1. cgi #! /usr/bin/perl

Perl程式設計-資料型態(cont)-Lab 3 print "Integer t $integer. Valuen"; Lab 3. value 1. cgi #! /usr/bin/perl print "Floating Point t”; $integer. Value = 10; Print “$floating. Point. Valuen"; $floating. Point. Value = 11. 43; print "Scientific t $scientific. Valuen"; $scientific. Value = 42. 03 E-04; $national. Debt = 6_000_000; print "National Debt t $national. Debtn"; $division. Value = 23/7; print "Division t $division. Valuen"; $hex. Value = 0 x 0 F 3; print "Hex tt $hex. Valuen"; $octal. Value = 037; print "Octal tt $octal. Valuen"; $itotal = $integer. Value + $hex. Value; print "nn"; $ftotal = $floating. Point. Value + $integer. Value; print "itotal = $itotaln"; $dtotal = $division. Value+ $octal. Value; 2022/1/21 print "ftotal = $ftotaln"; print "dtotal = $dtotaln"; 國立中央大學電算中心 陳慶彥 20

Perl程式設計-資料型態(cont)-Lab 4 scalearray 1. cgi #!/usr/bin/perl @Family. Name = ("Steven", "Michael", "Herrmann"); print $Family.

Perl程式設計-資料型態(cont)-Lab 4 scalearray 1. cgi #!/usr/bin/perl @Family. Name = ("Steven", "Michael", "Herrmann"); print $Family. Name[0]; print $Family. Name[1]; print $Family. Name[2]; print "nn"; print "$Family. Name[0] $Family. Name[1] $Family. Name[2]n"; 2022/1/21 國立中央大學電算中心 陳慶彥 24

Perl程式設計-資料型態(cont)-Lab 5 n Lab 5 scalearray 2. cgi(Mixed Data Assigned to Array Cells )

Perl程式設計-資料型態(cont)-Lab 5 n Lab 5 scalearray 2. cgi(Mixed Data Assigned to Array Cells ) #!/usr/bin/perl print "Cell number $cell. Index = $value n"; $cell. Index = 0; $cell. Index++; $mixed. Data[$cell. Index] = @mixed. Data; } $mixed. Data[2] = 876; print "nn"; $mixed. Data[6] = "The last cell"; print "The last cell of the array mixed. Data is $#mixed. Data"; $mixed. Data[4] = 3. 41467; foreach $value (@mixed. Data){ 2022/1/21 print "nn"; 國立中央大學電算中心 陳慶彥 25

Perl程式設計-資料型態(cont) n Hash Array(Associative Array) # 相關陣列是以 % 符號開頭的。 my %hash; my %hash=("i 1"=>"aaa",

Perl程式設計-資料型態(cont) n Hash Array(Associative Array) # 相關陣列是以 % 符號開頭的。 my %hash; my %hash=("i 1"=>"aaa", "i 2"=>"bbb", "i 3"=>"ccc"); $hash{'i 1'}="aaa"; $hash{'i 2'}="bbb"; $hash{'i 3'}="ccc"; 2022/1/21 國立中央大學電算中心 陳慶彥 27

Perl程式設計-資料型態(cont) -Lab 6 n Lab 6 hasharray. cgi #!/usr/bin/perl print. AA(%price. List); print "nn";

Perl程式設計-資料型態(cont) -Lab 6 n Lab 6 hasharray. cgi #!/usr/bin/perl print. AA(%price. List); print "nn"; print "You could print a hash cell like this $price. List{'Western Saddle'}n"; %price. List = ( "Hackamore"=>28. 92, "English Bridle"=>36. 31, "Western Saddle"=>124. 00, "Hunt Seat Saddle"=>178. 01, "Jumping Saddle"=>205. 87, "Leather Halter"=>24. 56, ); 2022/1/21 %price. List = ( 1358=>28. 92, 1359=>36. 31, 1965=>124. 00, 1966=>178. 01, 1977=>205. 87, 1357=>24. 56, ); 國立中央大學電算中心 陳慶彥 28

Perl程式設計-資料型態-Lab 6 (cont) n Lab 6 hasharray. cgi (cont) print "Or like this $price.

Perl程式設計-資料型態-Lab 6 (cont) n Lab 6 hasharray. cgi (cont) print "Or like this $price. List{1965} nn"; print. AA(%price. List); sub print. AA { my %my. Price. List = @_; foreach $key (keys(%my. Price. List)){ print "The price of item $key is $price. List{$key}n"; } print "nn"; } 2022/1/21 國立中央大學電算中心 陳慶彥 29

Perl程式設計-資料型態(cont) n References(Pointer) ⊙如何取得變數的位址? $scalar. Ref=$scalar. Var; $array. Ref=@array. Var; $hash. Ref=%hash. Var; $func.

Perl程式設計-資料型態(cont) n References(Pointer) ⊙如何取得變數的位址? $scalar. Ref=$scalar. Var; $array. Ref=@array. Var; $hash. Ref=%hash. Var; $func. Ref=&func. Name; 2022/1/21 國立中央大學電算中心 陳慶彥 31

Perl程式設計-資料型態(cont) n References(Pointer) ⊙如何使用指標? print $$scalar. Ref; print "@$array. Ref"; print $hash. Ref->{$key}; &$func.

Perl程式設計-資料型態(cont) n References(Pointer) ⊙如何使用指標? print $$scalar. Ref; print "@$array. Ref"; print $hash. Ref->{$key}; &$func. Ref; 2022/1/21 國立中央大學電算中心 陳慶彥 32

Perl程式設計-資料型態(cont)-Lab 7 n Lab 7 pointer. cgi #! /usr/bin/perl $hash. Ref={'a'=>'aa', 'b'=>'bb', 'c'=>'cc'}; print

Perl程式設計-資料型態(cont)-Lab 7 n Lab 7 pointer. cgi #! /usr/bin/perl $hash. Ref={'a'=>'aa', 'b'=>'bb', 'c'=>'cc'}; print "$hash. Ref->{'a'}t$hash. Ref->{'b'}t$hash. Ref>{'c'}n"; my %hash. Ref 1=('a'=>'aa', 'b'=>'bb', 'c'=>'cc'); print " $hash. Ref 1{'a'}t $hash. Ref 1{'b'}t $hash. Ref 1{'c'}n"; 2022/1/21 國立中央大學電算中心 陳慶彥 33

Perl程式設計-控制敘述 n 條件控制敘述 (Conditional Control Statements) v if (Expression) {Code Segment} else {Code Segment}

Perl程式設計-控制敘述 n 條件控制敘述 (Conditional Control Statements) v if (Expression) {Code Segment} else {Code Segment} v statement if (Expression); v statement unless (Expression); 2022/1/21 國立中央大學電算中心 陳慶彥 34

Perl程式設計-迴圈控制敘述 n 迴圈控制敘述 (Loop Control Statements) for($i=0; $i<=10; $i++) {Code Segment} foreach $i (@array)

Perl程式設計-迴圈控制敘述 n 迴圈控制敘述 (Loop Control Statements) for($i=0; $i<=10; $i++) {Code Segment} foreach $i (@array) {Code Segment} for $i (0. . 10) {Code Segment} while($i<=10) {Code Segment} do {Code Segment} while(Expression); 2022/1/21 國立中央大學電算中心 陳慶彥 35

Perl程式設計-I/O和檔案處理-Lab 9 n Lab 9 file. cgi #! /usr/bin/perl open (FILE, "$File. Name") ||

Perl程式設計-I/O和檔案處理-Lab 9 n Lab 9 file. cgi #! /usr/bin/perl open (FILE, "$File. Name") || die $!; $File. Name=">test. txt"; @LINES=<FILE>; open(WFILE, "$File. Name"); close(FILE); print WFILE "123n"; $SIZE=@LINES; print WFILE "456n"; print WFILE "789n"; for ($i=0; $i<$SIZE; $i++){ close(WFILE); $My. Dept[$i]=$LINES[$i]; #讀取test. txt內容 $File. Name="<test. txt"; print "$My. Dept[$i]"; my @My. Dept; 2022/1/21 } 國立中央大學電算中心 陳慶彥 40

Perl程式設計-Regular Expressions n n n Regular Expression通常是用來尋找特定的字串 樣式(pattern),也就是所謂格式辨認(patternmatching)的功能。它的運算子是『=~』和『 !~』,可以把它念做match和not match。 Syntax: $string =~ /regular

Perl程式設計-Regular Expressions n n n Regular Expression通常是用來尋找特定的字串 樣式(pattern),也就是所謂格式辨認(patternmatching)的功能。它的運算子是『=~』和『 !~』,可以把它念做match和not match。 Syntax: $string =~ /regular expression/expression modifier 例:$sentence =~ /Hello/ 2022/1/21 國立中央大學電算中心 陳慶彥 42

Perl程式設計- -Regular Expressions Pattern Quantifier:用來表示字元的數量關係。 2022/1/21 國立中央大學電算中心 陳慶彥 45

Perl程式設計- -Regular Expressions Pattern Quantifier:用來表示字元的數量關係。 2022/1/21 國立中央大學電算中心 陳慶彥 45

Perl程式設計- -Regular Expressions Character Patterns:下列的sequence用來match一些特定格式的字元 2022/1/21 國立中央大學電算中心 陳慶彥 46

Perl程式設計- -Regular Expressions Character Patterns:下列的sequence用來match一些特定格式的字元 2022/1/21 國立中央大學電算中心 陳慶彥 46

Perl程式設計-Regular Expressions-Lab 10 n Lab 10 regular 1. cgi #! /usr/bin/perl $url="http: //ccy. dd.

Perl程式設計-Regular Expressions-Lab 10 n Lab 10 regular 1. cgi #! /usr/bin/perl $url="http: //ccy. dd. ncu. edu. tw: 8080/cgi-bin/news. cgi"; ($host, $port, $file)=($url=~m|http: //([^/: ]+): {0, 1}(d*)(S*)$|); print “處理的字串: $urln"; print “處理後的主機名稱: $hostn"; print "處理後的主機port: $portn"; print “處理後的檔名: $filen"; 2022/1/21 國立中央大學電算中心 陳慶彥 53

Perl程式設計-Regular Expressions-Lab 10 n (S*)$ match一串非空白字元,並以找到的字串為結尾。找到的字串存 在$3中。 n ()=() ($host, $port, $file)=($1, $2, $3)

Perl程式設計-Regular Expressions-Lab 10 n (S*)$ match一串非空白字元,並以找到的字串為結尾。找到的字串存 在$3中。 n ()=() ($host, $port, $file)=($1, $2, $3) 即$host="my. machine. tw" $port=8080 $file="/cgi-bin/test. pl" 2022/1/21 國立中央大學電算中心 陳慶彥 55

Perl程式設計-利用Perl撰寫CGI程式 這是一個由Netscape Navigator 3. 0版所發出的 request: GET / HTTP/1. 0 Connection: Keep-Alive User-Agent: Mozilla/3.

Perl程式設計-利用Perl撰寫CGI程式 這是一個由Netscape Navigator 3. 0版所發出的 request: GET / HTTP/1. 0 Connection: Keep-Alive User-Agent: Mozilla/3. 0 (Win. NT; I) Host: ind. ntou. edu. tw Accept: image/gif, image/x-xbitmap, image/jpeg, */* 2022/1/21 國立中央大學電算中心 陳慶彥 58

Perl程式設計-利用Perl撰寫CGI程式 而ind的apache HTTP server的回應則是: HTTP/1. 1 200 OK Date: Sat, 06 Sep 1997 03:

Perl程式設計-利用Perl撰寫CGI程式 而ind的apache HTTP server的回應則是: HTTP/1. 1 200 OK Date: Sat, 06 Sep 1997 03: 32: 12 GMT Server: Apache/1. 2 b 11 Last-Modified: Mon, 16 Jun 1997 14: 03: 10 GMT ETag: "b 5496 -ab 0 -33 a 5479 e" Content-Length: 2736 Accept-Ranges: bytes Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html <html><body bgcolor="#ffffff"><title>Welcome to NTOU CC Email/WWW Server</title>. . . </body></html> 2022/1/21 國立中央大學電算中心 陳慶彥 59

Perl程式設計-利用Perl撰寫CGI程式-Lab 11 #! /usr/bin/perl sub Print. ENV { Print. Header(); my $key; print "<html><body>";

Perl程式設計-利用Perl撰寫CGI程式-Lab 11 #! /usr/bin/perl sub Print. ENV { Print. Header(); my $key; print "<html><body>"; foreach $key (sort keys %ENV) { Print. ENV(); print "</html></body>"; sub Print. Header { print "Content-type: print "$key = $ENV{$key} n"; } } text/htmlnn"; } 2022/1/21 國立中央大學電算中心 陳慶彥 61

應用系統實作–公佈欄-Client傳送data # 副程式: 系統登入 sub Login { print_top(); print "<center>"; print "<form action='$My. Para{'CGIFile.

應用系統實作–公佈欄-Client傳送data # 副程式: 系統登入 sub Login { print_top(); print "<center>"; print "<form action='$My. Para{'CGIFile. Name'}? $temp_function' method='post'>"; <p>帳號: <input name='login_name' size='20'></p>”; <p>密碼: <input name='login_password' type='password' value size='20'></p>"; print “<input name=‘B 1’ type=‘submit’ value=‘系統登入’>”; print “<input type='reset' value='清除設定'>"; print "</form>"; Print “</center>"; print_bottom(); } 2022/1/21 國立中央大學電算中心 陳慶彥 68

應用系統實作–公佈欄-Server端接收data @in = split(/&/, $in); Read. Form(*FORM); foreach $i (0. . $#in) { Print

應用系統實作–公佈欄-Server端接收data @in = split(/&/, $in); Read. Form(*FORM); foreach $i (0. . $#in) { Print “$FORM{‘login_name’}”; $in[$i] =~ tr/+/ /; Print “$FORM{‘login_password’}”; # Convert plus's to spaces # Split into key and value sub Read. Form { # splits on the first = local (*in) = @_ if @_; ($key, $val) = split(/=/, $in[$i], 2); local ($i, $key, $val); # Convert %XX from hex numbers to alphanumeric # Read in text $key =~ s/%(. . )/pack("C", hex($1))/ge; if ($ENV{'REQUEST_METHOD'} eq "GET") { $val =~ s/%(. . )/pack("C", hex($1))/ge; $in = $ENV{'QUERY_STRING'}; $val =~ s/<!--(. |n)*-->//g; # Kill SSI command } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { # Associate key and value # is the multiple separator read(STDIN, $in, $ENV{'CONTENT_LENGTH'}); if (defined($in{$key})) { if ($ENV{'QUERY_STRING'} =~ /=/) { $in{$key} = join("", $in{$key}, $val); $in = join("&", $in, $ENV{'QUERY_STRING'}); } else { } $in{$key} = $val; } } return 1; } } 2022/1/21 國立中央大學電算中心 陳慶彥 69