Web Technologies and What is Perl & What is it for? Applications • Perl is an acronym for Practical Extraction and Report language. Winter 2001 • It is currently the most used language for CGI- CMPUT 499: Perl, Cookies and other based Web applications. • Perl is powerful and flexible like a high-level Dr. Osmar R. Zaïane programming language. Perl 5 is object-oriented. • Perl is scripting language that combines features from awk and sed, yet as powerful as C. University of Alberta Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web Technologies and Applications University of Alberta 1 Web Technologies and Applications University of Alberta 2 Course Content Objectives of Lecture 8 • Introduction • Databases & WWW Perl, Cookies and Other , Cookies and Other Perl • Internet and WWW • SGML / XML • Protocols • Managing servers • Introduce Perl language for CGI • HTML and beyond • Search Engines development (This is not a Perl course) • Animation & WWW • Web Mining • Learn about magic cookies and what we can • Java Script • CORBA • Dynamic Pages • Security Issues use them for. • Perl Intro. • Selected Topics • See some examples with cookies in Perl and • Java Applets • Projects Javascript. Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 3 3 4 Web Technologies and Applications University of Alberta Web Technologies and Applications University of Alberta
Perl the scripting Language Outline of Lecture 8 • Perl is commonly used to write scripts. • What is Perl? • It is interpreted and is available on most • Variables and Expressions platforms (free software foundation and GNU ) • Control Structures • It suffices to write a script in a text file, make • File Input and Output it executable and run it. • Pattern matching • Initially designed to monitor software projects • A CGI example with Perl and generate reports, Perl gained popularity • Cookies and example with Perl thanks to the Internet & WWW . #!/usr/local/bin/perl • Cookie example with JavaScript $input=<STDIN>; • Example print “$input”; Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web Technologies and Applications University of Alberta 5 Web Technologies and Applications University of Alberta 6 Perl Capabilities Programming and Debugging • Perl programs are written in any text editor • Perl has rich and easy-to-use built-in text • Start the first line with #!/usr/local/bin/perl processing capabilities. • You can also run a perl expression in a • Perl is very flexible when it comes to file text command line with “perl ….” processing and pattern matching. • Debugging is tricky. There are debuggers • There are many intrinsic functions for but they are not as good as C/C++ and Java manipulating strings debuggers • Expressions are simple and concise • Use “perl –w” or try with “perl –c” • There are many ways to write the same thing in • Use print statements in your script to trace. Perl. Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 7 8 Web Technologies and Applications University of Alberta Web Technologies and Applications University of Alberta
Outline of Lecture 8 Scalar Variables • What is Perl? • Scalar variables start with $. Ex. $myVariable • Variables and Expressions • A scalar variable can contain: • Control Structures – a string $myVariable=“this is a string”; • File Input and Output – a integer number $myVariable=42; • Pattern matching – a floating-point number $myVariable=49.33; • A CGI example with Perl • A string with “string” is evaluated while a • Cookies and example with Perl string with ‘string’ is not evaluated: • Cookie example with JavaScript – $myString = “this is your total: $total”; Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web Technologies and Applications University of Alberta 9 Web Technologies and Applications University of Alberta 10 Arrays and Hash Outline of Lecture 8 • Arrays in Perl are indexed from 0 to n-1 • What is Perl? • An array is prefixed with @: • Variables and Expressions – @myArray = (‘bill’, ‘john’, ‘sue’, ‘Amelia’); • Control Structures • An element of an array is prefixed with $ • File Input and Output – $myArray[3] = ‘alpha’; $myVar=$myArray[0]; • Pattern matching • $#myArray is the last index of @myArray • A CGI example with Perl • Hash or associative array uses keys to • Cookies and example with Perl reference elements %myHash • Cookie example with JavaScript – $myHash{‘billy’} = 42; $myHash{$foo}=“abc”; Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 11 12 Web Technologies and Applications University of Alberta Web Technologies and Applications University of Alberta
Conditionals Loops • while (condition) {statements} • if (condition) {statements} • until (condition) {statements} – if ($number) {print “the number is not zero!”;} • do {statements} while (condition); • if (condition) {statements} else {statements} • do {statements} until (condition); • if (condition) {statements } • for (init;condition;increment) {statements} elsif (condition) {statements} • foreach (list) {statement} else {statements} – foreach $line (@myArray) {print “$line\n”;} • Variable = (condition)? expression1 : expression2; • next, last, redo Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web Technologies and Applications University of Alberta 13 Web Technologies and Applications University of Alberta 14 Outline of Lecture 8 Opening a File • open(variable, filename) • What is Perl? – open(MYFILE, “/usr/me/myfile.txt”); • Variables and Expressions • Open for writing • Control Structures – open(MYFILE, “>/usr/me/myfile.txt”); • File Input and Output • Open for reading – open(MYFILE, “</usr/me/myfile.txt”); • Pattern matching • Open for appending • A CGI example with Perl – open(MYFILE, “>>/usr/me/myfile.txt”); • Cookies and example with Perl • Closing a file with close(MYFILE) • Cookie example with JavaScript Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 15 16 Web Technologies and Applications University of Alberta Web Technologies and Applications University of Alberta
Determining the status of a file Reading/Writing to/from a File • if (-e “file”) # checks if file exists • $line=<MYFILE> • if (-d “file”) # checks if file is directory • @lines=<MYFILE> • if (-f “file”) #checks if file is ordinary file • while ($line=<MYFILE>) { • if (-l “file”) #checks if file is symbolic link chop $line; • if (-r “file”) #checks if file is readable print “line $i:[$line]\n”; • if (-w “file”) #checks if file is writable } • if (-x “file”) # checks if file is executable • print MYFILE “this will go in the file as a line\n”; • … Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 Web Technologies and Applications University of Alberta 17 Web Technologies and Applications University of Alberta 18 Outline of Lecture 8 The power of Perl • What is Perl? • One of the most powerful and attractive • Variables and Expressions capabilitites of Perl is the use of regular • Control Structures expressions for pattern matching. • File Input and Output • Allows large amount of text to be searched • Pattern matching with relatively simple expressions. • A CGI example with Perl • if ($myString eq “hello”) # equality operator • Cookies and example with Perl • if ($myString =~ /hello/) # matching operator • Cookie example with JavaScript Dr. Osmar R. Zaïane, 2001 Dr. Osmar R. Zaïane, 2001 19 20 Web Technologies and Applications University of Alberta Web Technologies and Applications University of Alberta
Recommend
More recommend