1 / 24

Fundamentals of Web Programming

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 .

vstevens
Download Presentation

Fundamentals of Web Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals ofWeb Programming Lecture 8: Perl Basics Lecture 8: Perl Basics

  2. 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

  3. 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

  4. 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

  5. Required table Lecture 8: Perl Basics

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Conditional • if (expression) { statement1; statement2; } else { statement3; statement4; } • if ($modem[$I] == 0) { print “No”; } else {print “Yes”;} Lecture 8: Perl Basics

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

More Related