introduction to programming the www i cmsc 10100-1 summer 2004 lecture 11
TRANSCRIPT
Introduction to Programming the WWW I
Introduction to Programming the WWW I
CMSC 10100-1
Summer 2004
Lecture 11
2
Today’s TopicsToday’s Topics
• Looping statement
• Hash data type
• Working with files
3
Looping StatementsLooping Statements
• Advantages of using loops: Your programs can be much more concise.
When similar sections of statements need to be repeated in your program, you can often put them into a loop and reduce the total number of lines of code required.
You can write more flexible programs. Loops allow you to repeat sections of your program until you reach the end of a data structure such as a list or a file (covered later).
4
Advantages of Using LoopsAdvantages of Using Loops
$max = 0; $max = 0; $i=0;if ( $grades[0] > $max ) { while ( $i < 4 ) { $max = $grades[0]; if ( $grades[$i] > $max ) {} if ( $grades[1] > $max ) { $max = $grades[$i];
$max = $grades[1]; }} if ( $grades[2] > $max ) { $i = $i + 1;
$max = $grades[2]; }} if ( $grades[3] > $max ) {
$max = $grades[3];}
Each time through looplook at different list element.
Without looping must have a separate"if" statement for every list element.
5
The Perl Looping ConstructsThe Perl Looping Constructs
• Perl supports four types of looping constructs:
The for loop The foreach loop The while loop The until loop
• They can be replaced by each other
6
The for loopThe for loop
• You use the for loop to repeat a section of code a specified number of times
(typically used when you know how many times to repeat)
for ( $i = 0; $i < $max; $i++ ) {
Set of statements to repeat}
Ini ti al i zati onexpressi on. Sets thei ni ti al val ue of "$i "
Loop test condi ti on.
I terati on expressi on.Increment $i each
l oop i terati on
7
3 Parts to the for Loop3 Parts to the for Loop
• The initialization expression defines the initial value of a variable used to control the loop. ($i is used above with value of 0).
• The loop-test condition defines the condition for termination of the loop. It is evaluated during each loop iteration. When false, the loop ends. (The loop above will repeat as long as $i is less than $max).
• The iteration expression is evaluated at end of each loop iteration. (In above loop the expression $i++ means to add 1 to the value of $i during each iteration of the loop. )
8
for loop examplefor loop example
1. #!/usr/local/bin/perl2. print 'The sum from 1 to 5 is: ';3. $sum = 0;4. for ($count=1; $count<6; $count++){5. $sum += $count; 6. }7. print "$sum\n";
This would output The sum from 1 to 5 is: 15
Run in Perl Builder
9
The foreach Loop The foreach Loop
• The foreach loop is typically used to repeat a set of statements for each item in an array
• If @items_array = (“A”, “B”, “C”); Then $item would “A” then “B” then “C”.
foreach $item ( @items_array ) {
Set of statements to repeat}
A loop scalar value.It receives the next list elementeach loop iteration.
Repeat the loop once forevery element in the listvariable.
10
foreach Exampleforeach Example
1. #!/usr/local/bin/perl2. @secretNums = ( 3, 6, 9 );3.$uinput = 6; 4. $ctr=0; $found = 0;5. foreach $item ( @secretNums ) {6. $ctr=$ctr+1;7. if ( $item == $uinput ) {
print "Number $item. Item found was number $ctr<BR>";
8. $found=1;9. last;10. }11.}12.if (!$found){13. print “Could not find the number/n”;14. }
The last statement willForce an exit of the loop
Run in Perl Builder
11
Would Output The Following ...Would Output The Following ...
12
The while Loop The while Loop
• You use a while loop to repeat a section of code as long as a test condition remains true.
while ( $ctr < $max ) {
Set of statements to repeat}
Repeat as longas the conditionaltest is true.
Conditionenclosed in parenthesis
13
Consider The Following ...Consider The Following ...
Calculation of sum from 1 to 5
1. #!/usr/local/bin/perl2. print 'The sum from 1 to 5 is: ';3. $sum = 0; $count=1;4. while ($count < 6){5. $sum += $count; 6. $count++;7. }8. print "$sum\n";
Run in Perl Builder
14
Hw3 discussionHw3 discussion
• Problem3: using while loop to read input from command line# while the program is running, it will return # to this point and wait for inputwhile (<>){
$input = $_;chomp($input);…
}
• <> is the input operator (angle operator) with a included file handle Empty file handle = STDIN
• $_ is a special Perl variable It is set as each input line in this example
• Flowchart my example
chomp() is a built-in functionto remove the end-of-line character of a string
15
The until Loop The until Loop
• Operates just like the while loop except that it loops as long as its test condition is false and continues until it is true
do { Set of statements to repeat
} until ( $x > 100 );
Repeat unti l thi scondi ti on i s true.
The "do"word startsthe l oop
16
Example Program Example Program
Calculation of sum from 1 to 5
1. #!/usr/local/bin/perl2. print 'The sum from 1 to 5 is: ';3. $sum = 0; $count=1;4. do {5. $sum += $count; 6. $count++;7. }until ($count >= 6);8. print "$sum\n";
until loop mustend with a ;
Run in Perl Builder
17
Logical Operators(Compound Conditionals)
Logical Operators(Compound Conditionals)
• logical conditional operators can test more than one test condition at once when used with if statements, while loops, and until loops
• For example, while ( $x > $max && $found ne ‘TRUE’ )
will test if $x greater than $max AND $found is not equal to ‘TRUE’
18
Some Basic Logical Operators Some Basic Logical Operators
• &&—the AND operator - True if both tests must be true:
while ( $ctr < $max && $flag == 0 )
• ||—the OR operator. True if either test is true
if ( $name eq “SAM” || $name eq “MITCH” )
• !—the NOT operator. True if test falseif ( !($FLAG == 0) )
19
Consider the following ...Consider the following ...1. #!/usr/local/bin/perl2. @safe = (1, 7);3. print ('My Personal Safe');4. $in1 = 1;5. $in2 = 6;6. if (( $in1 == $safe[0] ) && ( $in2 == $safe[1])){7. print "Congrats you got the combo";8. }elsif(( $in1 == $safe[0] ) || ( $in2 == $safe[1])){9. print “You got half the combo";10.}else {11. print "Sorry you are wrong! ";12. print "You guessed $in1 and $in2 ";13.}
Run in Perl Builder
20
Hash Lists (or associated arrays)Hash Lists (or associated arrays)
• Items not stored sequentially but stored in pairs of values
the first item the key, the second the data
The key is used to look up or provide a cross-reference to the data value.
Instead of using sequential subscripts to refer to data in a list, you use keys.
21
Advantages of Hash ListsAdvantages of Hash Lists
• Need to cross-reference one piece of data with another. Perl supports some convenient functions that use hash
lists for this purpose. (E.g,. You cross reference a part number with a product description.)
• Concerned about the access time required for looking up data. Hash lists provide quicker access to cross-referenced data.
E.g, have a large list of product numbers and product description, cost, and size, pictures).
22
Using Hash ListsUsing Hash Lists
• General Format to define a hash list:
• Alternate syntax%months = ( "Jan" => 31, "Feb" => 28, "Mar" => 31,
"Apr" => 30, "May" => 31, "Jun" => 30, "Jul" => 31, "Aug" => 31, "Sep" => 30, "Oct" => 31,"Nov" => 30,
"Dec" => 31 );
%months = ( "Jan", 31, "Feb", 28, "Mar", 31, "Apr", 30, "May", 31, "Jun", 30, "Jul", 31, "Aug", 31,
"Sep", 30, "Oct", 31, "Nov", 30, "Dec", 31 );
Name of hash list variable start with '%'.Key element 1 and data element 1.
Key element 2 and data element 2.
Key element 3 and data element 3.
23
Accessing Hash List ItemAccessing Hash List Item
• When access an individual item, use the following syntax:
• Note: You Cannot Fetch Keys by Data Values. This is NOT valid:
$mon = $months{ 28 };
$day = $months{'Mar'};
Requests touse this key tofetch it's value.
Starts with'$' since,resultsis scalar.
Enclose incurly brackets.
24
Hash Keys and Values Access Functions
Hash Keys and Values Access Functions
• The keys() function returns a list of all keys in the hash list
%Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12);
@keyitems = keys(%Inventory);print “keyitems= @keyitems”;
• The values() function returns a list of all values Example,
%Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12);@values = values(%Inventory);print “keyvaluess= @values”;
• Perl outputs hash keys and values according to how they are stored internally. So, a possible output order:
keyitems= Screws Bolts Nuts
25
Keys() & values() are often used to output the contents of a hash list. For example,
%Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12);foreach $key ( keys (%Inventory) ) { print “Key=$key Value=$Inventory{$key} ";}
• The following is one possible output order: Item=Screws Value=12 Item=Bolts Value=55 Item=Nuts Value=33
Using keys() and values()Using keys() and values()
26
Changing a Hash ElementChanging a Hash Element
• You can change the value of a hash list item by giving it a new value in an assignment statement. For example,%Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12); $Inventory{‘Nuts’} = 34;
• This line changes the value of the value associated with Nuts to 34.
27
Adding a Hash ElementAdding a Hash Element
• You can add items to the hash list by assigning a new key a value. For example, %Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12);
$Inventory{‘Nails’} = 23;
• These lines add the key Nails with a value of 23 to the hash list.
28
Deleting a Hash ElementDeleting a Hash Element
• You can delete an item from the hash list by using the delete() function. For example, %Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12);
delete $Inventory{‘Bolts’};
• These lines delete the Bolts key and its value of 55 from the hash list.
29
Verifying an Element’s ExistenceVerifying an Element’s Existence
• Use the exists() function verifies if a particular key exists in the hash list. It returns true ( or 1) if the key exists. (False if not). For
example: %Inventory = ( 'Nuts', 33, 'Bolts', 55, 'Screws', 12);if ( exists( $Inventory{‘Nuts’} ) ) {
print ( “Nuts are in the list” );} else {
print ( “No Nuts in this list” );
}
• This code outputs
Nuts are in the list.
30
Environmental Hash ListsEnvironmental Hash Lists
• When your Perl program starts from a Web browser, a special environmental hash list is made available to it.
Comprises a hash list that describe the environment (state) when your program was called. (called the %ENV hash).
• Can be used just like any other hash lists. For example, print “Language=$ENV{‘HTTP_ACCEPT_LANGUAGE’}”;
31
Some environmental variables
Some environmental variables
• HTTP_USER_AGENT. defines the browser name, browser version, and computer platform of the user who is starting your program. For example, its value might be “Mozilla/4.7 [en] (Win 98, I)” for Netscape.
You may find this value useful if you need to output browser-specific HTML code.
32
Some Environmental Variables
Some Environmental Variables
• HTTP_ACCEPT_LANGUAGE - defines the language that the browser is using. E.g., might be “en” for English for Netscape or “en-us” for English for Internet Explorer.
• REMOTE_ADDR - indicates the TCP/IP address of the computer that is accessing your site. (ie., the physical network addresses of computers on
the Internet —for example, 65.186.8.8.)
May have value if log visitor information
33
Some environmental variables
Some environmental variables
• REMOTE_HOST - set to the domain name of the computer connecting to your Web site. It is a logical name that maps to a TCP/IP
address—for example, www.yahoo.com. It is empty if the Web server cannot translate the
TCP/IP address into a domain name.
• Display all environmental variables http://people.cs.uchicago.edu/~hai/hw4/env.cgi
34
Example: Checking LanguageExample: Checking Language
#!/usr/local/bin/perlprint "content-type: text/html\n\n";print "<html>\n<head>\n";print "<title>Check Environment</title>";print "</head>\n<body>\n";$lang=$ENV{'HTTP_ACCEPT_LANGUAGE'};if ( $lang eq 'en' || $lang eq 'en-us' ) { print "Language=$ENV{'HTTP_ACCEPT_LANGUAGE'}"; print "<BR>Browser= $ENV{'HTTP_USER_AGENT'}";}else { print 'Sorry I do not speak your language';}print "\n</body>\n</html>";
http://people.cs.uchicago.edu/~hai/hw4/check_lan.cgi
35
Printing Text BlocksPrinting Text Blocks• In Perl there are easier ways to print out large blocks of code
instead of line-by-line• Solution 1:
print <<"CodeBlock"; #some word(s) to indicate the beginning of a # block(no " " are needed) if it's a simple word .... CodeBlock #same word(s) to mark the end of block. #Notice no quotes or semicolon here!
• Solution 2:print qq+ #qq followed by some character not used in #the following text to start the block .... +; #the same character to mark the end of the block. #Notice semicolon #here!
• Examples: old_version solution1 solution2
36
Creating a Hash List of List Items Creating a Hash List of List Items
• Hash tables use a key to cross-reference the key with a list of values (instead of using simple key/value pairs).
• For example consider the following tablePartNumber
PartName
NumberAvailable
Price Picture File
AC1000 Hammer 122 12 hammer.gif
AC1001 Wrench 344 5 wrench.gif
AC1002 Hand Saw 150 10 saw.gif
37
Creating Hash TablesCreating Hash Tables
%Inventory = (AC1000 => [ 'Hammer', 122, 12, 'hammer.gif'],AC1001 => [ 'Wrench', 344, 5, 'wrench.gif'],AC1002 => [ 'Hand Saw', 150, 10, 'saw.gif']
);
Use '=>' to associate key with list.
A hashvariablename.
Part Num is key for a list of items.
List is enclosedin square brackets.
• Access items from a hash table much like you access a hash list:
$Inventory{‘AC1000’}[0] is ‘Hammer’,
$Inventory{‘AC1001’}[0] is ‘Wrench’,
$Inventory{‘AC1002’}[1] is 150.
38
Changing Hash Table ItemsChanging Hash Table Items
• Changing Hash table items:%Inventory = (
AC1000 => [ 'Hammer', 122, 12, 'hammer.gif'],
AC1001 => [ 'Wrench', 344, 5, 'wrench.gif'],
AC1002 => [ 'Hand Saw', 150, 10, 'saw.gif']
);
$numHammers = $Inventory{‘AC1000’}[1];
$Inventory{‘AC1001’}[1] = $Inventory{‘AC1001’}[1] + 1;
$partName = $Inventory{‘AC1002’}[0];
print “$numHammers, $partName, $Inventory{‘AC1001’}[1]”;
This code would output
122, Hand Saw, 345
39
Adding/Deleting Hash Table ItemsAdding/Deleting Hash Table Items
• When you want to add a hash table row, you must specify a key and list of items.
For example, the following item adds an entry line to the %Inventory hash table
%Inventory = ( AC1000 => [ 'Hammers', 122, 12, 'hammer.gif'], AC1001 => [ 'Wrenches', 344, 5, 'wrench.gif'], AC1002 => [ 'Hand Saws', 150, 10, 'saw.gif'] );
$Inventory{‘AC1003’} = [‘Screw Drivers’, 222, 3,
‘sdriver.gif’ ];
40
• Because the exists() and delete() hash functions both work on a single key,specify them just as you did before
For example, the following code checks whether a key exists before deleting it:
if ( exists $Inventory{‘AC1003’} ) { delete $Inventory{‘AC1003’};
} else { print “Sorry we do not have the key”;
}
• Check if the record exists before you try to delete it.
Adding/Deleting Hash Table Items
41
Working with FilesWorking with Files
• So far programs cannot store data values in-between times when they are started.
Working with files enable programs to store data, which can then be used at some future time.
• Will describe ways to work with files in CGI/Perl programs, including opening files, closing files, and reading from and writing to files
42
Using the open() Function Using the open() Function
• Use to connect a program to a physical file on a Web server. It has the following format:
file handle - Starts with a letter or number—not with “$”, “@”, or “%”. (Specify in all capital letters. (By Perl convention.)
filename - name of file to connect to. If resides in the same file system directory then just specify the filename (and not the entire full file path).
open ( INFILE, "mydata.txt" ); Filename: The nameof the file on the web server.
Filehandle: A name used torefer to the file in your program.
43
More On open() functionMore On open() function
• open() returns 1 (true) when it successfully opens and returns 0 (false) when this attempt fails.
• A common way to use open()$infile = “mydata.txt”;
open (INFILE, $infile ) || die “Cannot open $infile: $!”;
Execute die only when open fails
Output systemmessage
Connect to mydata.txt.
Perl specialvariable, containingthe error string
44
Specifying FilenamesSpecifying Filenames
• So far need to keep file in same directory You can specify a full directory path name for
the file to be opened.
My "home" filesystemon my fileserver.
$infile="/home/perlpgm/per-pgm-www/cgi-bin/C7/infile.txt";Directories potentiallyviewable on the Internet.
The name of the file.
45
File HandlesFile Handles• Use the file handle to refer to an opened file• Combine with the file handle with the file input operator
(“<>”) to read a file into your program
• Perl automatically opens 3 file handles upon starting a program
STDIN -- standard in, usually the keyboard
• Empty input operator (<>) is the same as <STDIN>
STDOUT -- standard out, usually the monitor screen
• print() function prints to STDOUT by default
STDERR -- standard error, usually the monitor screen
46
Using the File Handle to Read Files
Using the File Handle to Read Files
• You can read all the content of a file into an array variable
Each line turns to an array item
• Or you can read the file line by line
Using the special variable of Perl, $_
• The “mydata.txt” file used in the following 2 examples:
Apples are redBananas are yellowCarrots are orangeDates are brown
47
Reading File into Array Reading File into Array
$infile="mydata.txt";open (INFILE, $infile ) || die "Cannotopen $infile: $!";@infile = <INFILE>;print $infile[0];print $infile[2];close (INFILE);
• Then the output of this program would beApples are red
Carrots are orange
http://people.cs.uchicago.edu/~hai/hw4/readFile1.cgi
48
Reading One Line At a TimeReading One Line At a Time
• Reading a very large file into the list variable @infile consumes a lot of computer memory. Better is to read one line at a time. For example the
following would print each line of the input file. $infile=”mydata.txt”;
open (INFILE, $infile ) || die “Cannot open $infile: $!”;
while ( <INFILE> ) { $inline = $_;
print $inline; }
close (INFILE);
Automatically setto the next input line.
http://people.cs.uchicago.edu/~hai/hw4/readFile2.cgi
49
Two String FunctionsTwo String Functions• split(/pattern/, string)
Search the string for occurrence of the pattern. it encounters one, it puts the string section before the pattern into an array and continues to search
Return the array Example: (run in command line)
http://people.cs.uchicago.edu/~hai/hw4/split.cgi
• join(“pattern”, array) Taking an array or list of values and putting the into a single
string separated by the pattern Return the string Reverse of split() Example: http://people.cs.uchicago.edu/~hai/hw4/join.cgi
50
Working with split() Function Working with split() Function
• Data usually stored as records in a file Each line is a record Multiple record members usually separated
by a certain delimiter
• The record in the following input file “part.txt” has the format part_no:part_name:stock_number:price
AC1000:Hammers:122:12AC1001:Wrenches:344:5AC1002:Hand Saws:150:10AC1003:Screw Drivers:222:3
51
Example ProgramExample Program
…$infile="infile.txt";open (INFILE, $infile )|| die "Cannot open $infile:$!";while ( <INFILE> ) {$inline=$_;
($ptno, $ptname, $num, $price ) = split ( /:/,
$inline ); print "We have $num $ptname ($ptno). "; print "The cost is $price dollars.", br;}close (INFILE);…http://people.cs.uchicago.edu/~hai/hw4/readFile3.cgi
52
Using Data FilesUsing Data Files
• Do not store your data files in a location that is viewable by people over the Internet—
too much potential for tampering by people you do not know.
Make sure permissions are set correctly (644)
$infile="/home/perlpgm/data/mydata.txt";A directory created for my data files.
My home filesystemon my web server.
Name of my file .
53
Open ModesOpen Modes
• Three open modes are commonly used to open a file:
read-only (default mode) To explicitly specify it, put the character < before the filename.
open(INFILE, “<myfile.txt”) || die “Cannot open: $!”;
write-only-overwrite: allows you to write to a file. • If the file exists, it overwrites the existing file with the output
of the program.
• To specify this mode, use > at the beginning of the filename used in the open() function. For example,
open(INFILE, “>myfile.txt”) || die “Cannot open: $!”;
54
Open Modes(cont’d)Open Modes(cont’d)
• write-only-append: allows you to write and append data to the end of a file. If the file exists, it will write to the end of the existing
file. Otherwise will create it. To specify this mode, use >> before the filename in
the open() function.open(OFILE, “>>myfile.txt”) || die “Cannot open: $!”;
• Example way to write to print OFILE “My program was here”;
55
Locking Before WritingLocking Before Writing
• If two programs try to write to the same file at the same time, can corrupt a file. an unintelligible, usefully mixture of file
contents provides a flock() function that ensures only
one Perl program at a time can write data.
flock(OFILE, 2);
Exclusive access to file.
File handle.
Change 2 to 8To unlock the file
http://www.icewalkers.com/Perl/5.8.0/pod/func/flock.html
56
Example of flock()Example of flock()
$outfile=">>/home/perlpgm/data/mydata.txt";open (OFILE, $outfile ) || die "Cannot open $outfile: $!";
flock( OFILE, 2 );print OFILE "AC1003:Screw Drivers:222:3\n";
close (OFILE);
• Appends the following to part.txt :
AC1003:Screw Drivers:222:3
57
Reading and Writing FilesReading and Writing Files
#!/usr/bin/perlprint "Content-type: text/html\n\n";print qq+<html><head><title>My Page</title></head><body><FONT SIZE=5>WELCOME TO MY SITE </FONT>+;$ctfile="counter.txt";open (CTFILE, "<" . $ctfile ) || die "Cannot open $infile: $!";@inline = <CTFILE>;$count=$inline[0] + 1;close (CTFILE);open (CTFILE, ">$ctfile" ) || die "Cannot open $infile: $!";flock (CTFILE, 2);print CTFILE "$count";close (CTFILE);print qq+<br><FONT COLOR=BLUE>You Are Visitor $count </FONT></body></html>+;
counter.txt permissionmust be RW
http://people.cs.uchicago.edu/~hai/hw4/readWriteFile1.cgi
58
Read Directory ContentRead Directory Content
• Not much different from reading a file Use opendir(), readdir(), and closedir() functions
• Example:$dir = "/home/hai/html/hw4/";
opendir(DIR, $dir);
@content = readdir(DIR);
foreach $entry (@content){
print $entry . "\n";
}
closedir(DIR);
http://people.cs.uchicago.edu/~hai/hw4/readDir1.cgi
59
File TestFile Test
Example of Perl file testing functions: -e $file : exists -z $file : zero file -s $file : non-zero file, returns size -r $file : readable -w $file : write-able -x $file : executable -f $file : plain file -d $file : directory -T $file : text file -B $file : binary file -M $file : age in days since modified -A $file : age in days since last accessed
http://www.netadmintools.com/html/1perlfunc.man.html#ixABE
60
File TestFile Test
@content = readdir(DIR); if (-d $entry ){ # test if it is a directory print $entry . " is a directory\n"; }elsif (-f $entry ){ # test if it is a file print $entry . " is a "; if (-T $entry ){ # test if it is a text file print "text file,"; }elsif (-B $entry ){ # test if it is a binary file print "binary file,"; }else { print "file of unknown format,"; } print " “ . (-s $entry) . " bytes\n"; # get the file size
}
http://people.cs.uchicago.edu/~hai/hw4/readDir2.cgi