Unformatted text preview:

Introduction to Programming the WWW I CMSC 10100 1 Summer 2004 Lecture 12 Today s Topics Working with files Subroutines functions 2 Working with Files So far programs cannot store data values inbetween 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 3 Using the open Function Use to connect a program to a physical file on a Web server It has the following format open INFILE mydata txt Filehandle A name used to Filename The name of the refer to the file in your file on the web server pr ogr a m 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 4 More On open function open returns 1 true when it successfully opens and returns 0 false when this attempt fails A common way to use Perl special containing open variable the error string infile mydata txt open INFILE infile die Cannot open infile Connect to mydata txt Execute die only when open fails Output system message 5 Specifying Filenames So far need to keep file in same directory You can specify a full directory path name for the file to be opened The name of the file infile home perlpgm per pgm www cgi bin C7 infile txt My home Directories potentially filesystem viewable on the Internet on my fileserver 6 File 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 7 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 red Bananas are yellow Carrots are orange Dates are brown 8 Reading File into Array infile mydata txt open INFILE infile die Cannot open infile infile INFILE print infile 0 print infile 2 close INFILE Then the output of this program would be Apples are red Carrots are orange http people cs uchicago edu hai hw4 readFile1 cgi 9 Reading 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 Automatically set inline to the next input print inline line close INFILE http people cs uchicago edu hai hw4 readFile2 cgi 10 Two 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 11 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 12 AC1001 Wrenches 344 5 AC1002 Hand Saws 150 10 AC1003 Screw Drivers 222 3 12 Example 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 13 Using 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 Name of my file infile home perlpgm data mydata txt My home filesystem on my web server A directory created for my data files Make sure permissions are set correctly 644 14 Open 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 15 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 16 Locking 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 File handle Change 2 to 8 To unlock the file Exclusive access to file http www icewalkers com Perl 5 8 0 pod func flock html 17 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 18 Reading and Writing Files usr bin perl print Content type text html n n print qq html head title My Page title head body FONT SIZE 5 WELCOME TO MY SITE FONT counter txt permission ctfile counter txt must RW open CTFILE ctfile die Cannot openbe 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 http people cs uchicago edu hai hw4 readWriteFile1 cgi 19 Read Directory Content Not much different from reading a file Use opendir readdir and closedir


View Full Document

UChicago CMSC 10100 - Introduction to Programming the WWW I

Loading Unlocking...
Login

Join to view Introduction to Programming the WWW I and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Programming the WWW I and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?