Data Files CGI/Perl Programming 5

advertisement
5
Data Files
CGI/Perl
Programming
By Diane Zak
1
5
Objectives
• In this chapter, you will:
• Open a file
• Use the die function to exit a script
when a file cannot be opened
• Write records to a file using the print
function
• Read records from a file
• Remove the newline character using the
chomp function
2
5
Objectives
• In this chapter, you will:
• Separate fields using the split function
• Initialize and update counters
• Learn how to use the keys and sort
functions
3
5
Introduction
• Most data from a form should
be saved to a file for future
reference
– Keep track of information
– Calculate statistics
4
The SuperBowl Form
and Acknowledgment
5
5
5
Planning and Coding the
Super Bowl Script
6
5
Saving Data to a file
• Most form data saved to a file is
organized into:
– Fields
• Single item of information about a person,
place, or thing
– Records
• One or more related fields that contain all of
the necessary data about a specific person,
place, or thing
• Data file:
– Collection of related records
7
5
Creating and Opening a File
• open function:
– Used to create and/or open a file
– Syntax:
• open (filehandle, mode, filename)
– filehandle – name assigned to data file, typically
uppercase
– mode – how the data file is to be opened (optional)
– filename – name of file to open or create
Mode
Description
<
- Open existing file for input or reading, default mode
>
- Open file for output
- Creates new file for writing data or write over, if file exists
>>
- Open file for append
8
- Write data to end of existing file or create file if necessary
5
Creating and Opening a File
• open function examples:
• Location of files to create or open if
no path is used:
– UNIX – same directory as script
– Windows – default directory, typically
cgi-bin
• It is good practice to begin
filehandle names with IN or OUT,
depending on the filehandle is used
9
5
Using the die function
• If the open function fails, by
default, the script continues to
run
• die function:
– Displays message and exits script
if there is an error while accessing
a file
– Syntax:
• die message
10
5
Using the die function
• open (filehandle, mode, filename) or
die message;
– Default die message:
• “Died at scriptname line linenumber”
• The default die message will be appended to
the message unless a newline character (\n)
is used
– $! is a special variable that holds the
reason that the open function failed
11
5
Using the die function
12
5
Writing Records to a File
• Can use print to write to the
filehandle representing an open file
– print filehandle data\n;
• Typically, each record in a datafile
is written to a separate line
13
5
Writing Records to a File
• If a record contains more than 1
field, then a field separator
(delimiter) is used:
– Examples:
•
•
•
•
•
comma (,)
colon (:)
ampersand (&)
tab (\t)
pipe (|)
– Field separator must not be part of the
data in the fields
• Otherwise, when reading the data from the
file, there will be problems confusing the
separator in the data and the actual field
separator
14
5
Writing Records to a File
15
5
Closing a File
• To make sure that no data is lost,
any opened files should be closed
before the script ends
• Syntax:
– close (filehandle);
• A file is automatically closed when
the script ends, but it is good
practice to close the file when
finished using it
16
Modifying the
SuperBowl script
5
• The file survey.txt is
opened for appending
data, to the OUTFILE
filehandle
• Each record is written
to OUTFILE using a
comma as a field
separator
• OUTFILE is closed
when the print
command has
17
completed
Reading Records
into an Array
5
• Syntax:
– arrayname = <filehandle>;
– Reads records from a file into an array
– <> = angle operator
– Angle operator instructs to read record
from filehandle and store each in a
scalar variable in arrayname
• Example:
– @records = <INFILE>;
18
5
Calculating Survey Statistics
for the SuperBowl page
• @records array is used
• INFILE filehandle is used
19
5
More on foreach
• Can declare the
loop variable in
the foreach
statement
– The variable can
be used within the
loop, and will be
removed from
memory when the
loop has
completed running
20
Removing the Newline
Character
5
• Chomp function can be used to
remove the newline character
(\n) from the end of a record
• Syntax:
– chomp (variable);
• Example:
– chomp ($rec);
21
5
Using the split function
• The split function can be used to
divide a string of characters into
separate parts based on a pattern
• Syntax:
– split (/pattern/, string);
• Example:
– split (/,/, “John,Jane”);
• Splits the string “John,Jane” into 2 parts –
John and Jane
22
5
Using the split function
23
5
Using Counters
• Counter:
– variable for counting
– 2 tasks associated with counter:
• Initializing
– Assigning a beginning value to the counter
• Updating
– Incrementing or adding a number to the value
in the counter
– Incremented by a constant value, typically 1
• Accumulator:
– Used for adding numbers together, like
counter, except an accumulator can be
incremented by a varying amount
24
5
Updating the SuperBowl Script
• split is used to
split on the field
separator of a
comma (,)
• chomp is used
to remove
newlines from
$rec
• An array and
hash of
counters are
used
25
The keys and sort
functions
5
• keys function:
– Returns an unordered list of keys stored
in a hash
– Syntax:
• keys (hash);
• values function:
– A similar function to keys which returns
an unordered list of values stored in a
hash
– Syntax:
• values (hash);
26
The keys and sort
functions
5
• sort function:
– temporarily sorts a list of values
– Syntax:
• sort (list);
– Can be used to sort a commaseparated list of values, array, or
keys of a hash
27
The keys and sort
functions
5
• keys and sort
can be used
together, as in
Example 3
– sort function
will alphabetize
the keys in a
hash
28
Completed SuperBowl
Script
5
29
Completed SuperBowl
Script
5
30
5
Summary
• A field is a single item of information
about a person, place, or thing.
• A record is one or more related fields
containing all necessary data about a
specific person, place, or thing.
• Data file = collection of related records.
• open function can be used to create
and/or open a data file.
– Syntax: open (filehandle, mode, filename);
• Modes: < = input, > = output, >> = append
31
5
Summary
• die function can be used to display a
message and exit a script if an error occurs
with the open function
– Syntax: die message;
– Actual error message during a failed open function
is stored in $! variable
• To write a record to an open file:
– Syntax: print filehandle data\n;
– Use field separator if there are multiple fields per
record
• close function is used to close an open file.
– Syntax: close (filehandle);
32
Summary
5
• Use the angle operator (<>) to read records
from a file into an array.
– Syntax: arrayname = <filehandle>;
• chomp function can be used to remove newline
characters from the end of records.
– Syntax: chomp (variable);
• split function can be used to divide a string
into separate parts based on a pattern.
– Syntax: split (/pattern/, string);
– The values can be returned to an array, hash, or
comma-separated list of scalar variables
33
Summary
5
• Counter and accumulator variables:
– Counter is for counting numbers.
– An accumulator is used to add together numbers.
– Both must be initialized and updated.
• The keys function can be used to return an
unordered list of keys stored in a hash.
– Syntax: keys (hash);
• The values function can be used to return an
unordered list of values stored in a hash.
– Syntax: values (hash);
• The sort function can be used to temporarily
sort values in a list, in alphabetical order.
– Syntax: sort (list);
34
Download