Perl Training - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Perl Training

Description:

The fibonacci number is computed by adding one less than the number, and two ... print ' HTML n BODY bgcolor='$arguments{ color'}' n' ... – PowerPoint PPT presentation

Number of Views:211
Avg rating:3.0/5.0
Slides: 27
Provided by: perlw
Category:
Tags: bgcolor | perl | training

less

Transcript and Presenter's Notes

Title: Perl Training


1
Perl Training
  • CGI and CGI.pm

http//perlwizard.org/perl/week6
2
Agenda
  • Homework Review
  • CGI Environmental Variables
  • Hashes
  • Query String
  • Getting more Information
  • Getting User Input from the Query String
  • CGI.pm examples
  • More CGI.pm functions
  • Homework!!!

3
Homework Problem 1
  • The fibonacci number is computed by adding one
    less than the number, and two less than the
    number. Create a program that will compute the
    fibonacci number of a number.

!/usr/local/bin/perl _at_fib (0,1) sub fib
my (n) _at__ unless defined (fibn)
fibn fibn-1 fibn-2 return
fibn
Main Program foreach num (1 ..
50) printf(The fib of 2.0f is 15.0f\n,
num, fib(num)
4
Bonus Homework Problem
  • Create a Perl program that makes your job easier,
    or helps you do your job more efficiently.
  • Come up and present what you did!!

5
CGI Environmental Variables
  • ENV
  • ENVVARNAME Environmental Variable named
    VARNAME
  • tmp ENVREMOTE_HOST Domain name of
    computer making request
  • A Couple of Others
  • GATEWAY_INTERFACE CGI/1.1
  • HOME /
  • HTTP_ACCEPT image/gif, image/jpeg, /
  • HTTP_HOST www.vservers.com
  • PATH_TRANSLATED /usr/local/apache/cgi-bin
  • QUERY_STRING eastern
  • REMOTE_ADDR 209.124.234.223
  • REMOTE_HOST ratbert.lightrealm.com
  • REQUEST_METHOD GET
  • SCRIPT_NAME /telltime.cgi
  • SERVER_NAME www.vservers.com
  • SERVER_PORT 80

6
Hashes (Quick Overview)
  • Hashes are just like arrays, but instead of using
    numbers as an index, they use strings (called
    keys).
  • myHash () Create Empty Hash
  • myHashapple Hello Using the key
    apple, assign the value Hello
  • myHashcharlie Goodbye Using the key
    charlie, assign Goodbye
  • tmp boy
  • tmp2 forever
  • myHashtmp tmp2 Using the key boy,
    assign forever
  • _at_AllKeys sort(keys(myHash))
  • foreach key (_at_AllKeys)
  • print key myHashkey\n Prints each
    key and value in order alphabetically
  • according to the key (apple, boy,
    charlie).

7
The Query String
  • lta href/cgi-bin/telltime.cgi?centralgtCentrallt/A
    gt
  • lta href/cgi-bin/telltime.cgi?easterngtEasternlt/A
    gt
  • theTime time()
  • desiredTimeZone ENVQUERY_STRING
  • if(desiredTimeZone eq central) theTime
    3600 Add one hour
  • else theTime 7200 Add two hours

8
Getting more information
Results After filling in the information, the
web browser sends the information to the
myscript.pl script. It sends the information in
a URL encoded string of the form
elementnameelementvalue. The spaces are turned
into for ease of use. The different
keyvalue pairs are seperated by . An example
QUERY_STRING could be FirstNameJasonLastName
NoblecolorBlue There are more things that are
done special (s and s have to be encoded)
to the URL string before it is sent. Most of
this is done automagically.
  • HTML
  • lthrgt
  • ltform methodpost action/cgi-bin/myscript.plgt
  • First Name ltinput nameFirstName size30gtltbrgt
  • Last Name ltinput nameLastName size30gtltbrgt
  • Favorite Color ltselect namecolorgt
  • ltoptiongtBluelt/optiongtltoptiongtRedlt/optiongt
  • lt/selectgtltbrgt
  • ltinput typesubmit valueSendgt
  • lt/formgt
  • lthrgt

9
Retrieving Info from the Query_String
  • if(ENVREQUEST_METHOD eq GET)
  • TheResp ENVQUERY_STRING
  • Variables are in a string format
  • else
  • read(STDIN, TheResp, ENVCONTENT_LENGTH)
  • Variables are given as standard input, of
    length ENVCONTENT_LENGTH

10
Everything you need for getting user input
  • sub parse_form_data
  • local (FORM_DATA) _at__
  • local (request_method, query_string,
    _at_key_value_pairs, key_value, key, value)
  • 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')
  • _at_key_value_pairs split (//,
    query_string)
  • foreach key_value (_at_key_value_pairs)
  • (key, value) split (//, key_value)
  • value tr// /
  • value s/(\dA-Fa-f\dA-Fa-f)/pack
    ("C", hex (1))/eg Replace special encoding
    with appropriate letter
  • key tr// / You could use
    sprintf instead
  • key s/(\dA-Fa-f\dA-Fa-f)/pack
    ("C", hex (1))/eg
  • if (defined(FORM_DATAkey))
  • FORM_DATAkey join ("\0",
    FORM_DATAkey, value) If its already
    defined, join it together with a null

11
Using parse_form_data function
  • parse_form_data (arguments)
  • print ltHTMLgt\nltBODY bgcolor\argumentscolor
    \gt\n
  • print Hello argumentsFirstName
    argumentsLastNameltBRgt\n
  • print Welcome to my home pageltBRgt\n
  • print lt/BODYgt\nlt/HTMLgt\n

12
CGI 101
  • The Simplest little CGI in the world
  • print Content-type text/html\n\n
  • print Hello world!\n

13
CGI.pm
  • CGI.pm (sometimes simply called CGI) is a Perl
    Module that has a lot of built in functionality
    that helps you build webpages.
  • Makes building large webpages faster
  • Cuts down on repetitive typing (print this, print
    that...)
  • It may have the functionality you need, so all
    you need to do is call a function, instead of
    figuring it out on your own

14
CGI.pm (Cont.)
  • !/usr/local/bin/perl
  • use CGI qw(standard)
  • print header(text/plain),
  • Nothing to it
  • After saving this to a file (/www/cgi-bin/plaintex
    t.pl), you can run this program by going to
    http//yourserver.com/cgi-bin/plaintext.pl with a
    web browser, or logging into the server itself
    and running /www/cgi-bin/plaintext.pl. By
    default it will ask you to enter arguments, just
    hit Cntrl-D when youre done inputting variables.
  • Output
  • Content-type text/plain
  • Nothing to it!

15
CGI.pm (Cont.)
  • Use the HERE document
  • !/usr/local/bin/perl
  • use CGI qw(standard)
  • header header(text/plain)
  • print ltltEOF
  • header
  • Ode to Perl
  • Perl is the best,
  • Why mess with the rest,
  • Ever wanted to do CGI?
  • It does it on the fly!!
  • EOF

16
Reading in from a file
  • !/usr/local/bin/perl
  • use CGI qw(standard)
  • FILE /usr/tmp/press_release.txt
  • open(FILE, FILE) die Cant open FILE
    !\n
  • print header(text/plain)
  • while(ltFILEgt)
  • s/DATE/20000915/g
  • print
  • close FILE

17
An HTML Page (via CGI.pm)
  • !/usr/local/bin/perl
  • use CGI qw(standard)
  • current_time localtime
  • print header, print Content-type
    text/html\n\n
  • start_html(A virtual clock), print
    ltHTMLgtltHEADgtltTITLEgtltBODYgt
  • A virtual Clocklt/TITLEgtlt/HEADgtlt/BODYgt
  • h1(A virtual clock), print ltH1gtA
    virtual clocklt/H1gt
  • The current time is current_time.,
    print the string
  • hr, print ltHRgt
  • end_html print lt/BODYgtlt/HTMLgt

18
Adding Images with CGI.pm
  • img(Attributes)
  • Attributes
  • align alt border height width
  • hspace ismap src lowsrc vspace
  • usemap
  • print header,
  • start_html,
  • img(-srcgtimages/perl.gif, -aligngtright),
  • end_html

19
Adding Links with CGI.pm
  • a(Attributes)
  • Attributes
  • href name onClick onMouseOver
  • target
  • print header,
  • start_html,
  • a(-hrefgt/perl/perl2.htm, Go to week 2),
  • end_html

20
Unordered Lists
  • !/usr/local/bin/perl
  • use CGI qw(standard)
  • print header,
  • start_html(Vegetables),
  • h1(Eat your Vegetables),
  • ol(
  • li(peas),
  • li(broccoli),
  • li(cabbage),
  • li(peppers,
  • ul(
  • li(red),
  • li(green)
  • )
  • ),
  • ),
  • hr,
  • end_html

Results Content-Type text/html lt!DOCTYPE HTML
PUBLIC "-//IETF//DTD HTML//EN"gt ltHTMLgt ltHEADgt
ltTITLEgtVegetableslt/TITLEgt lt/HEADgt
ltBODYgt ltH1gtEat your Vegetableslt/H1gt
ltOLgt ltLIgtpeaslt/LIgt
ltLIgtbroccolilt/LIgt ltLIgtcabbagelt/LIgt
ltLIgtpeppers ltULgt ltLIgtredlt/LIgt
ltLIgtgreenlt/LIgt lt/ULgt lt/LIgt lt/OLgt
ltHRgt lt/BODYgt lt/HTMLgt
21
Other CGI Functions
22
More CGI Functions
23
More information
  • http//stein.cshl.org/WWW/software/CGI/
  • Examples
  • http//www.wiley.com/compbooks/stein/source.html
  • http//www.wiley.com/compbooks/stein/
  • perldoc CGI

24
Homework Problem 1
  • Add a CGI frontend to your bonus program that you
    created last week

25
Bonus Homework Problem
  • Add another item of functionality to your bonus
    problem from last week.

26
Questions?
Write a Comment
User Comments (0)
About PowerShow.com