Please send questions to
st10@humboldt.edu .
#!/usr/bin/perl -w
#######################################################
# lect06_filetest1
#
# adding a file test to lect06_filehandle_output2 ---
# if output file doesn't exist, we'll create it, not die;
#
# modified by Sharon Tuttle from "Learning Perl",
# by Schartz and Phoenix
#
# last modified: 9-28-04
#######################################################
# let's say that this reads from a previously-created messages file,
# IF one exists --- otherwise, it creates one!
if (-e "lect06_09_msgs")
{
open PREV_MSGS, "< lect06_09_msgs"
or die "Cannot open lect06_09_msgs: $!";
# show the current contents of this file
while (<PREV_MSGS>)
{
chomp($old_msg = $_);
print "old message from file: $old_msg\n";
}
close PREV_MSGS; # through reading from class8_05_msgs
}
# now I want to open it for appending; note, if DIDN'T exist
# before --- now it will! 8-)
open ALL_MSGS, ">> lect06_09_msgs"
or die "Cannot open lect06_09_msgs for appending: $!";
# add a new message to the end of lect06_09_msgs
print "What message shall be added this time?\n";
chomp($new_msg = <STDIN>);
print ALL_MSGS "$new_msg\n";
close ALL_MSGS; # close appending filehandle
# end of lect06_filetest1