Fundamentals of Web Programming Lecture 9 ServerSide Scripting
Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 1
Today’s Topics • Review server scripting • CGI support for Perl 5: CGI. pm • Input/output: file handles • Example: dynamic file listing • URL support for Perl 5: LWP. pm • Example: fetch a stock quote 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 2
Server Scripting • Parse Environment Information • Prepare Response Content • Print Return Header Content-type: <MIME type>, e. g. Content-type: text/html • Print Return Content 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 3
CGI Support in Perl 5 • Perl Module: CGI. pm use CGI; • Subroutine to fetch environment &CGI: : Read. Parse(); • Environment variables and form element values stored in hash $lastname = $in{‘last_name’}; • Support for interactive debugging 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 4
Quick Example #!/usr/bin/perl use CGI; &CGI: : Read. Parse(); $name = $in{‘name’}; print <<End. Text Content-type: text/plain Name: $name End. Text ; exit 0; 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 5
Input/Output: File Handles • Including data in your scripts is okay if: – there’s not a lot of it – it doesn’t change very often • For larger data stores, it’s better to use separate data files – can grow over time – can be provided by or processed by other programs 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 6
Input/Output: File Handles • In Perl, file input and output are accomplished via filehandles • Correspond to input / output streams in other languages • Basic sequence: – open a file handle for input or output – read/write from/to the file handle – close the file handle 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 7
Input/Output: File Handles • Input: open(IN, ”<guestbook. txt”); # read lines from IN close(IN); • Output: open(OUT, ”>guestbook. txt”); # write lines to OUT close(OUT); 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 8
Input/Output: File Handles • Append: open(OUT, ”>>guestbook. txt”); # append lines to OUT close(OUT); • Pipe from Command: open(LS, ”ls |”); # read lines from LS command close(LS); 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 9
Input/Output: File Handles • Pipe to Command: open(SORT, ”| sort > gbsort. txt”); # sort lines and save in gbsort close(SORT); 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 10
Reading Lines In • Once open, the file handle is accessed using <> notation and the assignment operator = • How much is read depends on the type of the LHS expression • Array context: @lines = <IN>; • Scalar context: $line = <IN>; 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 11
Input Examples • Array context open(IN, ”<gbook. txt”); @guests = <IN>; close(IN); foreach $guest (@guests) { print $guest; } 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 12
Input Examples • Scalar context open(IN, ”<gbook. txt”) while ($guest = <IN>) { print $guest; } close(IN); 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 13
Printing Output • Once open, the file handle is accessed using <> notation and the print or printf command • E. g. , print OUT “$namen”; 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 14
Output Examples open(OUT, ”>gbook. txt”); print OUT “Start new file. n”; close(OUT); open(OUT, ”>>gbook. txt”); print OUT “Add to old file. n”; close(OUT); 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 15
Using Pipes • The file handle is opened by calling another program and piping the results as though they were an input file • E. g. , open(LS, ”ls |”); • Example script: listall. cgi 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 16
Accessing the Web • Fetching URLs: LWP. pm use LWP: : Simple; $url = “http: //www. cnn. com”; $result = get($url); • Example: quote. cgi 20 -753: Fundamentals of Web Programming Lecture 9: Server-Side Scripting I 17
- Slides: 17