Download presentation
Presentation is loading. Please wait.
Published byJenna Page Modified over 11 years ago
1
Chapter 25 Perl and CGI (Common Gateway Interface)
2
Outline Introduction Introduction First program in Perl First program in Perl Variables Variables Scalars and Their Operations Scalars and Their Operations String Processing String Processing Fundamentals of Arrays Fundamentals of Arrays Hashes Hashes Viewing Environment Variables Viewing Environment Variables Form Processing Form Processing
3
Introduction Practical Extraction and Report Language (Perl) Practical Extraction and Report Language (Perl) –One of the most widely used language for Web programming Common Gateway Interface (CGI) Common Gateway Interface (CGI) –Standard interface through which users interact with applications on Web servers –Provides way for clients to interact with applications on Web server –CGI script Can be written in many different languages Can be written in many different languages Perl is the most commonly used language for CGI Perl is the most commonly used language for CGI
4
Introduction Data path of a typical CGI-based application.
5
Introduction Perl is case sensitive Perl is case sensitive A name must begin with a letter; any number of letters, digits, or underscore characters can follow A name must begin with a letter; any number of letters, digits, or underscore characters can follow By convention, names of variables use only lowercase letters By convention, names of variables use only lowercase letters Comment character ( # ) Comment character ( # ) –Instruct interpreter to ignore everything on current line following # –Allows programmers to write descriptive comments in programs –shebang construct ( #! ) Indicates the path to the Perl interpreter Indicates the path to the Perl interpreter
6
First Program in Perl
7
Variables Variables are implicitly declared Variables are implicitly declared A scalar variable that has not been assigned a value has the value undef A scalar variable that has not been assigned a value has the value undef –In numeric context Evaluates to 0 Evaluates to 0 –In a string context Empty string () Empty string (" ")
9
Scalars and Their Operations Numeric Operators – –Like those of C, Java, etc. String Operators – –Catenation - denoted by a period e.g., If the value of $dessert is "apple", the value of $dessert. " pie" is "apple pie" – –Repetition - denoted by x e.g., If the value of $greeting is "hello ", the value of $greeting x 3 is "hello hello hello "
10
fig25_04.pl (1 of 2)
12
String Processing Perl has two kinds of string literals, those delimited by double quotes and those delimited by single quotes – –Single-quoted literals cannot include escape sequences – –Double-quoted literals can include them – –Names embedded in double-quoted string literals are interpolated – –e.g., If the value of $salary is 47500, the value of "Jack makes $salary dollars per year " is "Jack makes 47500 dollars per year"
13
String Processing Text manipulation Text manipulation –Done with a regular expression Series of characters that serves as a pattern-matching template Series of characters that serves as a pattern-matching template String-processing tasks String-processing tasks –Can be accomplished by using Perls equality and comparison operators foreach statement foreach statement –Iterates sequentially through elements
14
fig25_06.pl (1 of 1)
15
Fundamentals of Arrays Perl arrays store only scalar values, which can store strings, numbers, and references Perl arrays store only scalar values, which can store strings, numbers, and references A list is an ordered sequence of scalar values A list is an ordered sequence of scalar values –Range operator (.. ) can be used to create a list –e.g. @array = (A.. Z) A list literal that has only scalar names can be the target of a list assignment A list literal that has only scalar names can be the target of a list assignment –e.g. ($one, $two, $three) = (1, 2, 3); An array is a variable that can store a list An array is a variable that can store a list
16
Fundamentals of Arrays Array names all begin with at signs (@) Array names all begin with at signs (@) Arrays can be assigned other arrays or list literals Arrays can be assigned other arrays or list literals –e.g. @list = (2, 4, 6, 8); @list2 = @list; If an array is used where a scalar is expected, the length of the array is used If an array is used where a scalar is expected, the length of the array is used –e.g. @list = (1, 55, 193); $len = @list; # $len now has the value 3 $len = @list; # $len now has the value 3 When an array element is referenced or assigned, the name is a scalar name When an array element is referenced or assigned, the name is a scalar name –e.g. $list[3] = 17; $age = $list[1]; The length of an array is dynamic; it is always the highest subscript that has been assigned, plus 1 The length of an array is dynamic; it is always the highest subscript that has been assigned, plus 1
17
fig25_05.pl (1 of 2)
19
Hashes Differ from arrays in two fundamental ways: Differ from arrays in two fundamental ways: 1. Arrays use integers as indices, hashes use strings 1. Arrays use integers as indices, hashes use strings 2. Array elements are ordered, hash elements are not 2. Array elements are ordered, hash elements are not Hash names begin with percent signs (%) Hash names begin with percent signs (%) –List literals are used to initialize hashes –Can be comma-separated values e.g. %hash1 = ("Monday", 10451, "Tuesday", 10580); e.g. %hash1 = ("Monday", 10451, "Tuesday", 10580); –Or, implication symbols can be used between a key and its value e.g. %hash2 = ("Monday" => 10451, "Tuesday" => 10580); e.g. %hash2 = ("Monday" => 10451, "Tuesday" => 10580); Subscripts are keys (strings) placed in braces Subscripts are keys (strings) placed in braces e.g. $s = $salaries{"Joe Schmoe"}; e.g. $s = $salaries{"Joe Schmoe"}; $salaries{"Michel Angelo"} = 1000000; $salaries{"Michel Angelo"} = 1000000;
20
Hashes Elements can be deleted with delete Elements can be deleted with delete –e.g. delete $salaries{"Bill Clinton"}; Use exists to determine whether a key is in a hash Use exists to determine whether a key is in a hash –e.g. if (exists $salaries{"George Bush"}) …
21
Viewing Environment Variables Environment variables Environment variables –Contain information about execution environment in which a script is being run %ENV hash %ENV hash –Built-in table in Perl that contains names and values of all environment variables use statement use statement –Instructs Perl programs to include modules –Modules Contents of predefined packages Contents of predefined packages – import tag :standard Import a predefined set of standard functions Import a predefined set of standard functions –Key Value name Value name Assigned a value using the arrow operator ( => ) Assigned a value using the arrow operator ( => )
22
fig25_11.pl (1 of 2)
24
Form Processing XHTML forms XHTML forms –Enable Web pages collect data from users and send to Web server for processing by server-side programs and scripts –Function param Part of Perl CGI module Part of Perl CGI module Retrieves values from a form fields value Retrieves values from a form fields value –Function br Adds a break ( ) to XHTML page Adds a break ( ) to XHTML page –Functions span and div Adds and to page respectively Adds and to page respectively
25
fig25_12.html (1 of 3)
26
fig25_12.html (2 of 3)
27
fig25_12.html (3 of 3)
30
fig25_13.pl (2 of 4)
31
fig25_13.pl (3 of 4)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.