#!/usr/bin/perl -w ####################################################### # class8_09_filetest1 # # adding a file test to class8_07_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: 5-5-03 ####################################################### # let's say that this reads from a previously-created messages file, # IF one exists --- otherwise, it creates one! if (-e "class8_09_msgs") { open PREV_MSGS, "< class8_09_msgs" or die "Cannot open class8_09_msgs: $!"; # show the current contents of this file while () { 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, ">> class8_09_msgs" or die "Cannot open class8_09_msgs for appending: $!"; # add a new message to the end of class8_09_msgs print "What message shall be added this time?\n"; chomp($new_msg = ); print ALL_MSGS "$new_msg\n"; close ALL_MSGS; # close appending filehandle # end of class8_09_filetest1