Docsity

CS 1520 Fall 2007: Perl for Web Applications - Lecture Notes, Study notes of Computer Science

Toggle sidebar
bg1
1
CS 1520 / Fall 2007
Programming Languages
for Web Applications
Alexandros Labrinidis
University of Pittsburgh
04 – Perl (cont)
Alexandros Labrinidis, Univ. of Pittsburgh 2CS 1520 / Fall 2007
How to run Perl programs (revised)
Use text editor (say pico) to write and save program, say hello .pl
Option 1:
perl -w hello.pl
Next time:
perl - w hello.pl
Option 2:
Make sure first line of hello.pl is the following
#!/bin/perl -w
chmod u+x hello.pl
./hello.pl
Next time:
./hello.pl
Alexandros Labrinidis, Univ. of Pittsburgh 3CS 1520 / Fall 2007
Lists and Arrays in Perl
Q: What is a List or Array?
A: a list is ordered scalar data
A: an array is a variable that holds a list
Example - list literals:
(1, 2, 3) #array of three values: 1, 2, and 3
(“fred”, 4, 5) #array of three values: “ fred”, 4, and 5
($a, 42) #two values: current value of $a and 42
($a+$b, $c+$d) #also two values
() #the empty list (no elements)
(1 .. 5) #same as (1, 2, 3, 4, 5)
Alexandros Labrinidis, Univ. of Pittsburgh 4CS 1520 / Fall 2007
More examples - arrays
@a = (“rachel”, “phoebe”, “c handler”, “ross”, “monica” ,
“joey”);
Shortcut:
@a = qw(rachel phoebe chandler ross monica joey);
qw stands for quote word: it creates a list from the
nonwhitespace parts within the parentheses
@b = qw(mary 2 5 had a little 4 lamb 6);
Q: what will the following do?
print (“the answer is “,@a,”\n” );
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS 1520 Fall 2007: Perl for Web Applications - Lecture Notes and more Study notes Computer Science in PDF only on Docsity!

CS 1520 / Fall 2007

Programming Languages

for Web Applications

Alexandros Labrinidis

University of Pittsburgh

04 – Perl (cont)

Alexandros Labrinidis, Univ. of Pittsburgh 2 CS 1520 / Fall 2007

How to run Perl programs (revised)

 Use text editor (say pico) to write and save program, say hello.pl

 Option 1:

perl -w hello.plNext time: perl - w hello.pl

 Option 2:

 Make sure first line of hello.pl is the following #!/bin/perl -w chmod u+x hello.pl ./hello.plNext time: ./hello.pl Alexandros Labrinidis, Univ. of Pittsburgh^3 CS 1520 / Fall 2007

Lists and Arrays in Perl

 Q: What is a List or Array?

 A: a list is ordered scalar data

 A: an array is a variable that holds a list

 Example - list literals:

 (1, 2, 3) #array of three values: 1, 2, and 3  (“fred”, 4, 5) #array of three values: “fred”, 4, and 5  ($a, 42) #two values: current value of $a and 42  ($a+$b, $c+$d) #also two values  () #the empty list (no elements)  (1 .. 5) #same as (1, 2, 3, 4, 5) Alexandros Labrinidis, Univ. of Pittsburgh^4 CS 1520 / Fall 2007

More examples - arrays

 @a = (“rachel”, “phoebe”, “chandler”, “ross”, “monica”,

“joey”);

 Shortcut:

 @a = qw (rachel phoebe chandler ross monica joey);

qw stands for quote word : it creates a list from the nonwhitespace parts within the parentheses

 @b = qw(mary 2 5 had a little 4 lamb 6);

 Q: what will the following do?

print (“the answer is “,@a,”\n”);

Alexandros Labrinidis, Univ. of Pittsburgh 5 CS 1520 / Fall 2007

Arrays - Assignment

 @rachel = (1, 2, 3);

#the rachel array is a 3-element list literal

 @monica = @rachel;

#the rachel array is copied to the monica array

 @joey = 1;

1 is automatically converted to (1)

 @ross = qw(one two three);

equivalent to @ross = (“one”, “two”, “three”);

Alexandros Labrinidis, Univ. of Pittsburgh 6 CS 1520 / Fall 2007

Arrays - Assignment (RHS)

 @phoebe = qw(smelly cat);

#equivalent to @phoebe = (“smelly”, “cat”);

 @chandler = (“here”, “is”, @phoebe, 5, 6);

#chandler becomes (“here”, “is”, “smelly”, “cat”, 5, 6);

 @chandler = (42, @chandler);

#puts 42 in front of chandler

 @chandler = (@chandler, “last”);

#puts “last” at the end of chandler Alexandros Labrinidis, Univ. of Pittsburgh^7 CS 1520 / Fall 2007

Arrays - Assignment (LHS)

 ($a, $b, $c) = (1, 2, 3)

#give value of 1 to $a, 2 to $b, 3 to $c

 ($a, $b) = ($b, $a)

#swap $a and $b

 ($d, @ross) = ($a, $b, $c)

#give $a to $d, and ($b, $c) to @ross

 ($f, @ross) = @ross;

#remove first element of @ross and give it to $f #this makes $f = $b, and @ross = ($c) Alexandros Labrinidis, Univ. of Pittsburgh^8 CS 1520 / Fall 2007

Scalar and List context

 Perl will do automatic “conversion” of an array,

depending on the context.

 List context : normal

 Scalar context : when the array must be “squeezed”

into a scalar variable.

 In this case, the length of the array is returned!

 Examples:

 @barney = @fred; # all of @fred is copied to @barney  ($a) = @fred; # $a gets the first element of @fred  $a = @fred; # $a gets the length of @fred

Alexandros Labrinidis, Univ. of Pittsburgh 13 CS 1520 / Fall 2007

If/else statement

if (some_expression) {

true_statement1;

true_statement2;

} else {

false_statement1;

false_statement2;

Alexandros Labrinidis, Univ. of Pittsburgh 14 CS 1520 / Fall 2007

Comparison operators

 Distinction between numeric and string comparison

Numeric String  Equal: == eq  Not equal: != ne  Less than: < lt  More than: > gt  Less than or equal: <= le  More than or equal: >= ge Alexandros Labrinidis, Univ. of Pittsburgh^15 CS 1520 / Fall 2007

If/elsif/else statement

if (first_expression) {

true_first_statement1;

true_first_statement2;

} elsif (second_expression) {

true_second_statement1;

true_second_statement2;

} else {

all_false_statement1;

all_false_statement2;

Alexandros Labrinidis, Univ. of Pittsburgh^16 CS 1520 / Fall 2007

Control Structures: while

 while loop

 while ($a > 5) { #do something }

 until

 until ($a <= 5) { $a++; }

Alexandros Labrinidis, Univ. of Pittsburgh 17 CS 1520 / Fall 2007

Control Structures: do/while

 do/while loop

 do { #do something } while ($a > 5);

 do/until loop

 do { #do something } until ($a <= 5); Alexandros Labrinidis, Univ. of Pittsburgh 18 CS 1520 / Fall 2007

while and do/while loops

while (some_expression) {

statement_1;

statement_2;

do {

statement_1;

statement_2;

} while (some_expression);

Alexandros Labrinidis, Univ. of Pittsburgh^19 CS 1520 / Fall 2007

while loop example

$stops = 0;

do {

$stops++;

print “Next step is stop number $stop\n”;

} while ($stops <5);

Alexandros Labrinidis, Univ. of Pittsburgh^20 CS 1520 / Fall 2007

For statement

for (initial_expr; test_expr; re_initialize_expr) {

statement_1;

statement_2;

initial_expr;

while (test_exp) {

statement_1;

statement_2;

re_initialize_expr;

Alexandros Labrinidis, Univ. of Pittsburgh 25 CS 1520 / Fall 2007

Lists/Arrays/Slices: Push/Pop

 Push (to right end of array)

 Adds an element to end of list  @list = (2, 4, 6);  push @list, 5 ; # @list now (2, 4, 6, 5 );  Push @list, (3, 10); # @list now (2, 4, 6, 5, 3, 10);

 Pop (from right end of array)

 Removes the last element of a list  $p = pop @list; # $p becomes 10  # @list now (2, 4, 6, 5, 3); Alexandros Labrinidis, Univ. of Pittsburgh 26 CS 1520 / Fall 2007

Lists/Arrays/Slices: Shift/Unshift

 Unshift (to left end of array)

 Adds an element to the beginning of list  @list = (2, 4, 6);  unshift (@list, 7); # @list now ( 7 , 2, 4, 6);  unshift (@list, 1, 12); # @list now (1, 12, 7, 2, 4, 6);

 Shift (from left end of array)

 Removes the first element of a list  $p = shift @list; # $p becomes 1  # @list now (12, 7, 2, 4, 6); Alexandros Labrinidis, Univ. of Pittsburgh^27 CS 1520 / Fall 2007

Lists/Arrays/Slices: Sort/Reverse

 Sort

 sorts elements of an array  Example:  @x = qw(small medium large);  @y = sort (@x); # @x stays the same  # @y is (“large”, “medium”, “small”)

 Reverse

 Reverses current order (i.e., NOT the reverse sort order)  @a = (10, 5, 12, 45);  @b = reverse @a; # @a stays the same  # @b is (45, 12, 5, 10) Alexandros Labrinidis, Univ. of Pittsburgh^28 CS 1520 / Fall 2007

Hashes

 Arrays are nice, index is always numeric

 Q: Would it not be nice to access arrays using strings?

 A: Yes.

 A: using Hashes!

 % class is a hash

 Initialization:

 $class{“CS1520”} = “SENSQ 5502”;  %class = (“CS1520” => “SENSQ 5502”, “CS1555” => “SENSQ 5502”);  %class = (“CS1520”, “SENSQ 5502”, “CS1555”, “SENSQ 5502”);

Alexandros Labrinidis, Univ. of Pittsburgh 29 CS 1520 / Fall 2007

Hash Functions

 keys(%hashname)

 return the list of current keys  $fred{“a”} = “b”;  $fred{“c”} = “d”;  $fred(15) = 143;  @list = keys(%fred); # @list = (“a”, “c”, 15);  $n = keys(%fred); # $n = 3;  foreach $k (keys (%fred)) { print “we have $fred{$k} at key $k\n”; }

 values(%hashname)

Alexandros Labrinidis, Univ. of Pittsburgh 30 CS 1520 / Fall 2007

Hash functions (cont)

 each (%hashname)

 Iterate over all elements of hash hashname  $lastname{“Alex”} = “Labrinidis”; $lastname{“Panos”} = “Chrysanthis”; $lastname{“John”} = “Ramirez”; while ( ($first, $last) = each (%lastname) ) { print “The last name of $first is $last\n”; }

 delete

 delete $lastname{“Panos”}; Alexandros Labrinidis, Univ. of Pittsburgh^31 CS 1520 / Fall 2007

Regular Expressions (intro)

 grep abc somefile > results

 while (<>) {

if (/abc/) {

print $_;

Alexandros Labrinidis, Univ. of Pittsburgh^32 CS 1520 / Fall 2007

Input/Output

 Output:

print - simple  printf - formatted version

 Input:

 standard input is accessed via  will return 0 if no more input  <> form is more general; can read from files  $_ to access line within loop  while () { print $_; }