Transcript Slide8,

Files
File Overview
• A file is a sequence of bytes stored on disk.
• Provides a means of stable store.
• Perl has 3 predefined files:
– One for keyboard input.
– One for screen output.
– One for error messages.
• Most languages and systems originating in
the Unix environment have these.
Filehandles
• Files are referenced through special
variables called filehandles.
• A filehandle is bound to an actual file.
• Names of filehandles do not begin with a
special character.
• By convention, filehandles are all uppercase
– Avoid name conflicts with reserved words.
– e.g. STDIN, STDOUT, STDERR.
Opening Files
• open binds a file to a filehandle.
• The function call is:
– open ( filehandle, filename);
• filename is a string expression or a simple
string literal.
• Opened files are implicitly closed before
being reopened and when a program
terminates.
Open (cont.)
• By default, files are opened for reading.
• File name must be preceded by special
characters for other uses.
– > Output, from start of file.
– >> Output, from end of file.
–<
Input (default).
• Examples:
– open (INDAT, “score”);
– open (RESULTS, “>results”);
Open Failures
• open can fail for several reasons:
– File for input does not exist.
– No more filehandles.
• open returns “true” if it succeeds.
• Can check why open fails with die.
– open (INDAT, “score”) or die “Error $!”;
• $! contains error message from the system.
Close
• Some programmers prefer to explicitly
close a file.
• Closing a file can fail too:
– Not enough disk space.
• The function call is:
– close (filehandle);
• Can detect cause of failure with:
– close (RESULTS) or die “Error - $!”;
Reading and Writing Files
• We have read and written to files before:
– Read lines from the keyboard and written to
screen.
• Reading a line from a file is similar:
– <filehandle>
• Writing a line to a file is easy too:
– print filehandle OUTPUT;
An Example
# Open the files.
open (ALLNAMES, “names”) or die “Error: $!”;
open (FIRST, “>firstnames”) or die “Error: $!”;
# Read until EOF.
While (<ALLNAMES>) {
($last, $first, $middle) = split /, /;
print FIRST $first . “\n”;
}
# Close the filehandles.
close (ALLNAMES);
close (FIRST);
Reading Multiple Lines
• read function can read segments longer than
one line.
• Why use read?
• The function call is:
– read (filehandle, buffer, length [, offset]);
• What read returns:
– The number of bytes read.
– 0 when EOF is reached.
– undef if error occurs.
An Example
# Open the files.
open (ALLNAMES, “names”) or die “Error: $!”;
open (FIRST, “>firstnames”) or die “Error: $!”;
$return = read (ALLNAMES, $buf, 60);
while ($return) {
@lines = split /\n/, $buf;
foreach $line (@lines) {
($last, $first, $middle) = split /, /, $line;
print FIRST $first . “\n”;
}
$return = read (ALLNAMES, $buf, 60);
}
# Close the filehandles.
close (ALLNAMES);
close (FIRST);
File Tests
• Sometimes, a program needs to know
information about a file before using it.
– Does a file with the same name exists?
• Perl includes a collection of file tests.
• File tests are unary operators.
• Operand is an expression that evaluates to a
string.
– String must be a file or filehandle.
File Tests (cont.)
• -e True if file exists.
• -r True if file is
readable.
• -w True if file is
writeable.
• -x True if file is
executable.
• -d True if file is a
directory.
• -f True if file is a
regular file.
• -T True if file is a
text file.
• -B True if file is a
binary file.
• -s Length of file in
bytes.
Removing Files
• Files are typically removed through the OS.
• This can be done in Perl with the unlink
function.
• The function call is:
– unlink (filename);
• unlink returns the number of files removed.
Removing Files (cont.)
• unlink can be used to remove a collection of
files.
• For example:
– unlink (<*.doc>);
• To check for problems use:
foreach $nextfile (<*.doc>) {
unlink ($nextfile) or
warn “$nextfile not deleted\n”;
}
Renaming Files
• Files can be renamed with the rename
function.
• The function call is:
– rename (old_file, new_file);
• “True” if it succeeds. “False” otherwise.
• old_file is destroyed if it exists already.