Fundamentals of Web Programming - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Fundamentals of Web Programming

Description:

open(IN,' guestbook.txt'); # read lines from IN. close(IN); Output: open(OUT,' guestbook.txt'); # write lines to OUT. close(OUT); 9. 20-753: Fundamentals of ... – PowerPoint PPT presentation

Number of Views:1006
Avg rating:3.0/5.0
Slides: 18
Provided by: erich178
Category:

less

Transcript and Presenter's Notes

Title: Fundamentals of Web Programming


1
Fundamentals ofWeb Programming
  • Lecture 9 Server-Side Scripting I

2
Todays 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

3
Server Scripting
  • Parse Environment Information
  • Prepare Response Content
  • Print Return HeaderContent-type ltMIME typegt,
    e.g.Content-type text/html
  • Print Return Content

4
CGI Support in Perl 5
  • Perl Module CGI.pmuse CGI
  • Subroutine to fetch environmentCGIReadParse()
  • Environment variables and form element values
    stored in hashlastname inlast_name
  • Support for interactive debugging

5
Quick Example
  • !/usr/bin/perluse CGICGIReadParse()na
    me innameprint ltltEndTextContent-type
    text/plainName nameEndTextexit 0

6
Input/Output File Handles
  • Including data in your scripts is okay if
  • theres not a lot of it
  • it doesnt change very often
  • For larger data stores, its better to use
    separate data files
  • can grow over time
  • can be provided by or processed by other programs

7
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

8
Input/Output File Handles
  • Inputopen(IN,ltguestbook.txt) read lines
    from INclose(IN)
  • Outputopen(OUT,gtguestbook.txt) write lines
    to OUTclose(OUT)

9
Input/Output File Handles
  • Appendopen(OUT,gtgtguestbook.txt) append
    lines to OUTclose(OUT)
  • Pipe from Commandopen(LS,ls ) read lines
    from LS commandclose(LS)

10
Input/Output File Handles
  • Pipe to Commandopen(SORT, sort gt
    gbsort.txt) sort lines and save in
    gbsortclose(SORT)

11
Reading Lines In
  • Once open, the file handle is accessed using ltgt
    notation and the assignment operator
  • How much is read depends on the type of the LHS
    expression
  • Array context _at_lines ltINgt
  • Scalar context line ltINgt

12
Input Examples
  • Array contextopen(IN,ltgbook.txt)_at_guests
    ltINgtclose(IN)foreach guest (_at_guests)
    print guest

13
Input Examples
  • Scalar contextopen(IN,ltgbook.txt)while
    (guest ltINgt) print guestclose(IN)

14
Printing Output
  • Once open, the file handle is accessed using ltgt
    notation and the print or printf command
  • E.g., print OUT name\n

15
Output Examples
  • open(OUT,gtgbook.txt)print OUT Start new
    file.\nclose(OUT)open(OUT,gtgtgbook.txt)pr
    int OUT Add to old file.\nclose(OUT)

16
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

17
Accessing the Web
  • Fetching URLs LWP.pmuse LWPSimpleurl
    http//www.cnn.comresult get(url)
  • Example quote.cgi
Write a Comment
User Comments (0)
About PowerShow.com