240 likes | 288 Vues
Fundamentals of Web Programming. Lecture 8: Perl Basics. Introduction to Perl. Basic data and variable types. Scalars, arrays, hashes. Basic control structures: if-then, while, foreach . Example: generating HTML tables. Adding your own pages/scripts to Apache. Perl characteristics .
E N D
Fundamentals ofWeb Programming Lecture 8: Perl Basics Lecture 8: Perl Basics
Introduction to Perl • Basic data and variable types. • Scalars, arrays, hashes. • Basic control structures: if-then, while, foreach. • Example: generating HTML tables. • Adding your own pages/scripts to Apache. Lecture 8: Perl Basics
Perl characteristics • Scripting language. • Interpreted. • Highly portable. • Easy to learn. • Suitable for prototyping. • Good support of CGI. • Also a bit eclectic... Lecture 8: Perl Basics
Tedious task: HTML tables • Imagine you’ve been hired by a company selling PCs on the Web. • You’re responsible for creating a page that describes the products. • You start with three models, but then the number grows… • Some models are special . Lecture 8: Perl Basics
Required table Lecture 8: Perl Basics
What do we need to do? • Store information about the PCs. • Write a script that would generate the table in a way that allows for quick changes. • Install the script on the server. • Test it. Lecture 8: Perl Basics
Files, variables, data types • Usually, information is stored in files. • For now we will use variables. • Variable: a piece of memory with a name. • Variables have types: they can hold data of a particular type. Lecture 8: Perl Basics
Basic Data Types in Perl • Scalars: include numbers and strings. • Arrays or ordered lists. • Hashes. • A literal is the way a value is represented in the text of a program, e.g., “string”. Lecture 8: Perl Basics
Scalars • Scalars: • Integer literals, e.g. 123, 11111; • Float literals (‘real numbers’), e.g. 3.14, 1.2e10; • Single-quoted strings: ‘hello’, ‘don\’t’; • Double-quoted strings: “hello”, “this string contains \””. Lecture 8: Perl Basics
Scalar variables • Scalar variables can hold only scalars. • The name of a scalar variable begins with $. • $pi = 3.14; $s1 = “hello”; • = denotes assignment. Lecture 8: Perl Basics
Arrays • Arrays and lists: • A list is an ordered collection of data. • It has elements that are identified by indices. • Each element is a scalar. • Since lists are ordered, there is a first and a last element. • Can be empty (with no elements). Lecture 8: Perl Basics
Array literals and variables • The name of an array variable begins with @, e.g., @my_array. • In a program, lists are enclosed in (), e.g., @numbers = (1,2,3); @strings = (“CMU”, “UCLA”, “MIT”); @empty_list = (); Lecture 8: Perl Basics
Array Element Access • You use an index to access an element in an array. • Since arrays contain scalars, the resulting element will be a scalar. • So, if you assign @numbers = (1,2,3); $numbers[0] contains 1. Lecture 8: Perl Basics
Array Element Access • You can use a scalar variable to access an array element: $I = 1; $numbers[$I] equals 2. • For every array @a Perl provides a special scalar variable $#a that contains the index of the last element: e.g. $#numbers is 2; $#empty_list is -1. Lecture 8: Perl Basics
Printing a table row • Let’s define a few variables: • $pcName = “PC1”; $memory = 128; $disk = 6; $modem = 0; print “<tr>\n”; print “<td> $pcName </td> <td> $memory </td> <td> $disk </td> <td> $modem </td>\n”; print “</tr>\n”; • <tr> <td> PC1 </td> <td> 128 </td> <td> 6 </td> <td> 0 </td> </tr> Lecture 8: Perl Basics
Variable interpolation • When a variable name appears in a double-quoted string, the value of the variable is substituted for the name: print “<td> $pcName </td>” prints <td> PC1 </td> Lecture 8: Perl Basics
Same with arrays • @names = (“PC1”, “PC2”, “PC3”); @memory = (128, 128, 256); @disks = (6, 6, 10); @modems = (0, 1, 1); $I = 1; print “<tr>\n”; print “<td> $names[$I] </td> <td> $memory[$I] </td> <td> $disks[$I] </td> <td> $modems[$I]<td>\n”; print “<\tr>\n”; Lecture 8: Perl Basics
Control Statements • Control statements such as if, unless, foreach, while, etc, change the control flow in the program. • They usually evaluate an expression and their behavior depends on the value of the expression. Lecture 8: Perl Basics
Conditional • if (expression) { statement1; statement2; } else { statement3; statement4; } • if ($modem[$I] == 0) { print “No”; } else {print “Yes”;} Lecture 8: Perl Basics
Iteration: for statement • for ($I = 1; $I <= 10; $I++) { print “$I “; } • evaluate $I = 1; • repeat print “$I “; $I++ as long as $I <= 10 is true. Lecture 8: Perl Basics
Iteration: while statement • The above is equivalent to: $I = 1; while ($I <= 10) { print “$I “; } • The body of the loop is executed as long as the conditon holds. Lecture 8: Perl Basics
Iteration: foreach statement • foreach takes a list of values. • Assigns them one at a time to a scalar variable and executes the body. • @numbers = (1, 2, 3); foreach $number (@numbers) { print $number, “ “; } Lecture 8: Perl Basics
Complete example • @names = (“PC1”, “PC2”, “PC3”); @memory = (128, 128, 256); @disks = (6, 6, 10); @modems = (0, 1, 1); for ($I = 0; $I <= $#names; $I++) { print “<tr>\n”; print “<td> $names[$I] </td> <td> $memory[$I] </td> <td> $disks[$I] </td> <td> $modems[$I]<td>\n”; print “<\tr>\n”; } Lecture 8: Perl Basics
Common mistakes • The while loop requires correct initialization and control expression: watch out for array bounds. ($I = 0; $I <= $#array) • When you traverse an array in a loop, make sure that the index doesn’t get out of bounds; results in an error message. Lecture 8: Perl Basics