150 likes | 364 Views
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:
E N D
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 examples More 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 examples 2. Splitting Example: $_='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 Operations Example: $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 ValueKey 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";
HTML and Perl files 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";... <html><head>... </head> <body>... <form method="POST" action="/cgi-vc/login_2.pl"><input type="password" name="password">... </form></html>
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/