24
1 20-753: Fundamentals of Web Programming Lecture 8: Perl Basics Copyright © 1999, Carnegie Mellon. All Rights Reserved. Fundamentals of Web Programming Lecture 8: Perl Basics

Fundamentals of Web Programming

  • Upload
    lise

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Fundamentals of Web Programming

120-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

Fundamentals ofWeb Programming

Lecture 8: Perl Basics

Page 2: Fundamentals of Web Programming

220-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 3: Fundamentals of Web Programming

320-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

Perl characteristics

• Scripting language.• Interpreted.• Highly portable.• Easy to learn.• Suitable for prototyping.• Good support of CGI.• Also a bit eclectic...

Page 4: Fundamentals of Web Programming

420-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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 .

Page 5: Fundamentals of Web Programming

520-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

Required table

Name PC1 PC2 PC3

RAM 128MB 128MB 256MB

Disk 6GB 6GB 10GB

Modem No Yes Yes

Page 6: Fundamentals of Web Programming

620-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 7: Fundamentals of Web Programming

720-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 8: Fundamentals of Web Programming

820-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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

Page 9: Fundamentals of Web Programming

920-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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

Page 10: Fundamentals of Web Programming

1020-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

Scalar variables

• Scalar variables can hold only scalars.

• The name of a scalar variable begins with $.

• $pi = 3.14; $s1 = “hello”;• = denotes assignment.

Page 11: Fundamentals of Web Programming

1120-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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

Page 12: Fundamentals of Web Programming

1220-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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 = ();

Page 13: Fundamentals of Web Programming

1320-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 14: Fundamentals of Web Programming

1420-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 15: Fundamentals of Web Programming

1520-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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>

Page 16: Fundamentals of Web Programming

1620-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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>

Page 17: Fundamentals of Web Programming

1720-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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”;

Page 18: Fundamentals of Web Programming

1820-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 19: Fundamentals of Web Programming

1920-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

Conditional • if (expression) {

statement1; statement2;

} else { statement3; statement4; }

• if ($modem[$I] == 0) { print “No”;

} else {print “Yes”;}

Page 20: Fundamentals of Web Programming

2020-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 21: Fundamentals of Web Programming

2120-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.

Page 22: Fundamentals of Web Programming

2220-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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, “ “; }

Page 23: Fundamentals of Web Programming

2320-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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”; }

Page 24: Fundamentals of Web Programming

2420-753: Fundamentals of Web Programming

Lecture 8: Perl Basics

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

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.