Files
and I/O
Filehandles:
A
filehandle is the name for the connection between your Perl program
and the operating system. Filehandles follow the same naming
conventions as labels, and occupy their own namespace.
Every
Perl program has three filehandles that are automatically opened for
it: STDIN,
STDOUT,
and STDERR:
STDIN
Standard input (keyboard or file)
STDOUT
Standard output (print and write send output here)
STDERR
Standard error (channel for diagnostic output)
Filehandles
are created using the open() function:
open(FILE,”filename”);
You
can open files for reading, writing, or appending:
open(FILE,”>
newout.dat”) Writing, creating a new file
open(FILE,”>>
oldout.dat”) Appending to existing file
open(FILE,”<
input.dat”) Reading from existing file
As
an aside, under Windows, there are a number of ways to refer to the
full path to a file:
”c:\\temp\\file”
Escape the backslash in double quotes
‘c:\temp\file’
Use proper path in single quotes
“c:/temp/file”
UNIX-style forward slashes
It
is important to realize that calls to the open() function are
not always successful. Perl will not (necessarily) complain about
using a filehandle created from a failed open().
This
is why we test the condition of the open statement:
open(F,”<
badfile.dat”) or die “open: $!”
You
may wish to test for the existence of a file or for certain
properties before opening it.
Fortunately,
there are a number of file test operators available:
File
test Meaning
-e
file File or directory exists
-T
file File is a text file
-w
file File is writable
-r
file File is readable
-s
file File exists and has nonzero length
These
operators are usually used in a conditional expression:
if
(-e myfile.dat) {
open(FILE,”<
myfile.dat”) or die “open: $!\n”;
}
Even
more information can be obtained about a given file using the stat()
function.
Using
filehandles
After
a file has been opened for reading you can read from it using the
diamond operator just as you have already done for STDIN:
$_
= <FILE>; or
while
(<FILE>) {
statements;
}
To
print to your open output file, use the filehandle as the first
argument to the print statement (N.B. no commas between the
filehandle and the string to print).
print
FILE “Look Ma! No hands!\n”;
To
change the default output filehandle from STDOUT to another one, use
select:
select
FILE;
From
this point on, all calls to print or write without a filehandle
argument will result in output being directed to the selected
filehandle. Any special variables related to output will also then
apply to this new default. To change the default back to STDOUT,
select it:
select
STDOUT;
When
you are finished using a filehandle, close it using close():
close(FILE);
Formats:
Perl
has a fairly sophisticated mechanism for generating formatted output.
This involves using pictorial representations of the output, called
templates, and the function write.
Using
a format consists of three operations:
1.
Defining the format (template)
2.
Loading data to be printed into the variable portions of the format
3.
Invoking the format.
Format
templates have the following general form:
format
FORMATNAME =
fieldline
$value_one,
$value_two, …
Everything
between the “=” and the “.” is considered part of the format
and everything (in the fieldlines) will be printed exactly as
it appears (whitespace counts). Fieldlines permit variable
interpolation via fieldholders:
Hi,
my name is @<<<<<, and I’m @< years old. <--
Fieldline
$name,
$age <-- Valueline
Fieldholders
generally begin with a @ and consist of characters indicated
alignment/type.
@<<<
Four character, left-justified field
@>>>
Four character, right-justified field
@|||
Four character, centered field
@###.##
Six character numeric field, with two decimal places
@*
Multi-line field (on line by itself – for blocks of text)
^<<<<
Five character, “filled” field (“chews up” associated
variables)
The
name of the format corresponds to the name of a filehandle. If write
is invoked on a filehandle, then the corresponding format is used.
Naturally then, if you’re printing to standard output, then your
format name should be STDOUT. If you want to use a format with a name
other than that of your desired filehandle, set the $~ variable
to the format name.
There
are special formats which are printed at the top of the page. If the
active format name is FNAME, then the “top” format name is
FNAME_TOP. The special variable $% keeps a count of how many
times the “top” format has been called and can be used to number
pages.
Manipulating
files & directories:
The
action of opening a file for writing creates it. Perl also provides
functions to
manipulate
files without having to ask the operating system to do it.
unlink(filename)
Delete
an existing file. Unlink can take a list of files, or wildcard as an
argument as well: unlink(<*.bak>)
rename(oldname,
newname)
This
function renames a file. It is possible to move files into other
directories by specifying a path as part of the new name. Directories
also have some special function associated with them
mkdir(dirname,
mode)
Create
a new directory. The “mode” specifies the permissions (set this
to 0777 to be safe).
rmdir(dirname)
Removes
(empty) directories
chdir(dirname)
Change
current working directory to dirname, File and directory attributes
can be modified as well:
chmod(permission,
list of files)
Change
the permissions of files or directories:
666
= read and write
444
= read only
777
= read, write, and executable
utime(atime,
mtime, list of files)
Modify
timestamps on files or directories. “atime” is the time of the
most recent access, and “mtime” is the time the file/directory
was last modified.
No comments :
Post a Comment