国立中央大学电算中心陈庆彦
《国立中央大学电算中心陈庆彦》由会员分享,可在线阅读,更多相关《国立中央大学电算中心陈庆彦(71页珍藏版)》请在装配图网上搜索。
1、2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 1 CGI程 式 設 計 (for UNIX Perl) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 2 主 題n CGI 及 其 環 境 簡 介 n Perl 程 式 設 計n 應 用 系 統 實 作 公 佈 欄 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 3 CGI 及 其 環 境 簡 介Internet Web Server 後 端資 料 庫CGI程 式前 端 瀏 覽 器 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 4 CGI 及 其
2、 環 境 簡 介 (cont)n 為 什 麼 我 的 HTML page/form 需 要 用 script因 為 有 的 時 候 您 需 要 在 HTML 文 件 中 使 用 動 態 資 料 ( 非 固 定 不 變的 資 料 )n CGI 各 代 表 什 麼 ?Common 向 您 確 保 CGI 可 以 使 用 多 種 程 式 語 言 並 和 多 種 不 同 的系 統 互 動G ateway 提 示 您 CGI 的 力 量 不 在 於 它 本 身 所 做 的 事 , 而 在 於 它提 供 了 連 結 其 他 系 統 的 潛 力Interface 只 是 表 示 CGI 對 如 何 善 用
3、其 特 性 提 供 了 明 確 的 界 定 換 句 話 說 , 您 可 以 設 計 程 式 來 適 當 利 用 這 個 介 面 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 5 CGI 及 其 環 境 簡 介 (cont)n CG I程 式 相 關 的 環 境 變 數 環 境 變 數 說 明 G A T E W A Y _ I N T E R F A C E S e r v e r 使 用 的 C G I 版 本 S E R V E R _ N A M E S e r v e r 的 h o s t 名 稱 或 I P 位 址 S E R V E R _ S O
4、F T W A R E 回 應 c lie n t r e q u e s t 的 S e r v e r 軟 體 名 稱 和 版 本 S E R V E R _ P R O T O C O L 傳 遞 資 訊 所 用 的 協 定 名 稱 或 版 本 S E R V E R _ P O R T S e r v e r 正 在 執 行 的 p o r t n u m b e r R E Q U E S T _ M E T H O D 發 出 r e q u e s t 的 方 法 P A T H _ I N F O 傳 遞 給 C G I 程 式 的 額 外 路 徑 P A T H _ T R
5、A N S L A T E D 存 在 P A T H _ I N F O 中 的 給 定 路 徑 的 傳 遞 版 本 S C R I P T _ N A M E 程 式 執 行 時 的 v ir tu a l p a th D O C U M E N T _ R O O T 網 路 提 供 的 文 件 服 務 所 在 路 徑 Q U E R Y _ S T R I N G 傳 遞 給 程 式 的 q u e r y 資 訊 R E M O T E _ H O S T 使 用 者 發 出 r e q u e s t 的 遠 端 h o s t 名 稱 R E M O T E _ A D D R
6、 使 用 者 發 出 r e q u e s t 的 遠 端 I P 位 址 A U T H _ T Y P E 用 來 確 定 使 用 者 合 法 性 的 鑑 定 方 法 R E M O T E _ U S E R 使 用 者 的 合 法 名 稱 R E M O T E _ I D E N T 發 出 r e q u e s t 的 使 用 者 C O N T E N T _ T Y P E q u e r y 資 料 中 的 M I M E 型 別 C O N T E N T _ L E N G T H 資 料 長 度 , 以 b y te 或 字 元 數 來 計 算 H T T P _
7、F O R M 使 用 者 發 出 r e q u e s t 的 電 子 郵 件 訊 息 H T T P _ A C C E P T c lie n t 可 以 接 受 的 M I M E 型 別 列 表 H T T P _ U S E R _ A G E N T c lie n t 用 來 發 出 r e q u e s t 的 瀏 灠 器 H T T P _ R E F E R E R 在 讀 取 C G I 程 式 前 ,c lie n t 所 指 的 文 件 U R L 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 6 CGI 及 其 環 境 簡 介 (co
8、nt) Perl是 什 麼 東 西 呢 ?它 是 Practical Extraction Report Language等 字 的 縮寫 , 是 一 種 能 掃 視 任 意 文 字 檔 , 並 且 能 從 中 取 出 資 訊製 成 報 表 的 解 譯 語 言 (Interpreter)。Perl的 作 者 Larry Wall說 過 一 句 話 : Perl是 懶 人 用 的工 具 。 如 果 Perl不 是 比 其 它 語 言 更 好 更 簡 單 更 容 易上 手 , 恐 怕 很 少 人 願 意 去 學 習 新 的 程 式 語 言 。 2021/5/27 國 立 中 央 大 學 電 算 中
9、 心 陳 慶 彥 7 CGI 及 其 環 境 簡 介 (cont) Perl的 特 性 Perl有 非 常 傑 出 的 文 字 處 理 能 力 , 能 輕 易 地 產 生HTML文 件 , 尤 其 它 具 有 完 整 的 Regular Expression語 法 ,使 得 在 C語 言 看 來 必 需 要 寫 一 大 串 程 式 碼 的 功 能 , Perl幾 行 就 解 決 了 。 Perl是 一 種 直 譯 式 語 言 , 因 此 可 以 避 免 在 各 種 不 同 系統 上 , 還 需 要 重 新 編 譯 的 麻 煩 。 Perl的 檢 查 旗 標 (-T)可 以 保 護 你 的 We
10、b Server, 避 免被 未 經 授 權 的 client破 壞 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 8 CGI 及 其 環 境 簡 介 (cont)n 哪 些 平 台 上 有 Perl? 要 到 哪 裡 去 找 ? 這 套 原 始 碼 完 全 不 需 任 何 移 植 工 作 便 可 輕 易 地 在絕 大 多 數 的 Unix系 統 ( Perl的 原 生 系 統 ) , 以 及 Plan 9、 VMS、 QNX、 OS/2 , 和 Amiga上 編 譯 安裝 完 成 。適 合 32 位 元 的 微 軟 系 統 和 蘋 果 系 統 、 以 執 行 檔
11、形 式 發 行 的 Perl各 可 在 http:/ 個 目 錄 底 下找 到 。http:/www.math.ncu.edu.tw/chenym/perl/ 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 9 CGI 及 其 環 境 簡 介 (cont)n Perl的 相 關 資 料 要 上 哪 兒 找 ?v man perlv perldoc perlv http:/ 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 10 Perl程 式 設 計 -執 行 環 境n server 通 常 是 設 定 成 去 執 行 放 在 cgi-bin 目
12、錄 底 下 的 CGI 程 式 。 不 過 , server 管 理 者 同 時 也 可 以 在 設 定 檔中 設 定 aliases, 讓 所 有 含 某 些 副 檔 名 ( 如 .cgi、 .pl) 的 scripts 都 能 執 行 AllowOverride None Options ExecCGI Options Indexes Includes ExecCGI AllowOverride None order allow,deny n 什 麼 是 檔 案 使 用 權 限 ? 怎 樣 改 ? 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 11 Perl程 式
13、 設 計 -Lab 1.您 的 第 一 個 程 式n 選 擇 編 輯 器 vi , joe , ee , pe2或 是 在 client端 將 編 輯 好了 的 檔 案 上 傳 到 servern 您 的 第 一 個 程 式 hello.cgi #! /usr/bin/perl print “hello world! n”;n Chmod 755 hello.cgi n 執 行 hello.cgi 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 12 Perl程 式 設 計 -資 料 型 態n Scalar:純 量 變 數 是 Perl裡 最 基 本 的 一 種 資 料
14、 型 態 , 它可 以 代 表 一 個 字 元 、 字 串 、 整 數 、 甚 至 浮 點 數 , 而 Perl把 它 們 都 看 成 是 一 樣 的 ! n Scalar Array:純 量 陣 列 , 陣 列 內 的 每 一 個 元 素 都 是Scalar variablen Hash Array(Associative Array):雜 湊 陣 列 也 叫做 相 關 陣 列 , 它 和 一 般 陣 列 沒 什 麼 不 同 , 差 別 只 是 在它 的 索 引 值 用 的 是 字 串 , 而 非 一 般 陣 列 所 用 的 整 數 值n References(Pointer): Perl
15、5新 增 了 參 考 指 標 的 資料 型 態 使 Perl和 C一 樣 可 借 由 指 標 建 立 一 些 複 雜 的 資 料結 構 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 13 Perl程 式 設 計 -資 料 型 態 (cont)n Scalar:字 串 運 算 子 String Operator Purpose x Returns a string consisting of the string on the left of the operand, repeated the number of times of the right operand
16、. Concatenates the two strings on both sides of the operator. eq Returns True if the two operands are equivalent, False otherwise. ne Returns True if the two operands are not equal, False otherwise. le Returns True if the operand on the left is stringwise less than the operand on the right of the op
17、erator. Returns False otherwise. lt Returns True if the operand on the left is stringwise less than or equal to the operand on the right of the operator. Returns False otherwise. ge Returns True if the operand on the left is stringwise greater than or equal to the operand on the right of the operato
18、r. Returns False otherwise. 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 14 Perl程 式 設 計 -資 料 型 態 (cont)n Scalar:字 串 運 算 子 String Operator Purpose gt Returns True if the operand on the left is stringwise greater than the operand on the right of the operator. Returns False otherwise. cmp Returns -1, 0, or 1 if
19、 the left operand is stringwise less than, equal to, or greater than the right operand. , Evaluates the left operand, the evaluates the right operand. It returns the result of the right operand. + Increments the string by one alphabetic value. 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 15 Perl程 式 設 計 -資 料
20、型 態 (cont)n Scalar:數 值 運 算 子Value Operator Purpose + Computes the additive value of the two operands. - Computes the difference between the two operands. * Computes the multiplication of the two operands. / Computes the division between the two operands. % Computes the modulus(remainder) of the two
21、operands. = = Returns Ture if the two operands are equivalent, False otherwise. != Returns Ture if the two operands are not equal, False otherwise. Returns Ture if the operand on the left is numerically greater than or equal to the operand on the right of the operator. Returns False otherwise. Retur
22、ns Ture if the operand on the left is numerically greater than the operand on the right of the operator. Returns False otherwise. Returns -1 if the left operand is less than the right, +1 if is it greater than, and 0(False) otherwise. print The owner of this program is $namen; $firstName = Eric ; $m
23、iddleInitial = C. ; $lastName = Herrmann; print The owner of this program is $firstName $middleInitial; print $lastNamen; $fName = Eric; $mInitial = C.; $lName = Herrmann; print The owner of this program is $fName $mInitial $lNamen; 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 19 Perl程 式 設 計 -資 料 型 態 (cont)T
24、ype Notation Example Integer NN 12 Floating point NN.NN 342.176 Scientific NN.NNENN 42.04E-6 Big number NN_NNN_NNN 6_000_000 Hex 0 xNNNN 0 xFFD3 Octal 0NNN 0374 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 20 Perl程 式 設 計 -資 料 型 態 (cont)-Lab 3 Lab3. value1.cgi #! /usr/bin/perl$integerValue = 10;$floatingPoint
25、Value = 11.43;$scientificValue = 42.03E-04;$nationalDebt = 6_000_000_000_000;$divisionValue = 23/7;$hexValue = 0 x0F3;$octalValue = 037; $itotal = $integerValue + $hexValue;$ftotal = $floatingPointValue + $integerValue;$dtotal = $divisionValue+ $octalValue;print Integer t $integerValuen;print Floati
26、ng Point t”;Print “$floatingPointValuen;print Scientific t $scientificValuen;print National Debt t $nationalDebtn;print Division t $divisionValuen;print Hex tt $hexValuen;print Octal tt $octalValuen;print nn;print itotal = $itotaln;print ftotal = $ftotaln;print dtotal = $dtotaln; 2021/5/27 國 立 中 央 大
27、 學 電 算 中 心 陳 慶 彥 21 Perl程 式 設 計 -資 料 型 態 -Lab 3(cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 22 Perl程 式 設 計 -資 料 型 態 (cont)n Scalar Array:純 量 陣 列 , 陣 列 內 的 每 一 個 元 素 都 是 Scalar variable。 宣告 及 使 用 方 式 如 下 : # 純 量 陣 列 以 開 頭 。my array;my array=qw(a b c d);# qw 函 數 會 將 其 後 的 每 個 元 素 用 逗 點 隔 開 , 效 果 就 像 下面
28、這 行 。my array=(a,b,c,d); 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 23 Perl程 式 設 計 -資 料 型 態 (cont)n Scalar Array:# 當 然 你 也 可 以 一 個 個 元 素 宣 告 , 下 面 就 是 存 取 每 一 個 元素 的 方 法 。# 因 為 陣 列 中 的 每 一 個 元 素 都 是 純 量 變 數 , 所 以 要 以 $ 開頭 ,# 剛 開 始 容 易 搞 混 , 請 注 意 。$array0=a; $array1=b; $array2=c; $array3=d;# 使 用 for loop印
29、出 陣 列 內 每 個 元 素 的 值 。for($i=0; $iaaa,i2=bbb,i3=ccc);$hashi1=aaa; $hashi2=bbb; $hashi3=ccc; 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 28 Perl程 式 設 計 -資 料 型 態 (cont) -Lab6#!/usr/bin/perlprint nn;%priceList = ( Hackamore=28.92, English Bridle=36.31, Western Saddle=124.00, Hunt Seat Saddle=178.01, Jumping Sad
30、dle=205.87, Leather Halter=24.56,); printAA(%priceList);print You could print a hash cell like this $priceListWestern Saddlen;%priceList = ( 1358=28.92, 1359=36.31, 1965=124.00, 1966=178.01, 1977=205.87, 1357=24.56, );n Lab 6 hasharray.cgi 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 29 Perl程 式 設 計 -資 料 型 態
31、-Lab6 (cont) print Or like this $priceList1965 nn;printAA(%priceList);sub printAA my %myPriceList = _; foreach $key (keys(%myPriceList) print The price of item $key is $priceList$keyn; print nn;n Lab 6 hasharray.cgi (cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 30 Perl程 式 設 計 -資 料 型 態 -Lab6 (cont) 2021
32、/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 31 Perl程 式 設 計 -資 料 型 態 (cont) n References(Pointer) 如 何 取 得 變 數 的 位 址 ? $scalarRef=$scalarVar; $arrayRef=arrayVar; $hashRef=%hashVar; $funcRef= 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 32 Perl程 式 設 計 -資 料 型 態 (cont)n References(Pointer) 如 何 使 用 指 標 ? print $scalarRef; pri
33、nt $arrayRef; print $hashRef-$key; 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 33 Perl程 式 設 計 -資 料 型 態 (cont)-Lab7#! /usr/bin/perl$hashRef=a=aa,b=bb,c=cc;print $hashRef-at$hashRef-bt$hashRef-cn;my %hashRef1=(a=aa,b=bb,c=cc);print $hashRef1at $hashRef1bt $hashRef1cn;n Lab 7 pointer.cgi 2021/5/27 國 立 中 央 大 學
34、電 算 中 心 陳 慶 彥 34 Perl程 式 設 計 -控 制 敘 述n 條 件 控 制 敘 述 (Conditional Control Statements)v if (Expression) Code Segmentv if (Expression) Code Segment else Code Segmentv statement if (Expression);v statement unless (Expression); 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 35 Perl程 式 設 計 -迴 圈 控 制 敘 述 n 迴 圈 控 制 敘 述
35、(Loop Control Statements)for($i=0; $i=10; $i+) Code Segmentforeach $i (array) Code Segmentfor $i (0.10) Code Segmentwhile($i=10) Code Segmentdo Code Segment while(Expression); 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 36 Perl程 式 設 計 -控 制 敘 述 -Lab8#! /usr/bin/perlmy custom;$custom0=張 三 ;$custom1=李 四 ;$cust
36、om2=王 五 ;for ($i=0; $i=$#custom; $i+) if ( $custom$i eq 李 四 ) print 我 是 李 四 n; $cnt=0;while ($cnt=$#custom) print 我 是 $custom$cntt; $cnt+;print “n”;# 第 一 個 引 數 是 純 量 變 數 , 第 二 個 引 數 要 用#括 號 括 起 來 , 裡 面 是 一 個 純 量 陣 列 , 顧 名#思 義 它 就 是 把 陣 列 中 的 每 個 元 素 依 序 傳 給#第 一 個 引 數 , 直 到 全 部 傳 完 。foreach $i (custo
37、m) print 我 是 $it; n Lab 8 if.cgi 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 37 n Syntax: sub NAME Coden 呼 叫 副 程 式 : 可 以 開 啟 一 個 以 string 指 定 檔 名 的 檔 案 ,並 傳 回 檔 案 代 碼 給 filehead 這 個 函 式 ,如 果 執 行 程 功 的 話 ,會 傳 回 true, 否 則 就 傳 回 undef. 如 果 開 頭 字 元 為 ,就 會 以 寫 入 模 式 開 啟 如 果 開 頭 字 元 是 以 ,就 會 以 新 增 模 式 開 啟 如 果 開 頭
38、 字 元 為 + (例 : + , test.txt;open(WFILE, $FileName); print WFILE 123n; print WFILE 456n; print WFILE 789n;close(WFILE);#讀 取 test.txt內 容$FileName=test.txt; my MyDept; open (FILE,$FileName) | die $!;LINES=;close(FILE);$SIZE=LINES;for ($i=0;$i$SIZE;$i+) $MyDept$i=$LINES$i; print $MyDept$i;n Lab 9 file.cg
39、i 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 41 Perl程 式 設 計 -控 制 字 元n 換 行t 定 位 (tab)a 警 示 (發 出 嗶 聲 )r 歸 位f 跳 頁cD Ctrl-DcC Ctrl-C 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 42 n Regular Expression通 常 是 用 來 尋 找 特 定 的 字 串樣 式 (pattern), 也 就 是 所 謂 格 式 辨 認 (pattern-matching)的 功 能 。 它 的 運 算 子 是 = 和 ! , 可 以 把 它 念 做 match
40、和 not match。 n Syntax: $string = /regular expression/expression modifier n 例 : $sentence = /Hello/Perl程 式 設 計 -Regular Expressions 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 43 Perl程 式 設 計 - -Regular Expressions g Match globally, i.e. find all occurrences. i Makes the search case-insensitive. m If the stri
41、ng has new-line characters embedded within it, the metacharacters and $ will not work correctly. This modifier tells Perl to treat this line as a multiple line. o Only compile pattern once. s The character . matches any character except a new line. This modifier treats this line as a single line, wh
42、ich allows . to match a new-line character. x Allows white space in the expression. Modifiers:修 飾 選 項 可 有 可 無 , 它 是 用 來 對 整 個 敘 述 作 修 正 的 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 44 Perl程 式 設 計 - -Regular Expressions Tells Perl to accept the following characters as a regular character; this removes spe
43、cial meanings from any metacharacter. Matches the beginning of the string, unless /m is used. . Matches any character except a new line character, unless /s is used. $ Matches the end of the string, unless /m is used. | Expresses alternation. This means the expressions will search for multiple patte
44、rns in the same string. ( ) Groups expressions to assist in alternation and back referencing. Looks for a set of characters. Metacharacter: 下 面 這 些 字 元 都 具 有 特 殊 意 義 , 可 以 讓 你 建 立 更 複 雜 的 搜 尋 樣 式 (searching pattern)。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 45 Perl程 式 設 計 - -Regular Expressions * Matchs
45、0 or more times. + Matchs 1 or more times. ? Matchs 0 or 1 times. n Matches exactly n times. n, Matches at least n times. n,m Matches at least n times but no more than m times. Pattern Quantifier: 用 來 表 示 字 元 的 數 量 關 係 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 46 Perl程 式 設 計 - -Regular Expressionsr Carr
46、iage return(CR), ASCII 13(十 進 位 ) n New line, UNIX 中 代 表 ASCII 10(十 進 位 ), DOS(Windows)系 統 中 則 是ASCII 13 + ASCII 10(十 進 位 ). t Tab, ASCII 9(十 進 位 ) w Matches an alphanumeric character. Alphanumeric also includes _. 即 A-Za-z0-9_. W Matches a nonalphanumeric character. 即 A-Za-z0-9_. s Matches a white
47、space character. This includes space, tab, FormFeed and CR/LF. 即 tfrn. S Matches a non-whote space character. 即 tfrn. d Matches a digit. 即 0-9. D Matches a nondigit character. 即 0-9. b Matches a word boundary. B Matches a nonword boundary. 033 octal char x1B hex char Character Patterns: 下 列 的 sequen
48、ce用 來 match一 些 特 定 格 式 的 字 元 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 47 n /abc/ 找 到 含 有 abc的 字 串n /abc/ 找 到 開 頭 是 abc的 字 串n /abc$/ 找 到 結 尾 是 abc的 字 串n /a|b/ 找 到 有 a或 b的 字 串 , 也 可 以 用 來 找 整 個 字 (word)Perl程 式 設 計 -Regular Expressions-Exam. 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 48 n /ab2,4c/ 找 到 a後 面 跟 著 2-4
49、個 b, 再 跟 著 c的 字 串 , 若 只 有/ab2,c/則 會 找 二 個 以 上 的 bn /ab*c/ 找 到 a後 面 跟 著 0個 或 多 個 b, 再 跟 著 c的 字 串 , 如同 /ab0,c/n /ab+c/ 找 到 a後 面 跟 著 一 個 以 上 的 b, 再 跟 著 c的 字 串 ,如 同 /ab1,c/Perl程 式 設 計 -Regular Expressions(cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 49 n /a.c/ .可 以 代 表 任 何 字 元 , 除 了 new line字 元 (n)外 。n /ab
50、c/ 找 到 含 有 這 三 個 字 元 中 任 何 一 個 的 字 串n /d/ 找 到 含 有 數 字 的 字 串 , 如 同 /0-9/n /w/ 找 到 含 有 字 母 的 字 串 , 如 同 /a-zA-Z0-9_/Perl程 式 設 計 -Regular Expressions(cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 50 n /s/ 找 到 含 有 white space的 字 串 , 如 同 / trnf/n /abc/ 找 到 沒 有 abc任 一 字 元 的 字 串n /*/ 找 到 含 有 字 元 *的 字 串 , 在 反 斜
51、線 後 面 的 字 元 Perl會 把 它 當 作 普 通 字 元 看 待 。 若 你 不 確 定 這 個 符 號 是否 為 特 殊 字 元 , 乾 脆 全 加 上 以 策 安 全 。n /abc/i 忽 略 abc的 大 小 寫Perl程 式 設 計 -Regular Expressions(cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 51 n /(d+).(d+).(d+).(d+)/ 找 到 類 似 IP的 字 串 , 並 將 IP的 四 個 數 字 分 別 存 在$1,$2,$3,$4四 個 特 殊 變 數 中 , 以 便 在 其 後 加 以 利
52、用 。 例 : if ($x = /(d+.d+).d+.d+/) print “中 央 大 學 if ($1 eq 140.115); n $x = s/s+/g把 所 有 的 white space全 部 去 除 掉 n $x = s/( *):*( *)/$2:$1/把 用 :分 開 的 兩 個 欄 位 互 相 對 調 Perl程 式 設 計 -Regular Expressions(cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 52 n $x = tr/this/that/把 this替 換 成 that n $x = tr/a-z/A-Z/把 小
53、 寫 字 母 全 部 替 換 成 大 寫 字 母 n $count = $x = tr/*/*/計 算 $x中 有 幾 個 * Perl程 式 設 計 -Regular Expressions(cont) 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 53 Perl程 式 設 計 -Regular Expressions-Lab10#! /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*)
54、$|);print “處 理 的 字 串 :$urln;print “處 理 後 的 主 機 名 稱 :$hostn;print 處 理 後 的 主 機 port:$portn;print “處 理 後 的 檔 名 :$filen;n Lab 10 regular1.cgi 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 54 Perl程 式 設 計 -Regular Expressions-Lab10n $url=m|m命 令 可 以 讓 你 自 訂 pattern的 分 隔 符 號 ,m後 面 跟 著 的 就 是 分 隔 符 號 , | |是 在 字 串 中 尋 找
55、 多 個 pattern。n (/:+)match一 個 字 串 , 裡 面 沒 有 /和 :字 元 。 找 到 的 字 串 存 在 $1中 。n :0,1(d*)match 0或 1個 :, 後 面 跟 著 一 串 數 字 或 nothing。 找 到 的 字 串 存 在 $2中 ,若 找 不 到 , $2就 是 空 的 。n (S*)$match一 串 非 空 白 字 元 , 並 以 找 到 的 字 串 為 結 尾 。 找 到 的 字 串 存在 $3中 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 55 Perl程 式 設 計 -Regular Express
56、ions-Lab10n (S*)$match一 串 非 空 白 字 元 , 並 以 找 到 的 字 串 為 結 尾 。 找 到 的 字 串 存在 $3中 。n ()=()($host, $port, $file)=($1, $2, $3)即 $host=my.machine.tw$port=8080$file=/cgi-bin/test.pl 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 56 Perl程 式 設 計 -利 用 Perl撰 寫 CGI程 式HTTP headers(表 頭 ) HTTP通 訊 協 定 是 一 種 很 簡 單 的 client-serve
57、r通 訊 協 定 , client和 server之 間 的 溝通 由 兩 部 分 所 組 成 , 一 是 HTTP headers,另 一 就 是 文 件 或 資 料 本 身 了 , 中 間 則 以 一個 空 白 行 隔 開 。 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 57 Perl程 式 設 計 -利 用 Perl撰 寫 CGI程 式由 於 瀏 覽 器 和 HTTP server都 有 很 多 不 同 的 版 本 ,所 使 用 的 HTTP headers也 各 不 相 同 , 常 用 的HTTP headers有 下 列 幾 種 : Header 範 例
58、與 說 明Status 傳 回 的 狀 態 碼 , 若 沒 有 錯 誤 就 會 出 現 200 OK例 : HTTP/1.1 200 OK Content-type 輸 出 字 串 的 MIME格 式 。例 : Content-Type: text/html Content-length 輸 出 字 串 的 長 度 , 通 常 以 byte計 算 。 例 : Content-Length: 2736 Location Server重 新 導 向 , 也 就 是 叫 瀏 覽 器 再 連 到 另 外 的 這 個 位 址 。例 : Location: http:/www.ncu.edu.tw/ Pr
59、agma 關 掉 瀏 覽 器 的 cache, 也 就 是 不 要 把 這 份 文 件 存 在 cache中 。例 : Pragma: no-cache 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 58 Perl程 式 設 計 -利 用 Perl撰 寫 CGI程 式這 是 一 個 由 Netscape Navigator 3.0版 所 發 出 的request: GET / HTTP/1.0Connection: Keep-AliveUser-Agent: Mozilla/3.0 (WinNT; I)Host: ind.ntou.edu.twAccept: imag
60、e/gif, image/x-xbitmap, image/jpeg, */* 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 59 Perl程 式 設 計 -利 用 Perl撰 寫 CGI程 式HTTP/1.1 200 OKDate: Sat, 06 Sep 1997 03:32:12 GMTServer: Apache/1.2b11Last-Modified: Mon, 16 Jun 1997 14:03:10 GMTETag: b5496-ab0-33a5479eContent-Length: 2736Accept-Ranges: bytesKeep-Alive:
61、 timeout=15, max=100Connection: Keep-AliveContent-Type: text/html Welcome to NTOU CC Email/WWW Server.而 ind的 apache HTTP server的 回 應 則 是 : 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 60 Perl程 式 設 計 -利 用 Perl撰 寫 CGI程 式輸 出 網 頁 內 容 的 步 驟 先 輸 出 Content-type: text/htmlnn 再 輸 出 網 頁 的 內 容 2021/5/27 國 立 中 央 大 學 電 算
62、 中 心 陳 慶 彥 61 Perl程 式 設 計 -利 用 Perl撰 寫 CGI程 式 -Lab11#! /usr/bin/perl PrintHeader(); print ; PrintENV(); print ;sub PrintHeader print Content-type: text/htmlnn; sub PrintENV my $key; foreach $key (sort keys %ENV) print $key = $ENV$keyn; 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 62 Perl程 式 設 計 -利 用 Perl撰 寫
63、CGI程 式 -Lab11 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 63 應 用 系 統 實 作 公 佈 欄 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 64 應 用 系 統 實 作 公 佈 欄 -系 統 架 構功 能 架 構瀏 覽 公 告 張 貼 公 告 帳 號 管 理 系 統 管 理版 權 宣 告 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 65 應 用 系 統 實 作 公 佈 欄 -系 統 架 構執 行 News.cgiReadPara();ReadDept();ReadUser(); 輸 入 “ ”
64、輸 入 “ 系 統 管 理 ”輸 入 “ 帳 號 管 理 ”輸 入 “ 張 貼 公 告 ” ReadNews();SystemConfigForm();AddUserForm(); PostNewsForm(); 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 66 應 用 系 統 實 作 公 佈 欄 (cont)CGI 功 能 說 明CGI 是 Common Gateway Interface 的 簡 稱 . 其 主 要 的 功 能 是 在 WWW 環 境 下 , 藉 由 從 使 用 者 端 傳 遞 一 些 訊 息 給 WWW 伺 服 軟 體 , 再 由 WWW 伺
65、服 軟 體 去 啟 動 所 指 定 的 程 式 碼 來 完 成 特 定 的 工 作 . 所 以 更 明 確 的 說 , CGI 僅 是 在 WWW 主 機 端 上 的 可 執 行 的 程 式 碼 . 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 67 應 用 系 統 實 作 公 佈 欄 -資 料 傳 遞 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 68 應 用 系 統 實 作 公 佈 欄 -Client傳 送 data# 副 程 式 : 系 統 登 入 sub Login print_top(); print ; print ; 帳 號 :
66、”; 密 碼 :; print “” ; print “; print ; Print “;print_bottom(); 2021/5/27 國 立 中 央 大 學 電 算 中 心 陳 慶 彥 69 應 用 系 統 實 作 公 佈 欄 -Server端 接 收 dataReadForm(*FORM); Print “$FORMlogin_name”;Print “$FORMlogin_password”;sub ReadForm local (*in) = _ if _; local ($i, $key, $val); # Read in text if ($ENVREQUEST_METHOD eq GET) $in = $ENVQUERY_STRING; elsif ($ENVREQUEST_METHOD eq POST) read(STDIN,$in,$ENVCONTENT_LENGTH); if ($ENVQUERY_STRING = /=/) $in = join( in = split(/foreach $i (0 .$#in) $in$i = tr/+/ /; # Con
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。