CGI programming using Perl

15
CGI programming using Perl Sam Department of Computer Science and Engineering The Chinese University of Hong Kong

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: - PowerPoint PPT Presentation

Transcript of CGI programming using Perl

Page 1: CGI programming using Perl

CGI programming using Perl

Sam Department of Computer Science and Engineering

The Chinese University of Hong Kong

Page 2: CGI programming using Perl

Content

Brief Introduction of Perl

Useful Techniques

CGI Example

Favorite Links to Visit

Page 3: CGI programming using Perl

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

Page 4: CGI programming using Perl

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."

Page 5: CGI programming using Perl

Key language features and examplesMore Example:

$_='My email address is <[email protected]>.';

/(<robert\@netcat.co.uk>)/i;

print "Found it ! $1\n";

Output:

Found it ! <[email protected]>

Page 6: CGI programming using Perl

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

Page 7: CGI programming using Perl

Key language features and examples

3. File OperationsExample:

$stuff="c:\scripts\stuff.txt";

open STUFF, $stuff;

while (<STUFF>) {

print "Line number $. is : $_";

}

Page 8: CGI programming using Perl

Key language features and examples

More Example:

@ARGV="c:/scripts/out.txt";

$^I=".bk";

while (<>) {

tr/A-Z/a-z/;

print;

}

Page 9: CGI programming using Perl

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

Page 10: CGI programming using Perl

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";

Page 11: CGI programming using Perl

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

Page 12: CGI programming using Perl

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";...

Page 13: CGI programming using Perl

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

Page 14: CGI programming using Perl

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/

Page 15: CGI programming using Perl

The End