Perl - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Perl

Description:

BODY BGCOLOR=ORANGE TEXT=BROWN p Hello this is my page /p p My ... print ' BODY BGCOLOR=ORANGE TEXT=BROWN '; print ' H2 Hello, welcome to my page /H2 ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 39
Provided by: mxs05
Category:
Tags: bgcolor | perl

less

Transcript and Presenter's Notes

Title: Perl


1
Perl
2
Common Gateway Interface (CGI)
  • Set of protocols through which CGI scripts
    interact with
  • Web servers
  • clients (e.g., Web browsers).
  • These protocols often are used to generate
    dynamic Web content based on client input.

3
Common Gateway Interface (CGI)
  • It is "common" in the sense that
  • it is not specific to any particular operating
    system or
  • to any programming language
  • Yet, from the beginning, Perl has been the
    language of choice for CGI programming, because
    of its powerful text-processing capabilities

4
Protocol
  • A set of rules to establish, maintain and close
    communications between two entities
  • Networking protocols establish connections
    to/through a specific service port on the server
  • An IP address and port number combination is
    necessary
  • Port number implies a specific service

5
(No Transcript)
6
Simple GET transaction
  • Generated by typing a URL in your browser
  • http//www.utdallas.edu/rml021000/index.html
  • Which is decomposed into
  • www.utdallas.edu as the IP address equivalent
  • http as the protocol/type-of-service
  • The directory/file combination

7
HTTP request
  • The browser sends an HTTP request message to the
    server.
  • GET /rml021000/index.html HTTP/1.0
  • The word GET indicates that the client wishes to
    get a resource.

8
Server responds
  • The server responds indicating the HTTP version,
    followed by a numeric code and the status of the
    transaction.
  • HTTP/1.0 200 OK
  • HTTP/1.0 404 Not found

9
HTTP header
  • If the server sends an text document the HTTP
    header is Content-type
    text/txt
  • If the server sends an HTML document the HTTP
    header is Content-type
    text/html
  • If the server sends an image file the HTTP header
    is
    Content-type image/gif

10
Client-side browser
  • The connection is terminated when the transfer of
    the resource is complete.
  • The client-side browser interprets the HTML it
    receives and displays the results.

11
Static Web pages
  • lthtmlgt
  • lttitlegt My portfolio lt/titlegt
  • ltBODY BGCOLORORANGE TEXTBROWNgt
  • ltpgt Hello this is my page lt/pgt
  • ltpgt My name is Rafael lt/pgt
  • ltpgt the date is Jul 15, 2004lt/pgt
  • ltpgt This is my portfolio lt/pgt
  • ltulgt
  • ltligt ltA HREFproject1.htmlgtproject 1lt/Agt
  • ltligt ltA HREFproject2.htmlgtproject 2 lt/Agt
  • lt/ulgt
  • lt/bodygt
  • lt/htmlgt

12
Dynamic web pages
  • CGI scripts (sh, perl, c, c...) helps us in the
    creation of dynamic web pages.
  • CGI allows us to redirect the output of a script
    to the server, which can send the output to a
    client's browser.

13
ex1.pl localtime()
  • !/usr/bin/perl
  • first example
  • print "Content-type text/html\n\n"
  • print "ltHTMLgt"
  • print "ltHEADltTITLEgtDatelt/TITLEgtlt/HEADgt"
  • print "ltBODYgt"
  • print scalar (localtime() )
  • print "lt/BODYgtlt/HTMLgt"

14
  • When a Perl program is executed as a CGI script,
    the standard output is redirected to the Web
    browser client.
  • To execute the program, you must place the Perl
    file in your Web server's cgi-bin directory.
  • In apache this is /public_html/cgi-bin

15
Executing the perl script
  • You can execute the program by typing in your
    browser's Address or Location field the following
    line
  • http//www.utdallas.edu/cgi-bin/cgiwrap/userid/per
    lscript
  • http//www.utdallas.edu/cgi-bin/cgiwrap/rml021000/
    ex1.pl
  • or
  • http//www.utdallas.edu/userid/cgi-bin/perlscript
  • http//www.utdallas.edu/rml021000/cgi-bin/ex1.pl

16
(No Transcript)
17
ex2.pl
  • !/usr/bin/perl
  • print "Content-type text/html\n\n"
  • print "ltHTMLgt"
  • print "ltHEADgt
  • print "ltTITLEgtMY PAGElt/TITLEgt"
  • print "lt/HEADgt"
  • print "ltBODY BGCOLORORANGE TEXTBROWNgt"
  • print "ltH2gtHello, welcome to my pagelt/H2gt"
  • print "ltPgt"
  • print "ltFONT FACEHELVETICA COLORWHITEgtMy name
    is Rafaellt/FONTgt"
  • print "lt/BODYgtlt/HTMLgt"

18
Use of CGI.pm
  • In the following examples, we will introduce the
    use of the CGI.pm module to generate dynamic
    HTML
  • CGI.pm contains a set of functions for
    simplifying the creation of CGI scripts
  • To use them we include the statement
  • use CGI
  • in our programs

19
ex3.pl
  • !/usr/bin/perl
  • use CGI
  • my query CGI-gtnew()
  • print query-gtheader("text/html"),
  • query-gtstart_html(-title gt"MY PAGE",
  • -bgcolor gt ORANGE",
  • -text gt BROWN"),
  • query-gth2("Hello, welcome to my page"),
  • query-gtp( "My name is Rafael" ),
  • query-gtend_html

20
ex3a.pl
  • !/usr/bin/perl
  • use CGI
  • my query CGI-gtnew()
  • print query-gtheader("text/html")
  • print ltlt END_HERE
  • lthtmlgtltHEADgtltTITLEgtMy PAGElt/TITLEgtlt/HEADgt
  • ltBODY BGCOLORORANGE TEXTBROWNgt
  • ltH2gtHello, Welcome to my pagelt/H2gt
  • ltPgt My name is Rafaellt/Pgt
  • lt/BODYgt
  • lt/HTMLgt
  • END_HERE

21
import tag standard
  • It denotes the standard function set within
    CGI.pm
  • qw operator to make it easy to add additional
    tags or functions later
  • Functions (such as header) can be called directly
    (without prefix)

22
ex4.pl
  • !/usr/bin/perl
  • use CGI qw(standard)
  • print header("text/html"),
  • start_html(-title gt"MY PAGE",
  • -bgcolor gt ORANGE",
  • -text gt BROWN"),
  • h2("Hello, welcome to my page"),
  • p( "My name is Rafael" ),
  • end_html

23
HTML forms to send input
  • HTML provides the ability to include forms to
    input information to be sent to a CGI script
  • The ltformgt tag takes two attributes
  • action, The call to a CGI script to process the
    form data
  • method, which normally is either GET or POST

24
ex5.html
  • ltHTMLgt
  • lttitlegtCGI TESTlt/titlegt
  • ltBODYgt
  • ltpgtWrite your name
  • ltform method "get" action"http//www.utdallas.e
    du/cgi-bin/cgiwrap/mxs015000/ex5.pl"gt
  • ltinput type"text" name"word"gt
  • ltinput type"submit" value"submit word"gt
  • lt/formgt
  • lt/BODYgt
  • lt/HTMLgt

25
ENV and QUERY_STRING
  • The ENV hash is a built-in hashtable in Perl
    that contains the names and values of all the
    environment variables
  • Using GET with an HTML form passes data to the
    CGI script through an environment variable
    (QUERY_STRING)

26
ex5.pl
  • !/usr/local/bin/perl
  • use warnings
  • use CGI qw(standard )
  • our ( name, value ) split( '',
    ENV'QUERY_STRING' )
  • print header(),
  • start_html(-title gt'Results')
  • if ( name eq 'word' )
  • print p ('Your word is ', b(value))
  • print end_html

27
split
  • The environment variable QUERY_STRING provides
    data to my CGI script by appending it to a URL in
    a GET request, following a question mark (?)
  • http//www.utdallas.edu/rml021000/cgi-bin/ex5.pl
    ?wordTechnology
  • We call split to break the pair at the equals
    sign into a name and a value

28
ex6.pl
  • !/usr/bin/perl
  • use warnings
  • use CGI qw( standard )
  • our ( name, value ) split( '', ENV
    'QUERY_STRING' )
  • print name
  • print header(),
  • start_html(-title gt'Using GET with
    forms')
  • print p( 'Write your name ' )
  • print 'ltform method "get" action"http//www.utd
    allas.edu/cgi-bin/cgiwrap/mxs015000/ex6.pl"gt',
  • 'ltinput type"text" name"word"gt',
  • 'ltinput type"submit" value"submit
    word"gt',
  • 'lt/formgt'
  • if ( name eq 'word' )
  • print p ('Your word is ', b(value))
  • print end_html

29
GET vs. POST
  • The GET method passes data to the CGI script via
    the QUERY_STRING variable
  • A GET request sends form contents as part of the
    URL, a POST request posts form contents to the
    end of the HTTP request
  • A GET request can be cached by the browser on the
    client, a POST cant

30
POST method
  • The POST method sends data to the CGI script via
    standard input (still in name-value pairs
    connected by equals signs)
  • Function read is used to read from standard input
    CONTENT_LENGTH characters and store them in the
    data variable
  • data is the source for the split, instead of
    QUERY_STRING

31
ex7.pl
  • !/usr/bin/perl
  • use warnings
  • use strict
  • use CGI qw( standard )
  • our ( data, name, value )
  • read( STDIN, data, ENV 'CONTENT_LENGTH' )
  • ( name, value ) split( '', data )
  • print header(), start_html( 'Using POST with
    forms' )
  • print p( 'Enter one of your favorite words here
    ' )
  • print 'ltform method "POST" action ex7.pl"gt'
  • print 'ltinput type "text" name "word"gt'
  • print 'ltinput type "submit" value "Submit
    word"gt'
  • print 'lt/formgt'
  • if ( name eq 'word' )
  • print p( 'Your word is ', b( value ) )
  • print end_html()

32
Using CGI.pm shortcuts
  • Function param takes as argument the name of the
    form field and returns its value
  • No read, split or URL decoding is required and
    param works with GET or POST

33
ex8.pl
  • !/usr/bin/perl
  • use warnings
  • use CGI qw( standard )
  • my word param( "word" )
  • print header(), start_html( 'Using CGI.pm with
    forms' )
  • print p( 'Enter one of your favorite words here
    ' )
  • print start_form(), textfield( "word" )
  • print submit( "Submit word" ), end_form()
  • print p( 'Your word is ', b( word ) ) if word
  • print end_html()

34
Shortcut functions
  • start_form generates the ltformgt tag and defaults
    to the POST method. If no action is given
    explicitly, the script calls itself
  • textfield generates HTML equivalent to ltinput
    type text name wordgt
  • submit creates a submit button with the argument
    as label

35
cgi-env.pl
  • !/usr/bin/perl -wT
  • use strict
  • my env_info (
  • SERVER_SOFTWARE gt "the server software",
  • SERVER_NAME gt "the server hostname
    or IP address",
  • GATEWAY_INTERFACE gt "the CGI specification
    revision",
  • SERVER_PROTOCOL gt "the server protocol
    name",
  • SERVER_PORT gt "the port number for
    the server",
  • REQUEST_METHOD gt "the HTTP request
    method",
  • PATH_INFO gt "the extra path info",
  • PATH_TRANSLATED gt "the extra path info
    translated",
  • DOCUMENT_ROOT gt "the server document
    root directory",
  • SCRIPT_NAME gt "the script name",
  • QUERY_STRING gt "the query string",
  • REMOTE_HOST gt "the hostname of the
    client",

36
cgi-env.pl
  • REMOTE_ADDR gt "the IP address of the
    client",
  • AUTH_TYPE gt "the authentication
    method",
  • REMOTE_USER gt "the authenticated
    username",
  • REMOTE_IDENT gt "the remote user is
    (RFC 931) ",
  • CONTENT_TYPE gt "the media type of the
    data",
  • CONTENT_LENGTH gt "the length of the
    request body",
  • HTTP_ACCEPT gt "the media types the
    client accepts",
  • HTTP_USER_AGENT gt "the browser the
    client is using",
  • HTTP_REFERER gt "the URL of the
    referring page",
  • HTTP_COOKIE gt "the cookie(s) the
    client sent"
  • )

37
cgi-env.pl
  • print "Content-type text/html\n\n"
  • print ltltEND_OF_HEADING
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtA List of Environment
    Variableslt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • ltH1gtCGI Environment Variableslt/H1gt
  • ltTABLE BORDER1gt
  • ltTRgt
  • ltTHgtVariable Namelt/THgt
  • ltTHgtDescriptionlt/THgt
  • ltTHgtValuelt/THgt
  • lt/TRgt
  • END_OF_HEADING

38
cgi-env.pl
  • my name
  • Add additional variables defined by web server
    or browser
  • foreach name ( keys ENV )
  • env_infoname "an extra variable
    provided by this server"
  • unless exists env_infoname
  • foreach name ( sort keys env_info )
  • my info env_infoname
  • my value ENVname "ltIgtNot
    Definedlt/Igt"
  • print "ltTRgtltTDgtltBgtnamelt/Bgtlt/TDgtltTDgtinfolt/TDgt
    ltTDgtvaluelt/TDgtlt/TRgt\n"
  • print "lt/TABLEgt\n"
  • print "lt/BODYgtlt/HTMLgt\n"
Write a Comment
User Comments (0)
About PowerShow.com