#!/usr/local/bin/perl
$url = 'www.nchu.edu.tw';
# Usage: /pattern/[g][o][i]
# g for global, o for only once, and i for case-insentitive
# url 是否包含 nchu 字串
if ( $url =~ /nchu/ )
{
print "$url is 國立中興大學.\n";
}
# 將 'edu' 取代為 EDU
# Can you use tr? (ie. tr/edu/EDU/)
$url =~ s/edu/EDU/;
print "\$url is now $url.\n";
那麼當成是接收到 input=aaa+bbb 的時候,它要如何將 +
轉換成空格呢?以下我們利用 GET 和 POST 的範例,分別列出轉換的方式。
GET 的方式:
form 表單:
該表單呼叫 sub.pl,而其程式碼如下:
#!/usr/local/bin/perl
print "Content-type: text/plain\n\n";
$input = $ENV{'QUERY_STRING'};
# translate + into space.
$input =~ tr/+/ /;
print "$input.\n";
# s: substitute
# \d: a number, A-F: A,B,C,D,E,F, a-f: a,b,c,d,e,f
# [\dA-Fa-f]: either a number, A, B, ... , F, a, b, ..., or f.
# pack("c", hex($1)): transform %[][] into a character ("c").
# e: treat pack() as an expression instead of a string.
# $input =~ s/%(..)/pack("c", hex($1))/eg;
$input =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("c", hex($1))/eg;
print "$input.\n";
This tutorial is copyrighted and provided as is and can be used as supplement
to any CGI tutorial materials such as
Common Gateway Interface,
written by 網際工作室(Internet Studio).
You are welcomed to use it for non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
chomp():由於使用者輸入數字之後需要按"Enter"鍵,因此變數
a、b、和 c 內的字串最後都有"跳行"的控制字元;因此我們需要使用 chomp()
把"跳行"的控制字元去除。
狀態 || 敘述:這個敘述的意思是"如果狀態為 false,則執行
|| 後的敘述。
die 的用法是將其後面的字串顯示出來之後,把程式結束掉。
格式化輸出
#!/usr/local/bin/perl
$s = "My name is Eric.";
$n = 5400;
$r = 123.456;
// 結果是 "My Name is Eric ^5400 ^^^^123.46"
printf "%15s %5d %10.2f\n", $s, $n, $r;
字串比較的運算子有: eq, lt, gt, le, ge, and ne。 相對的數值運算子有:
==, <, >, <=, >=, !=。
while 敘述: while (condition) { statements; }
do-while 敘述: do { statements; } while (condition);
for 敘述: for( initial_exp; test_exp; re-init_exp ) { statements; }
foreach $i ( @array ) { statements; }
控制結構與資料結構的例題:
例題 I:
if ($url{"address"} eq "www.cyut.edu.tw")
{
print "You're pointing to CYUT.\n";
}
if ($url{"port"} == 80)
{
print "Port is 80.\n";
}
else
{
print "Wrong port number.\n";
}
例題 II:
foreach $key (keys(%url))
{
print "key is $key.\n";
}
例題 III:
while (($key, $value) = each(%url))
{
print "$key is $value.\n";
}
例題 IV:
# if there are 6 elements in @url_list, when @url_list
# compares with an integer, it will return 6.
for ( $i = 0; $i < @url_list; $i++)
{
print "$url_list[$i] ";
}
print "\n";
# $#addr: size of addr - 1
for ( $i = 0; $i <= $#addr; $i++ )
{
print "$addr[$i]\n";
}
This tutorial is copyrighted and provided as is and can be used as supplement
to any CGI tutorial materials such as
Common Gateway Interface,
written by 網際工作室(Internet Studio).
You are welcomed to use it for non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
This tutorial is copyrighted and provided as is and can be used as supplement
to any CGI tutorial materials such as
Common Gateway Interface,
written by 網際工作室(Internet Studio).
You are welcomed to use it for non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
#!/usr/local/bin/perl
require 'req.pl';
require 'forms-lib.pl';
# open a file called "sa4a.txt" and assign it
# a filehandler called INP.
# if file cannot open, execute die().
open(INP, "sa4a.txt") || die("Cannot open sa4a.txt.\n");
@recs = <INP>;
# get the first record
$record = $recs[0];
# get rid of consecutive whitespaces
# \s: whitespace, +: one or more.
@fields = split(/\s+/, $record);
$num_field = @fields;
# a way of printing what we got.
#print "Record is @fields.\n";
#print "Number of field is $num_field.\n";
&Header("Grades");
# print table header
print "$record\n";
# read data till the end of the file
for($row=1; $row < @recs; $row++)
{
$record = $recs[$row];
print "$record\n";
}
&Footer;
寫資料到檔案:
#!/usr/bin/perl
$a1 = "Eric";
$a2 = 80;
$a3 = 75;
# ">sa4a.txt" 產生輸出檔
# ">>sa4a.txt" 產生附加式的輸出檔
open(OUTP, ">sa4a.txt") || die "Cannot open file\n";
print OUTP "$a1 $a2 $a3\n";
close OUTP;
select * from Product where Price >= 300 and Price < 10000
更改 (update): 將燒錄器的存量改為 20.
update Product set Qty=20 where Name='燒錄器'
刪除 (delete): 刪除燒錄器的產品資料
delete from Product where Name='燒錄器'
你可使用 Win32::OLE。 原始碼如下:
use Win32::OLE;
# setup the names of DSN and Table.
$DSN = "Samples";
$Table = "Product";
# Connect and open the database
$Conn = new Win32::OLE "ADODB.Connection";
$Conn->Open("DSN=$DSN;UID=;PWD=");
#$Conn->Open("$DSN", "", "");
#$Conn->Open("$DSN");
# The followings don't work well.
#$Conn->Open("DSN=Samples;UID=;PWD=") || die "Cannot connect";
#$Conn or die "Cannot connect";
# Execute the SQL statement
$RS = $Conn->Execute("SELECT * FROM $Table");
if(!$RS) {
$Errors = $Conn->Errors();
print "Errors:\n";
foreach $error (keys %$Errors) {
print $error->{Description}, "\n";
}
die;
}
# Retrieving records from Table.
while ( !$RS->EOF ) {
my($ID, $Name, $Price, $Qty) = (
$RS->Fields('ID')->value,
$RS->Fields('Name')->value,
$RS->Fields('Price')->value,
$RS->Fields('Qty')->value );
print $ID, " : ", $Name, " : ", $Price, " : ", $Qty, "\n";
$RS->MoveNext;
}
# Close the connection.
$RS->Close;
$Conn->Close;
若你使用 Win32::ODBC, 原始碼如下:
use Win32::ODBC;
# set Data Source Name
$DSN = "Samples";
# Open connection to DSN
if (!($O = new Win32::ODBC($DSN))){
print "Failure. \n\n";
exit();
}
# set Table name
$Table = Product;
# Get the content of the table.
if (! $O->Sql("SELECT * FROM $Table"))
{
# print out the field names.
@FieldNames = $O->FieldNames();
$Cols = $#FieldNames + 1;
for ($iTemp = 0; $iTemp < $Cols; $iTemp++){
$FmH2 .= "$FieldNames[$iTemp] ";
}
chop $FmH2;
print "$FmH2\n";
# Fetch the next rowset
while($O->FetchRow()){
undef %Data;
%Data = $O->DataHash();
print $Data{'ID'}, " ", $Data{'Name'}, " ",
$Data{'Price'}, " ", $Data{'Qty'}, "\n";
}
}
# Close Connection.
$O->Close();
#!/usr/bin/perl
use DBI;
# initialize datasourcename. for details, perldoc DBD::mSQL.
# to connect to a DB server on another host such as sun20,
#$dsn = 'db_test:sun20';
$dsn = 'db_test';
# 1. to connect to other DB such as Oracle, all you need to
# do is to replace "mSQL" with "Oracle". (assume that
# Oracle driver has been installed.)
#
# 2. to connect to MS Access, simply change DSN to 'dbi:ODBC:Samples'
#
$dbh = DBI->connect("dbi:mSQL:$dsn", "", "")
|| die "Can't connect to $data_source: $DBI::errstr";
$sth = $dbh->prepare("SELECT * FROM Product")
|| die "Can't prepare statement: $DBI::errstr";
$rc = $sth->execute
|| die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME} }\n";
while (($ID, $Name, $Price, $Qty) = $sth->fetchrow_array)
{
print "$ID, $Name, $Price, $Qty\n";
}
# check for problems which may have terminated the fetch early
die $sth->errstr if $sth->err;
$sth->finish;
#!/usr/bin/perl
use Msql;
# Connect to DB, db_test, located at the local host.
# mSQL is also available on sun20.im.cyut.edu.tw. Therefore, you
# can also connect to db_test on sun20 by
# $db = Msql->connect("sun20","db_test") or
# die "Cannot connect to db_test on sun20.";
# When using with CGI, it is a perfect example of 3-tier architecture.
#
$db = Msql->connect("","db_test") or die "Cannot connect to db_test";
# you can insert a row
#$db->query("insert into Product values('11', '數據機', 5000, 10)");
# or you can delete what you just inserted
#$db->query("delete from Product where ID='11'");
# submit the query.
$sth = $db->query("select * from Product") or die "Cannot Quety.";
# obtain various info about this table.
print "Number of rows: ", $sth->numrows, "\n";
print "Number of columns: ", $sth->numfields, "\n";
print "\n";
# obtain field names
@fnames = $sth->name;
@ftypes = $sth->type;
for($i=0; $i<$sth->numfields; $i++)
{
print $fnames[$i], " is of type ", $ftypes[$i], "\n";
}
print "\n";
# fetch records from the table
#
# Case 1: fetch a row at a time.
#while(@arr = $sth->fetchrow)
#{
# # print out field values of each row.
# foreach $arr (@arr)
# {
# print $arr, " ";
# }
# print "\n";
#}
#
# Case 2: fetch entire row, including field names, into a hash
while(%hash = $sth->fetchhash)
{
print $hash{'ID'}, " ", $hash{'Name'}, " ", $hash{'Price'},
" ", $hash{'Qty'}, "\n";
}
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
請勿轉貼
看其他教材
在 Part I 中,我們介紹了比較複雜的 .ajax() 方法;本文中,我們介紹
另一個相對簡單的方法 .get()。整個網頁都跟 Part I 幾乎相同,不一樣的
部分只有在 makeRequest() 方法:
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
我們的範例程式到此已經介紹完畢,可是我們要提醒讀者:這個系統只是
設計給單一使用者的;也就是說,這個系統的設計是假設一次只有一個使用者
在使用。試試看:打開兩個瀏覽器的視窗,然後分別開啟我們剛剛設計好的系統,
並分別在兩個視窗中新增資料,請觀察新增的資料是否立即反應再另一個視窗中。
練習題: 請新增一個"清除"的按鈕,使用者點選它之後,會把所有欄位的輸入
或者選擇的資料清除。
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
在本範例中,我們使用了第一種和第三種方式。第三種的使用方式就像
在 Part II 中說明的 Product 類別;至於第一種的使用方式
就是 Part III 的重點。在載入 inv.zul 網頁的時候,我們希望能夠
先到資料庫中把已經存在的存貨資料載入,並放到視窗內。為了
達成這個目標,我們首先把資料載入,載入的方式如下所示,
而 <zscript> 需要放在 <window> 內。
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
請勿轉貼
看其他教材
在第二個範例中,我們希望能夠設計一個系統,該系統除了使用 ZK 之外,
也使用關聯式資料庫,這樣的組合大概是目前最常見的。這個範例主要參考
ZK 的文件 Step by Step Tutorial: A Rela-World Application with a Database,而且為了上課方便,
我們使用了非常受歡迎的資料庫管理系統 MySQL。
這個範例的說明,我們把它分成三個部分,第一個部分主要著重於畫面的
設計,而第二個部分才說明資料庫的建立,第三個部分就在於 ZK 與
資料庫的互動,也就是 ZK 的事件處理機制。我們的目的在於
建立一個簡易的存貨管理系統,其畫面如下:
從畫面中,我們可以看得出來,整個版面是由之前介紹的 <window>
物件所形成的,該視窗的上半段條列出所有的存貨資料,而下半段則顯示對於存貨資料
的控制元件,包含輸入欄位以及增/改/刪的按鈕;使用者可以從這些控制
元件來新增、修改、以及刪除存貨資料。
就像之前說的,在 Part I,我們先說明這個視窗是如何形成的。第一個動作
就是修改前一個 Hello World 的範例,把視窗標題改成如下原始碼,並將
檔案名稱命名為 inv.zul 並放置於 d:\tomcat\webapps\test 中:
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu
The following examples had been tested on Mozilla's Firefox and Microsoft's
IE. The document is provided as is. You are welcomed to use it for
non-commercial purpose.
Written by: 國立中興大學資管系呂瑞麟 Eric Jui-Lin Lu