introduction to programming the www i cmsc 10100-1 summer 2004 lecture 12

45
Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

Upload: molly-johnston

Post on 27-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

Introduction to Programming the WWW I

Introduction to Programming the WWW I

CMSC 10100-1

Summer 2004

Lecture 12

Page 2: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

2

Today’s TopicsToday’s Topics

• Working with files

• Subroutines(functions)

Page 3: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

3

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

Page 4: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

4

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 name of the file on the web server.

Filehandle: A name used torefer to the file in yourprogram.

Page 5: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

5

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

Page 6: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

6

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.

Page 7: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

7

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

Page 8: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

8

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

Page 9: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

9

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

Page 10: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

10

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

Page 11: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

11

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

Page 12: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

12

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

Page 13: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

13

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.\n";}close (INFILE);…http://people.cs.uchicago.edu/~hai/hw4/readFile3.cgi

Page 14: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

14

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 .

Page 15: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

15

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: $!”;

Page 16: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

16

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

Page 17: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

17

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

Page 18: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

18

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

Page 19: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

19

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

Page 20: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

20

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

Page 21: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

21

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

Page 22: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

22

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

Page 23: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

23

Subroutines (functions)Subroutines (functions)

• Subroutines provide a way for programmers to group a set of statements, set them aside, and turn them into mini-programs within a larger program.

• These mini-programs can be executed several times from different places in the overall program

• Library functions vs. User-defined functions

Page 24: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

24

Subroutine AdvantagesSubroutine Advantages

• Smaller overall program size. Can place statements executed several times into a subroutine and just call them when needed.

• Programs that are easier to understand and change. Subroutines can make complex and long programs easier to understand and change. (e.g., Divide into logical sections).

• Reusable program sections. You might find that some subroutines are useful to other programs. (E.g, Common page footer). common page footer.

Page 25: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

25

More Library FunctionsMore Library Functions

• abs() – return the absolute value of a number• rand() – generate a random number from 0 to the

input number Use int() with rand() Example: http://people.cs.uchicago.edu/~hai/hw4/rand.

cgi

• localtime() – used with time() function to determine the current date and time ($sec, $min, $hr, $day, $mon, $yr, $wkday,

$DayNumOfYr, $TZ ) = localtime(time()); Example: http://people.cs.uchicago.edu/~hai/hw4/localtime.

cgi

Page 26: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

26

String FunctionsString Functions

• changing case lc(), lcfirst(), uc(), ucfirst()

• length and substrings length()

• Example: $len = length(“Hello”); substr(string, index, length)

• Example: $part = substr(“Hello”, 2, 2);

• chomp() and chop() chop() will remove the last char of the string and

return it• Example: $char = chop (“Hello”);

Page 27: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

27

String FunctionsString 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

Page 28: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

28

Define Your Own Subroutines Define Your Own Subroutines

• You can create a subroutine by placing a group of statements into the following format:

sub subroutine_name { set of statements

} sub subroutine_name ($parameter1, $parameter2,…) {

set of statements }

• For example a outputTableRow subroutinesub outputTableRow { print ‘<TR><TD>One</TD><TD>Two</TD></TR>’;}

Page 29: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

29

Rules to Execute a FunctionRules to Execute a Function

• Function name with parentheses myFunction( argument1, argument2, ... )

• Prepended function symbol, &, function name with parentheses: &myFunction( argument1, argument2, ... ) &outputTableRow(); or &outputTableRow;

• Parentheses may be omitted in certain conditions: myFunction, &myFunction, or myFunction parameter

• Perl's Calling Conventions of Functions http://www.classes.cs.uchicago.edu/classes/archive/2004

/winter/10100-1/02/perl/perl_function_calls.html

Page 30: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

30

Subroutine Example ProgramSubroutine Example Program#!/usr/local/bin/perlprint "Content-type: text/html\n\n";print qq+<html><head><title>Subroutine Example</title></head><body>Here is simple table <br><TABLE BORDER=1>+;&outputTableRow(); &outputTableRow(); &outputTableRow();print qq+</TABLE></body></html>+;

sub outputTableRow { print '<TR><TD>One</TD><TD>Two</TD></TR>';}

Page 31: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

31

Would Output The Following … Would Output The Following …

http://people.cs.uchicago.edu/~hai/hw4/sub1.cgi

Page 32: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

32

Passing Arguments to SubroutinesPassing Arguments to Subroutines

• Generalize a subroutine using input variables (called parameters or arguments).

&outputTableRow( ‘A First Cell’, ‘A Second Cell’ );

• Use a special array variable @_ to access arguments:

$_[0] as the variable name for the first argument,

• $_[0] would be set to ‘A First Cell’ and

$_[1] as the variable name for the second argument,

• $_[1] would be set to ‘A Second Cell’:

$_[n] as the variable name for the nth argument.

Page 33: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

33

Full Subroutine ExampleFull Subroutine Example#!/usr/local/bin/perlprint "Content-type: text/html\n\n";print qq+<html><head><title>Subroutine Example</title></head><body>Here is simple table <br><TABLE BORDER=1>+;for ( $i=1; $i<=3; $i++ ) { &outputTableRow( "Row $i Col 1", "Row $i Col 2");}print qq+</TABLE></body></html>+;

sub outputTableRow { print “<TR><TD>$_[0]</TD><TD>$_[1]</TD></TR>”;}

Page 34: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

34

Would Output The Following ...Would Output The Following ...

http://people.cs.uchicago.edu/~hai/hw4/sub2.cgi

Page 35: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

35

Subroutine Argument PitfallsSubroutine Argument Pitfalls

@array=(1,2,3);

$foo=42;

something($foo, @array);

print “$foo @array\n”;

sub something{$_[0]=13;

$_[2]=4;

print “@_\n”;

}

will print

13 1 4 3

13 1 4 3

@array=(1,2,3);$foo=42;something($foo, @array);print “$foo @array\n”;

sub something{ @copy=@_;$copy[0]=13;$copy[2]=4;print “@copy\n”;

}will print13 1 4 342 1 2 3

Make a private copy of the arguments to Make a private copy of the arguments to keep the original values intactkeep the original values intact

Page 36: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

36

Getting the Number of ArgumentsGetting the Number of Arguments

• There are at least two different ways …

The range operator is set to the last element in a list variable. Therefore, within a subroutine the number of arguments is:

• $numargs = $#_ + 1;

Second, you can use the @_ variable directly. For example,

• $numargs = @_;

Page 37: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

37

Returning Values Returning Values

• Subroutines can return values (can return list) to the calling program. $result = sqrt(144);

• To Return a value within a subroutine:

• Stops the subroutine execution and return the specified value to the calling program.

return( $result );

Scalar or list variable with avalue to return.

Page 38: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

38

Example Subroutine Return Value

Example Subroutine Return Value

$bigger = &calc_bigger( $num1, $num2 );

1. sub calc_bigger {2. # PURPOSE: returns the bigger of 2 numbers3. # ARGUMENTS $[0] – 1st number, $_[1] – 2nd number4. if ( $[0] > $[1] ) { 5. return( $[0] );6. }else { 7. return( $[1] ); 8. }

9.}

Page 39: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

39

ScopeScope

• Global, lexical(private) and dynamic scopes Default function variables are global A variable can be made lexical(private) by preceding

with the keyword mymy $myVar = 23.65;

A variable can be made dynamic by preceding with the keyword locallocal %myHash = (California => "Sacramento", Wisconsin => "Madison");

• Examples: http://people.cs.uchicago.edu/~hai/hw4/scope.cgi

Page 40: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

40

• It is sometimes useful to place sub-routines in external separate files to promote their reuse Other programs can use them without creating

separate copies

• To store in external file:

• 1. Move subroutine lines to a new file. For example, move outputTableRow sub outputTableRow { print '<TR><TD>One</TD><TD>Two</TD></TR>';}

Using Subroutines in External Files

Page 41: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

41

• 2. Place a 1 at the end of the new file. This step provides a return value of 1, which helps Perl recognize that the subroutine executed successfully.

• 3. Name the subroutine file. Usually, this file has a .lib suffix, which indicates that it is a library file of subroutines. For example, you might call the file startdoc.lib.

Using Subroutines in External Files

Page 42: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

42

• 4. Place the file in the same directory as the program file. (Can reside in another directory but For now assume in same.)

• 5. Include an additional require line in the calling program. Before a program can use the subroutine library file, it must add a line that indicates where to look for that file. This line has the following format: require ‘library_filename’;

Using Subroutines in External Files

Page 43: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

43

Example External Subroutine FileExample External Subroutine File

Content of file 'htmlhelper1.lib‘: 1. sub outputTableRow { 2. # PURPOSE: outputs a table row with 2 cols 3. # ARGUMENTS: uses $_[0] for first col 4. # : uses $_[1] for second col 5. print "<TR><TD>$_[0]</TD><TD>$_[1]</TD></TR>"; 6. } 7. sub specialLine { 8. # PURPOSE: Output a line with varible color: 9. # ARGUMENTS: $_[0] is the line to output10. # : $_[1] is the line color.11. print "<B><Font COLOR=$_[1] FACE=\"Helvetica\">

$_[0] </FONT></B>";

12. }

13. 1

Page 44: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

44

Example Calling ProgramExample Calling Program

1. #!/usr/local/bin/perl 2. print "Content-type: text/html\n\n"; 3. require 'htmlhelper1.lib'; 4. …; 5.&specialLine('Here is a simple table', 'RED'); 6.print '<TABLE BORDER=1>'; 7.print '<TH> Num </TH> <TH> Num Cubed </TH>'; 8.for ( $i=0; $i<3; $i++ ) { 9. &outputTableRow($i, $i**3);10.}

11. …;

Page 45: Introduction to Programming the WWW I CMSC 10100-1 Summer 2004 Lecture 12

45

Would Output The Following ...Would Output The Following ...

http://people.cs.uchicago.edu/~hai/hw4/sub3.cgihttp://people.cs.uchicago.edu/~hai/hw4/sub3.cgi