Programming On The Web (Csc309F) : Website: Office-Hour: Friday 12:00-1:00 (Sf2110) Email: Wael@Cs - Toronto.Edu
Programming On The Web (Csc309F) : Website: Office-Hour: Friday 12:00-1:00 (Sf2110) Email: Wael@Cs - Toronto.Edu
Programming On The Web (Csc309F) : Website: Office-Hour: Friday 12:00-1:00 (Sf2110) Email: Wael@Cs - Toronto.Edu
Introduction to Perl
History of Perl!
Initially developed by Larry Wall (English major student then!) in 1987. Modules continuously added since version 2.0 Latest version is Perl 5.6.1 Ported to 83 platforms. Most popular scripting language for server-side web development.
helloworld.pl
Source: print ( Hello to Perl World! ); print Hello to Perl World!; # yes, no brackets! Run it: C:\> perl helloworld.pl Hello to Perl World!Hello to Perl World!
Perl Strings
Strings:
Difference between using the single quote (') and the double quote (") to delimit strings: $Client = 'John Luu'; @Items = ( "Chocolate", "Biscuits", "Milk"); $Total = 60; $Transaction = "$Client is buying @Items for \$ $Total"; print( $Transaction ); # Outputs John Luu is buy ing Chocolate Biscuits Milk for $ 60 Concatenating Strings: $FirstName = John'; $LastName = Black; $FullName = $First Name . $LastName ; # FullName is now John Black String operators: # lt: less than , le: less or equal , eq: equal , ge: greater or equal , ne: not equal , eq: equal $Val1 = 1.1; $Val2 = 1.0; $Val3 = bbb; $Val4 = aaa; if( ($Val1 gt $Val2) && ($Val3 lt $Val4 ) ) Pattern matching:
# =~:
does match , !~: does not match $CatString = tomcat if ( $CatString =~ /cat/ ) . # Right-hand-side of these operators must always be a regular expression
Looping:
While Loops: while( $n <= 10 ) { printf( "%d squared is %d\n", $n, $n*$n ); $n = $n + 1; } For-each Loops: @Digits = (0,1,2,3,4,5,6,7,8,9); foreach $n (@Digits) { printf( "%d squared is %d\n", $n, $n*$n ); } For Loops: for( $n = 1; $n <= 10; ++$n ) { printf( "%d squared is %d\n", $n, $n*$n ); }
Functions
e.g.: sub func1{ my($param1,$param2) = @_; print "Inside func"; $param1 * $param2; } print(" Before calling func1"); $result = func1( 10,20 ); print(" After calling func1"); print(" result : $result"); # parameters are passed in an array called @_ # same as return($param1 * $param2 );
5
# prints result: 200
Reading input:
Standard Output: $NewLine = <STDIN>; $ProgramArguments = <ARGV>; Files: open( IN, <test.dat" ); $Line = <IN>; close( IN ); # Associates file handle with file # Reads the first line # Reads one complete line from standard input # Refer to arguments on the command line. # e.g. perl prog param1 param2 param3
Require Statement
Causes the Perl interpreter to execute the code in the require d file! # foo.pl perl code more perl code. 1; #----------------------------------------------# Poo.pl require foo.pl; perl code # you can call functions in foo.pl
use strict;
Forces Perl interpreter to be strict in using types. Will save you a lot of time !
Perl - CGI
Handling Query String
# dispatcher.pl use strict; # Retrieving query string $request_method = $ENV{REQUEST_METHOD}; if( $request_method eq GET ){ $query_string = $ENV{QUERY_STRING}; } elsif ( $request_method eq POST ){ read( STDIN , $query_string, $ENV{CONTENT_LENGTH} ); } else{ print(Invalid Request Method !); exit( 1 ); } # Extracting name -value pairs from query string (param1=value1¶m2=value2&..) @name_value_paris = split( /&/, $query_string ); foreach $name_value (@name_value_pairs){ ($name, $value ) = split( /=/, $name_value ); #more Perl code.. }
Generating HTML
print Content-type: text/html\n\n; print <html><head> \n; print <title> foo title </title></head> \n; print <body> .. Print </body></html>
10
Perl - cgi.pm
Using cgi.pm
use strict; use CGI qw(:standard); print ( header( ) ); print( start_html( Anna Web Page ) ); print( table( .) ); print( a() ); print( radio_group(..) ); print( end_html( ) );
11
Assignment 3
What is it about?
Client(Web-Browser) Server(HTTP Server)
SMTP
POP3
SMTP Server
POP3 Server
12