Introduction to Programming the WWW I - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Programming the WWW I

Description:

So far programs cannot store data values in-between times when ... AC1002:Hand Saws:150:10. AC1003:Screw Drivers:222:3. 13. Example Program $infile='infile.txt' ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 46
Provided by: robert306
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming the WWW I


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

2
Todays Topics
  • Working with files
  • Subroutines(functions)

3
Working 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

4
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 numbernot
    with , _at_, 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).

5
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 open()
  • infile mydata.txt
  • open (INFILE, infile ) die Cannot open
    infile !

Perl special variable, containing the error string
Connect to mydata.txt.
Execute die only when open fails
Output system message
6
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.

7
File Handles
  • Use the file handle to refer to an opened file
  • Combine with the file handle with the file input
    operator (ltgt) 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 (ltgt) is the same as ltSTDINgt
  • STDOUT -- standard out, usually the monitor
    screen
  • print() function prints to STDOUT by default
  • STDERR -- standard error, usually the monitor
    screen

8
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

9
Reading File into Array
  • infile"mydata.txt"
  • open (INFILE, infile ) die "Cannot
  • open infile !"
  • _at_infile ltINFILEgt
  • print infile0
  • print infile2
  • close (INFILE)
  • Then the output of this program would be
  • Apples are red
  • Carrots are orange
  • http//people.cs.uchicago.edu/hai/hw4/readFile1.c
    gi

10
Reading One Line At a Time
  • Reading a very large file into the list variable
    _at_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.
  • infilemydata.txt
  • open (INFILE, infile ) die Cannot open
    infile !
  • while ( ltINFILEgt )
  • inline _
  • print inline
  • close (INFILE)

Automatically set to the next input line.
http//people.cs.uchicago.edu/hai/hw4/readFile2.c
gi
11
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.uc
    hicago.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/jo
    in.cgi

12
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_nopart_namestock_numberprice
  • AC1000Hammers12212
  • AC1001Wrenches3445
  • AC1002Hand Saws15010
  • AC1003Screw Drivers2223

13
Example Program
  • infile"infile.txt"
  • open (INFILE, infile ) die "Cannot open
  • infile!"
  • while ( ltINFILEgt )
  • 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.c
gi
14
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.
  • Make sure permissions are set correctly (644)

15
Open Modes
  • Three open modes are commonly used to open a
    file
  • read-only (default mode) To explicitly specify
    it, put the character lt before the filename.
  • open(INFILE, ltmyfile.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 gt at the beginning of
    the filename used in the open() function. For
    example,
  • open(INFILE, gtmyfile.txt) die Cannot open
    !

16
Open Modes(contd)
  • 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 gtgt before the filename
    in the open() function.
  • open(OFILE, gtgtmyfile.txt) die Cannot open
    !
  • Example way to write to
  • print OFILE My program was here

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

Change 2 to 8 To unlock the file
Exclusive access to file.
File handle.
http//www.icewalkers.com/Perl/5.8.0/pod/func/floc
k.html
18
Example of flock()
  • outfile"gtgt/home/perlpgm/data/mydata.txt"
  • open (OFILE, outfile ) die "Cannot open
    outfile !"
  • flock( OFILE, 2 )
  • print OFILE "AC1003Screw Drivers2223\n"
  • close (OFILE)
  • Appends the following to part.txt
  • AC1003Screw Drivers2223

19
Reading and Writing Files
  • !/usr/bin/perl
  • print "Content-type text/html\n\n"
  • print qq
  • lthtmlgtltheadgtlttitlegtMy Pagelt/titlegtlt/headgt
  • ltbodygtltFONT SIZE5gtWELCOME TO MY SITE lt/FONTgt
  • ctfile"counter.txt"
  • open (CTFILE, "lt" . ctfile ) die "Cannot open
    infile !"
  • _at_inline ltCTFILEgt
  • countinline0 1
  • close (CTFILE)
  • open (CTFILE, "gtctfile" ) die "Cannot open
    infile !"
  • flock (CTFILE, 2)
  • print CTFILE "count"
  • close (CTFILE)
  • print qq
  • ltbrgtltFONT COLORBLUEgtYou Are Visitor count
    lt/FONTgt
  • lt/bodygtlt/htmlgt

counter.txt permission must be RW
http//people.cs.uchicago.edu/hai/hw4/readWriteFi
le1.cgi
20
Read Directory Content
  • Not much different from reading a file
  • Use opendir(), readdir(), and closedir()
    functions
  • Example
  • dir "/home/hai/html/hw4/"
  • opendir(DIR, dir)
  • _at_content readdir(DIR)
  • foreach entry (_at_content)
  • print entry . "\n"
  • closedir(DIR)

http//people.cs.uchicago.edu/hai/hw4/readDir1.cg
i
21
File 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.ht
mlixABE
22
File Test
  • _at_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.cg
i
23
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

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

25
More 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/ra
    nd.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/lo
    caltime.cgi

26
String 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)

27
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.uc
    hicago.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/jo
    in.cgi

28
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 subroutine
  • sub outputTableRow
  • print ltTRgtltTDgtOnelt/TDgtltTDgtTwolt/TDgtlt/TRgt

29
Rules 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.h
    tml

30
Subroutine Example Program
  • !/usr/local/bin/perl
  • print "Content-type text/html\n\n"
  • print qq
  • lthtmlgt
  • ltheadgt
  • lttitlegtSubroutine Examplelt/titlegt
  • lt/headgt
  • ltbodygt
  • Here is simple table ltbrgt
  • ltTABLE BORDER1gt
  • outputTableRow() outputTableRow()
    outputTableRow()
  • print qq
  • lt/TABLEgt
  • lt/bodygt
  • lt/htmlgt
  • sub outputTableRow

31
Would Output The Following
http//people.cs.uchicago.edu/hai/hw4/sub1.cgi
32
Passing 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 _at__ 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.

33
Full Subroutine Example
  • !/usr/local/bin/perl
  • print "Content-type text/html\n\n"
  • print qq
  • lthtmlgt
  • ltheadgtlttitlegtSubroutine Examplelt/titlegtlt/headgt
  • ltbodygt
  • Here is simple table ltbrgt
  • ltTABLE BORDER1gt
  • for ( i1 ilt3 i )
  • outputTableRow( "Row i Col 1", "Row i Col
    2")
  • print qq
  • lt/TABLEgt
  • lt/bodygtlt/htmlgt
  • sub outputTableRow
  • print ltTRgtltTDgt_0lt/TDgtltTDgt_1lt/TDgtlt/TRgt

34
Would Output The Following ...
http//people.cs.uchicago.edu/hai/hw4/sub2.cgi
35
Subroutine Argument Pitfalls
  • _at_array(1,2,3)
  • foo42
  • something(foo, _at_array)
  • print foo _at_array\n
  • sub something
  • _013
  • _24
  • print _at__\n
  • will print
  • 13 1 4 3
  • 13 1 4 3
  • _at_array(1,2,3)
  • foo42
  • something(foo, _at_array)
  • print foo _at_array\n
  • sub something
  • _at_copy_at__
  • copy013
  • copy24
  • print _at_copy\n
  • will print
  • 13 1 4 3
  • 42 1 2 3

Make a private copy of the arguments to keep the
original values intact
36
Getting 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 _at__ variable directly. For
    example,
  • numargs _at__

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

38
Example Subroutine Return Value
  • bigger calc_bigger( num1, num2 )
  • 1. sub calc_bigger
  • 2. PURPOSE returns the bigger of 2 numbers
  • 3. ARGUMENTS 0 1st number, _1 2nd
    number
  • 4. if ( 0 gt 1 )
  • 5. return( 0 )
  • 6. else
  • 7. return( 1 )
  • 8.
  • 9.

39
Scope
  • Global, lexical(private) and dynamic scopes
  • Default function variables are global
  • A variable can be made lexical(private) by
    preceding with the keyword my
  • my myVar 23.65
  • A variable can be made dynamic by preceding with
    the keyword local
  • local myHash (California gt "Sacramento",
    Wisconsin gt "Madison")
  • Examples http//people.cs.uchicago.edu/hai/hw4/s
    cope.cgi

40
Using Subroutines in External Files
  • 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 'ltTRgtltTDgtOnelt/TDgtltTDgtTwolt/TDgtlt/TRgt'

41
Using Subroutines in External Files
  • 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.

42
Using Subroutines in External Files
  • 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

43
Example 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 "ltTRgtltTDgt_0lt/TDgtltTDgt_1lt/TDgtlt/TRgt
    "
  • 6.
  • 7. sub specialLine
  • 8. PURPOSE Output a line with varible
    color
  • 9. ARGUMENTS _0 is the line to output
  • 10. _1 is the line color.
  • 11. print "ltBgtltFont COLOR_1
    FACE\"Helvetica\"gt _0 lt/FONTgtlt/Bgt"
  • 12.
  • 13. 1

44
Example 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 'ltTABLE BORDER1gt'
  • 7.print 'ltTHgt Num lt/THgt ltTHgt Num Cubed lt/THgt'
  • 8.for ( i0 ilt3 i )
  • 9. outputTableRow(i, i3)
  • 10.
  • 11.

45
Would Output The Following ...
http//people.cs.uchicago.edu/hai/hw4/sub3.cgi
Write a Comment
User Comments (0)
About PowerShow.com