Title: CGI????(for UNIX Perl)
1CGI????(for UNIX Perl)
2??
- CGI ??????
- Perl ????
- ?????? ???
3CGI ??????
?? ???
Internet
CGI??
?????
4CGI ??????(cont)
- ????? HTML page/form ??? script
- ?????????? HTML ?????????(????????)
- CGI ??????
- Common ???? CGI ?????????????? ???????
- Gateway ??? CGI ?????????????,? ???????????????
- Interface ???? CGI ??????????????
??-????,????????????? ????
5CGI ??????(cont)
6CGI ??????(cont)
- Perl???????
- ??Practical Extraction Report Language?????,??????
?????,??????????????????(Interpreter)? - Perl???Larry Wall??????Perl???????????Perl???????
??????????,?????????????????
7CGI ??????(cont)
- Perl???
- ?Perl????????????,??????HTML??,????????Regular
Expression??,???C?????????????????,Perl??????? - ?Perl????????,??????????????,???????????
- ?Perl?????(-T)??????Web Server,????????client???
8CGI ??????(cont)
?????????????????????????? Unix??(Perl?????),??
Plan 9?VMS?QNX? OS/2 ,? Amiga???????? ?? 32
?????????????????????? Perl??? http//www.perl.com
/CPAN/ports/????????? http//www.math.ncu.edu.tw/
chenym/perl/
9CGI ??????(cont)
- man perl
- perldoc perl
- http//www.perl.com/perl/info/documentation.html
10Perl????-????
- server ??????????? cgi-bin ????? CGI ?????,
server ??????????????? aliases,?????????(?
.cgi?.pl)? scripts ???? - ltDirectory /home/httpd/cgi-bingt
- AllowOverride None
- Options ExecCGI
- lt/Directorygt
- ltDirectory /home//public_htmlgt
- Options Indexes Includes ExecCGI
- AllowOverride None
- order allow,deny
- lt/Directorygt
- ??????????????
-
11Perl????-Lab 1.???????
- ?????
- vi , joe , ee , pe2???client????????????serve
r - ???????hello.cgi
- ! /usr/bin/perl
- print hello world! \n
- Chmod 755 hello.cgi
- ??hello.cgi
12Perl????-????
- Scalar?????Perl???????????,?????????????????????,
?Perl??????????! - Scalar Array????,???????????Scalar variable
- Hash Array(Associative Array)???????????,????????
???,???????????????,???????????? - References(Pointer)Perl 5?????????????Perl?C?????
??????????????
13Perl????-????(cont)
14Perl????-????(cont)
15Perl????-????(cont)
16Perl????-????(cont)
17Perl????-????(cont)
18Perl????-????(cont)-Lab 2
Lab2.
Stg1.cgi ! /usr/bin/perl name "Eric C.
Herrmann" print "The owner of this program is
name\n" firstName "Eric "
middleInitial "C. " lastName
"Herrmann" print "The owner of this program
is firstName middleInitial" print
"lastName\n" fName "Eric" mInitial
"C." lName "Herrmann" print "The owner
of this program is fName mInitial lName\n"
19Perl????-????(cont)
Type 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 0xNNNN 0xFFD3
Octal 0NNN 0374
20Perl????-????(cont)-Lab 3
Lab3. value1.cgi !
/usr/bin/perl integerValue 10 floatingPointVa
lue 11.43 scientificValue
42.03E-04 nationalDebt 6_000_000_000_000 div
isionValue 23/7 hexValue 0x0F3 octalValue
037 itotal integerValue
hexValue ftotal floatingPointValue
integerValue dtotal divisionValue
octalValue
print "Integer \t integerValue\n" print
"Floating Point \t Print floatingPointValue\n"
print "Scientific \t scientificValue\n" print
"National Debt \t nationalDebt\n" print
"Division \t divisionValue\n" print "Hex \t\t
hexValue\n" print "Octal \t\t
octalValue\n" print "\n\n" print "itotal
itotal\n" print "ftotal ftotal\n" print
"dtotal dtotal\n"
21Perl????-????-Lab 3(cont)
22Perl????-????(cont)
????,???????????Scalar variable??????????
????? _at_ ???my _at_arraymy _at_arrayqw(a b c d)
qw ????????????????,?????????my
_at_array("a","b","c","d")
23Perl????-????(cont)
?????????????,???????????????
?????????????????,???? ??, ???????,????array
0"a" array1"b" array2"c"
array3"d" ??for loop????????????for(i0
iltarray i) print "arrayi\n"
24Perl????-????(cont)-Lab4
Lab 4 scalearray1.cgi !/usr/bin/perl
_at_FamilyName ("Steven", "Michael", "Herrmann")
print FamilyName0 print FamilyName1
print FamilyName2 print "\n\n" print
"FamilyName0 FamilyName1 FamilyName2\n"
25Perl????-????(cont)-Lab5
- Lab 5 scalearray2.cgi(Mixed Data Assigned to
Array Cells )
!/usr/bin/perl cellIndex 0 mixedDatacellIn
dex \_at_mixedData mixedData2
876 mixedData6 "The last cell" mixedData4
3.41467 foreach value (_at_mixedData)
print "Cell number cellIndex value \n"
cellIndex print "\n\n" print "The last
cell of the array mixedData is
mixedData" print "\n\n"
26Perl????- ????-Lab5(cont)
27Perl????-????(cont)
- Hash Array(Associative Array)
?????? ??????my hashmy hash("i1"gt"aaa",
"i2"gt"bbb","i3"gt"ccc") hash'i1'"aaa"
hash'i2'"bbb" hash'i3'"ccc"
28Perl????-????(cont) -Lab6
!/usr/bin/perl print "\n\n" priceList (
"Hackamore"gt28.92, "English Bridle"gt36.31,
"Western Saddle"gt124.00, "Hunt Seat
Saddle"gt178.01, "Jumping Saddle"gt205.87,
"Leather Halter"gt24.56, )
printAA(priceList) print "You could print a
hash cell like this priceList'Western
Saddle'\n" priceList ( 1358gt28.92,
1359gt36.31, 1965gt124.00, 1966gt178.01,
1977gt205.87, 1357gt24.56, )
29Perl????-????-Lab6 (cont)
- Lab 6 hasharray.cgi (cont)
print "Or like this priceList1965
\n\n" printAA(priceList) sub printAA my
myPriceList _at__ foreach key
(keys(myPriceList)) print "The price of
item key is priceListkey\n" print
"\n\n"
30Perl????-????-Lab6 (cont)
31Perl????-????(cont)
??????????? scalarRef\scalarVar
arrayRef\_at_arrayVar hashRef\hashVar
funcRef\funcName
32Perl????-????(cont)
???????? print scalarRef print
"_at_arrayRef" print hashRef-gtkey
funcRef
33Perl????-????(cont)-Lab7
! /usr/bin/perl hashRef'a'gt'aa','b'gt'bb','c'
gt'cc' print "hashRef-gt'a'\thashRef-gt'b'\t
hashRef-gt'c'\n" my hashRef1('a'gt'aa','b'gt'
bb','c'gt'cc') print " hashRef1'a'\t
hashRef1'b'\t hashRef1'c'\n"
34Perl????-????
- ?????? (Conditional Control Statements)
- if (Expression) Code Segment
- if (Expression) Code Segment else Code
Segment - statement if (Expression)
- statement unless (Expression)
35Perl????-??????
- ?????? (Loop Control Statements)
for(i0 ilt10 i) Code Segment foreach i
(_at_array) Code Segment for i (0..10) Code
Segment while(ilt10) Code Segment do Code
Segment while(Expression)
36Perl????-????-Lab8
! /usr/bin/perl my _at_custom custom0'??' cus
tom1'??' custom2'??' for (i0
iltcustom i) if ( customi eq
'??') print "????\n"
cnt0 while (cntltcustom) print
"??customcnt\t" cnt print \n
??????????,????????????,?????????,
?????????????????????????,??????? foreach i
(_at_custom) print "??i\t"
37Perl????-???(Subroutines)
- Syntax sub NAME Code
- ????? NAME(para1, para2,...)
- ???? _at__
38Perl????-???(Subroutines)
39Perl????-I/O?????
- Syntax
- open(FILEHANDLE,"Expression")
- ??????? string ???????,
- ???????? filehead ????,????????,
- ???true, ????? undef. ??????? lt (?????????)
- ??????????
- ??????? gt ,?????????
- ???????? gtgt ,?????????
- ??????? (? gt , lt ) ???????????
- close(FILEHANDLE)
40Perl????-I/O?????-Lab9
! /usr/bin/perl FileName"gttest.txt" open(WFILE
, "FileName") print WFILE "123\n"
print WFILE "456\n" print WFILE
"789\n" close(WFILE) ??test.txt??FileName"ltte
st.txt" my _at_MyDept
open (FILE,"FileName") die ! _at_LINESltFILEgt
close(FILE) SIZE_at_LINES for
(i0iltSIZEi) MyDeptiLINESi
print "MyDepti"
41Perl????-????
42Perl????-Regular Expressions
- Regular Expression??????????????(pattern),????????
?(pattern-matching)??????????????!?,??????match
?not match? - Syntax string /regular expression/expression
modifier - ?sentence /Hello/
43Perl????- -Regular Expressions
Modifiers????????,??????????????
44Perl????- -Regular Expressions
Metacharacter?????????????,??????????
????(searching
pattern)?
45Perl????- -Regular Expressions
Pattern Quantifier????????????
46Perl????- -Regular Expressions
Character Patterns???sequence??match?????????
47Perl????-Regular Expressions-Exam.
- /abc/
- ????abc???
- /abc/
- ?????abc???
- /abc/
- ?????abc???
- /ab/
- ???a?b???,?????????(word)
48Perl????-Regular Expressions(cont)
- /ab2,4c/
- ??a????2-4?b,???c???,???/ab2,c/????????b
- /abc/
- ??a????0????b,???c???,??/ab0,c/
- /abc/
- ??a?????????b,???c???,??/ab1,c/
49Perl????-Regular Expressions(cont)
- /a.c/
- .????????,??new line??(\n)??
- /abc/
- ?????????????????
- /\d/
- ?????????,??/0-9/
- /\w/
- ?????????,??/a-zA-Z0-9_/
50Perl????-Regular Expressions(cont)
- /\s/
- ????white space???,??/ \t\r\n\f/
- /abc/
- ????abc???????
- /\/
- ?????????,????"\"?????Perl????????????????????
????????,?????\????? - /abc/i
- ??abc????
51Perl????-Regular Expressions(cont)
- /(\d)\.(\d)\.(\d)\.(\d)/
- ????IP???,??IP?????????1,2,3,4???????,????
?????? - ? if (x /(\d\.\d)\.\d\.\d/)
print ????" if (1 eq "140.115") -
- x s/\s//g????white space?????
- x s/( )( )/21/??""???????????
52Perl????-Regular Expressions(cont)
- x tr/this/that/?"this"???"that"
- x tr/a-z/A-Z/??????????????
- count x tr///??x????""
53Perl????-Regular Expressions-Lab10
! /usr/bin/perl url"http//ccy.dd.ncu.edu.tw80
80/cgi-bin/news.cgi" (host, port,
file)(urlmhttp//(/)0,1(\d)(\S))
print ?????url\n" print ????????host\n"
print "??????portport\n" print
??????file\n"
54Perl????-Regular Expressions-Lab10
- urlmm????????pattern?????,m???????????,
?????????pattern? - (/)match????,????/???????????1??
- 0,1(\d)match 0?1?,?????????nothing????????2
?,????,2????? - (\S)match???????,??????????????????3??
55Perl????-Regular Expressions-Lab10
- (\S)match???????,??????????????????3??
- ()()(host, port, file)(1, 2,
3)?host"my.machine.tw"port8080file"/cgi-
bin/test.pl"
56Perl????-??Perl??CGI??
HTTP headers(??) HTTP???????????client-server????
,client?server????????????,??HTTP
headers,????????????,????????????
57Perl????-??Perl??CGI??
??????HTTP server?????????,????HTTP
headers?????,???HTTP headers?????
58Perl????-??Perl??CGI??
?????Netscape Navigator 3.0?????request GET /
HTTP/1.0Connection Keep-AliveUser-Agent
Mozilla/3.0 (WinNT I)Host ind.ntou.edu.twAccep
t image/gif, image/x-xbitmap, image/jpeg, /
59Perl????-??Perl??CGI??
?ind?apache HTTP server?????
HTTP/1.1 200 OKDate Sat, 06 Sep 1997 033212
GMTServer Apache/1.2b11Last-Modified Mon, 16
Jun 1997 140310 GMTETag "b5496-ab0-33a5479e"C
ontent-Length 2736Accept-Ranges
bytesKeep-Alive timeout15, max100Connection
Keep-AliveContent-Type text/htmllthtmlgtltbody
bgcolor"ffffff"gtlttitlegtWelcome to NTOU CC
Email/WWW Serverlt/titlegt......lt/bodygtlt/htmlgt
60Perl????-??Perl??CGI??
- ?????????
- ???
- "Content-type text/html\n\n"
- ????????
61Perl????-??Perl??CGI??-Lab11
! /usr/bin/perl PrintHeader() print
"lthtmlgtltbodygt" PrintENV() print
"lt/htmlgtlt/bodygt" sub PrintHeader print
"Content-type text/html\n\n"
sub PrintENV my key foreach key (sort
keys ENV) print "key
ENVkeyltbrgt\n"
62Perl????-??Perl??CGI??-Lab11
63?????? ???
64?????????-????
65?????????-????
??News.cgi
??
ReadNews()
ReadPara()
SystemConfigForm()
??????
ReadDept()
AddUserForm()
??????
ReadUser()
??????
PostNewsForm()
66?????????(cont)
- CGI ? ? ? ?
- CGI ? Common Gateway Interface ? ? ?. ? ? ? ? ? ?
? ? WWW ? ? ?, ? ? ? ? ? ? ?? ? ? ? ? ? ? WWW ? ?
? ?, ? ? WWW ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ?? ?
? ? ?. ? ? ? ? ? ? ?, CGI ? ? ? WWW ? ? ? ? ? ? ?
? ? ? ? ?.
67?????????-????
68?????????-Client??data
??? ????
sub Login
print_top() print "ltcentergt" print
"ltform action'MyPara'CGIFileName'?temp_funct
ion' method'post'gt" ltpgt??ltinput
name'login_name' size'20'gtlt/pgt
ltpgt??ltinput name'login_password'
type'password' value size'20'gtlt/pgt"
print ltinput nameB1 typesubmit
value????gt print ltinput type'reset'
value'????'gt" print "lt/formgt" Print
lt/centergt"print_bottom()
69?????????-Server???data
_at_in split(//,in) foreach i (0 ..in)
ini tr// / Convert plus's
to spaces Split into key and value
splits on the first (key, val)
split(//,ini,2) Convert XX from hex
numbers to alphanumeric key
s/(..)/pack("C",hex(1))/ge val
s/(..)/pack("C",hex(1))/ge val
s/lt!--(.\n)--gt//g Kill SSI command
Associate key and value \0 is the multiple
separator if (defined(inkey))
inkey join("\0", inkey, val)
else inkey val return 1
ReadForm(FORM) Print FORMlogin_name Pri
nt FORMlogin_password sub ReadForm
local (in) _at__ if _at__ local (i, key,
val) Read in text if (ENV'REQUEST_METHOD
' eq "GET") in ENV'QUERY_STRING'
elsif (ENV'REQUEST_METHOD' eq "POST")
read(STDIN,in,ENV'CONTENT_LENGTH') if
(ENV'QUERY_STRING' //) in
join("", in, ENV'QUERY_STRING')
70??????-???
71??-??(exam.cgi)
- ???perl???????????????????CGI??