-
True or False. The diamond operator
is used for output processing?
False, the diamond operator <> is for input. Recall that <STDIN> (usually)
takes user input from the keyboard up to and including a new-line character.
-
There are two methods we have discussed up to this
point to produce output, what are they? Show an example
of thier use:
The methods print and printf are used for printing
output. The former allows for interpolation where a variable's name
is used inside the quotes, and the variable's value is substituted
in. The latter uses conversions where a variable's value
is to be substituted in. The conversion provides the formatting
for the value to be substituted in; the value is provided by a
list of expressions (possibly variables) following the quoted
string. For example, a program to calculate a gpa,
such as gpa.plx, might use
print "Your GPA is $gpa.\n";
which would yield output like Your GPA is
3.41176470588235.
or might use
printf "Your GPA is %3.2f.\n", $gpa;
which would yield output like Your GPA is 3.41.
-
True or False. <STDINPUT> is a default file handle?
False, <STDIN> (not <STDINPUT>) is a default file
handle.
-
We have learned four methods to open a file as input or
output, what are they?
When opening a file you can have just the file name, a
< and then the file name, a > and then the file name,
or >> and then the file name as shown below.
open(FILE1, "file1.txt"); #default open for reading
open(FILE2, "<file2.txt"); #open for reading
open(FILE3, ">file3.txt"); #open for writing (overwrites)
open(FILE4, ">>file4.txt"); #open for appending
-
What purpose does the flock() command do?
In a multi-user situation, you may wish to restrict or deny access
to a file for a second user if the first user is already accessing
the file. If both users want to read the file, there's not an issue, but if one or
both users are writing to the file, the integrity of the data is
at stake.
flock() takes two arguments: the first is the filehandle of the
file in question, and the second is a number corresponding to the
degree of access to permit subsequent users of the file.
-
How is the seek() command useful?
The command seek allows one some control over one's
position within certain files. It is typically used with random
access files and allows one for instance to move to the beginning
or end of the file.
-
How can you make sure that your
program does not overwrite an existing file?
The file operator -e determines whether a file of the name
following the operator exists in the directory from which the
program is running. In the example below
the file temp.txt is only open and written to if it does not already exist. If the
file does not exist, it is left alone and not overwritten. $filename="temp.txt";
if( -e $filename)
{
print "You already have a file $filename\n";
}
else
{
print "You don't have a file $filename\n";
print "I'll make you one.\n";
open MYFILE, ">" . $filename;
select MYFILE;
print "Here it is\n"; #printed in file
}
Another possibility is to open a file for appending (see question
4 above).
-
True or False. Back ticks once executed, return
processing back to perl?
True. If you place a command in back ticks (aka back quotes),
then the output of that command can be brought back into your
perl program for further processing. Multi-line
output can be assigned to a scalar variable or to an
array variable. (Learning Perl, p. 200)
-
True or False. The exec function enables you
to call an external program from perl and then continue
processing within perl?
False. The function exec will give control over to
some external process and not return control to perl.
-
What does the stat function do?
The stat function is applied to files. It returns a set of parameters providing
the properties of the file, such as when it was created, when it was last accessed,
how large it is, and so on. (Learning Perl, p. 158)