Post on 13-Jan-2016
description
CGI programming using Perl
Sam Department of Computer Science and Engineering
The Chinese University of Hong Kong
Content
Brief Introduction of Perl
Useful Techniques
CGI Example
Favorite Links to Visit
Perl
Perl is a high-level programming language Advantages:
rapid development (interpreted language)
portable across many platforms (UNIX, Windows, etc)
faster execution than shell scripts
free and huge library of free modules and scripts
Key language features and examples
1. Basic Regular Expressions (regex)
Perl's regex often look like this:
$name=~/piper/
That is saying "If 'piper' is inside $name, then True."
Key language features and examplesMore Example:
$_='My email address is <Robert@NetCat.co.uk>.';
/(<robert\@netcat.co.uk>)/i;
print "Found it ! $1\n";
Output:
Found it ! <Robert@NetCat.co.uk>
Key language features and examples2. SplittingExample:
$_='Piper:PA-28:Archer:OO-ROB:Antwerp';@details=split /:/, $_, 3;foreach (@details) { print "$_\n"; }
Output:
Piper
PA-28
Archer:OO-ROB:Antwerp
Key language features and examples
3. File OperationsExample:
$stuff="c:\scripts\stuff.txt";
open STUFF, $stuff;
while (<STUFF>) {
print "Line number $. is : $_";
}
Key language features and examples
More Example:
@ARGV="c:/scripts/out.txt";
$^I=".bk";
while (<>) {
tr/A-Z/a-z/;
print;
}
Key language features and examples
3. Associative Arrays
@myarray Vs %myhash
Index Value Key Value
0 Spain ES Spain
1 Belgium BE Belgium
2 Germany DE Germany
Key language features and examples
More Example:
%countries=('NL','The Netherlands','BE','Belgium','DE'
,'Germany','MC','Monaco','ES','Spain');
print "Enter the country code:";
chop ($find=<STDIN>);
$find=~tr/a-z/A-Z/;
print "$countries{$find} has the code $find\n";
CGI Example
Network
1. Request server/http://pc89075.cse.cuhk.edu/
vc_index.html
Web Server(Web Professional)
Oracle 8
Database
Documents
Disk
Scripts
HTML formvc_ index.html
.... ....
Querytemplate+Code
login.pl
Learner/browser
2. Web Page vc_index.html
3. Action="login.pl "
/input login data4. Result Page
formrequestedby learner
2
passing CGI strings toscript and activate it3
query the databaseand get the results
change the results to HTML format
4
HTML and Perl files<html><head>... </head> <body>...
<form method="POST" action="/cgi-vc/login_2.pl"><input type="password" name="password">... </form></html>
read (STDIN, $login_raw, $ENV{'CONTENT_LENGTH'});%logindata=split (/[&=]/, $login_raw);foreach (keys %logindata){ $logindata{$_}=~s/%([\dA-Fa-f]{2})/chr(hex $1)/ge; $logindata{$_}=~s/\+/ /g;} $logid=$logindata{'login'};$passwd=$logindata{'password'};...
print "Content-type: text/html\n\n";print "<html><body>\n";...
Favorite Links to Visit
Perl tutorial:Robert's Perl Tutorial
http://www.netcat.co.uk/rob/perl/win32perltut.html
Ten Perl Myths
http://www.perl.com/pub
ActiveSate Tool Corp.
http://www.activestate.com
Favorite Links to Visit
Perl Editor:Pete's Place
http://www.petes-place.com/
Resources:CPAN Module documentation
http://theoryx5.uwinnipeq.ca/CPAN/
Matt's Script Archive
http://www.worldwidemart.com/scripts/
The End