PERL http:www'cs'ualberta'castroulia410 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

PERL http:www'cs'ualberta'castroulia410

Description:

chomp removes ALL new line characters at the end of the string and returns the ... round to two decimals 1.67 and no decimals 3 ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 18
Provided by: elenist
Category:

less

Transcript and Presenter's Notes

Title: PERL http:www'cs'ualberta'castroulia410


1
PERL http//www.cs.ualberta.ca/stroulia/410
  • Eleni Stroulia
  • Department of Computing Science
  • University of Alberta

2
Practical Extraction and Report Language
  • Developed in 1980's by Larry Wall
  • He created it to help with text parsing tasks on
    UNIX
  • Became very popular for server-side Web
    programming because of platform independence and
    text parsing ability.

3
"hello world" in Perl
  • Running Perl programs
  • From command line Unix/Linux, Active Perl on
    Windows
  • Using an IDE MacPerl
  • As CGI program via HTTP request

4
Strings -- double or single quotes?
  • Double quoted strings
  • Escape sequences are interpreted
  • print "scoob\_at_afraid.com\nx\_at_y.z" gt
    scoob_at_afraid.com
  • Variables are interpolated
  • num10
  • print "The number is num" gt The number is 10
  • Single quoted strings
  • Escape sequences are NOT interpreted (except two
    cases \' and \\) and variables are NOT
    interpolated
  • print 'Scooby\nDoo\n' gt Scooby\nDoo\n
  • Ideal for printing HTML code which has lots of
    quotes
  • print 'lta href"mailtoscoob_at_afraid.com"gtemail
    Scooblt/agt',"\n"
  • print "lta href\"mailtoscoob\_at_afraid.com\"gtemail
    Scooblt/agt\n"

5
Scalar variables
  • Preceded with character
  • num 10
  • name "Raul"
  • Can hold strings or numbers (no formal Boolean
    type literal)
  • x "hello"
  • x 3
  • x 3.14
  • Perl is loosely typed (like JavaScript)
  • x "3"
  • y "4"
  • z x y
  • on-the-fly conversion -- result of 12

6
Concatenation -- (.)
  • Example
  • x"base"
  • y"ball"
  • zx.y "baseball"
  • x21
  • y12
  • zx.y "2112"

7
on-the-fly conversion examples
Perl tries to choose the proper context for an
operation.
8
Function calls
  • Standard notation
  • functionname(argument)
  • In Perl, the parentheses are optional!
  • functionname argument
  • Some built-in functions
  • x sqrt 36 6
  • x int 2.77 2 (truncation)
  • str "hello"
  • length str 5

9
chop and chomp
  • chop removes the last character and returns it
  • str "Hello\n\n"
  • x chop str
  • str contains "Hello\n" -- x contains "\n"
  • chomp removes ALL new line characters at the end
    of the string and returns the number of
    characters "chomped"
  • str "Hello\n\n"
  • x chomp str
  • str contains "Hello" -- x contains 2

10
The input operator ltgt
  • Returns one line from standard input (the
    keyboard) including the \n
  • oneline ltgt
  • To read individual lines from text files
  • oneline ltFILEHANDLEgt

11
printf
  • To format numeric output
  • printf "round to two decimals .2f and no
    decimals .0f" , 1.6667, 2.6667
  • gt round to two decimals 1.67 and no decimals 3
  • The is the field specifier and f stands for
    float
  • The specified fields are replaced (in order) with
    the numbers supplied at the end after the
    formatting has been applied
  • x4.99
  • y7
  • printf "integer d funky integer 03d", x,y
  • gt integer 4 funky integer 007

12
sprintf
  • cost"19.3333333"
  • formattedCost sprintf "\.2f" , cost
  • formatted cost contains "19.33"
  • hours4.05
  • minutes3
  • time sprintf "d02d" , hours , minutes
  • time contains "403"

13
A "Hello World" CGI program
14
The "hello world" CGI program executed via an
http transaction.
15
Usual CGI errors
  • You forget the shebang line
  • ! /usr/bin/perl
  • Result Internal Server Error 500
  • The server software can't find to Perl
    interpreter to run the program. The server
    returns an error page to the browser saying.
  • You forget to print the Content-type line or
    write it incorrectly.
  • print "Content-type text/html\n\n"
  • Result Internal Server Error 500
  • The server software needs the Content-type line
    to complete the http header -- something like
  • The server software adds all but the Content-type
    like followed by a blank line. You MUST supply
    that.

16
Usual CGI errors
  • The CGI program does not have permissions set so
    it can be executed by an anonymous user (anyone).
  • Result Forbidden Server Error 403
  • The program tries to do something illegal like
  • -- Using ltgt to try to read keyboard input
  • -- Trying to open a non-existent file
  • Result Internal Server Error 500
  • One more potential problem You don't get one of
    the server software's pre-made Web pages, but the
    browser gets back a Web page which doesn't
    display properly (or at all).
  • Cause The program is generating bad HTML. The
    Web server software does not care about this.
    The server software does not read the HTML. It
    is up to the browser to try to display what it
    gets.

17
Print Block
  • A handy way to print larger chunks of HTML.
  • Has best features from printing single and double
    quoted strings
  • Escape sequences are interpreted (like double
    quoted strings)
  • Scalar variables are interpolated (like double
    quoted strings)
  • Quotes (") don't need to be escaped (like single
    quoted strings)
  • Use any legal variable name -- the convention is
    to use upper-case and to be descriptive.
  • NEVER indent the closing label.
  • value"1234 Maple Ave."
  • print ltltAFORM
  • ltform action"http/www.uweb.edu/program.cgi"
    method"GET"gt
  • ltinput type"text" name"address"
    value"value"gt
  • lt/formgt
  • AFORM
Write a Comment
User Comments (0)
About PowerShow.com