Fundamentals of Web Programming

mewstennisΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

157 εμφανίσεις

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

1

Lecture 8: Perl Basics

Fundamentals of

Web Programming

Lecture 8: Perl Basics

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

2

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

3

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

4

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 .

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

5

Lecture 8: Perl Basics

Required table

Name
PC1
PC2
PC3
RAM
128MB
128MB
256MB
Disk
6GB
6GB
10GB
Modem
No
Yes
Yes
20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

6

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

7

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

8

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

9

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

10

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

11

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

12

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

13

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

14

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

15

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>

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

16

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>

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

17

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


20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

18

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

19

Lecture 8: Perl Basics

Conditional


if (expression) {



statement1;



statement2;
} else {



statement3;



statement4;
}



if ($modem[$I] == 0) {


print “No”;
} else {print “Yes”;}

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

20

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

21

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.

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

22

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

23

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

20
-
753: Fundamentals of

Web Programming

Copyright © 1999, Carnegie Mellon. All Rights Reserved.

24

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.