#!/usr/bin/perl -w ####################################################### # class8_03_die_better # # using die (and opening a file!), but now with # the preferred Java usage for open (open or die); # also showing die # with a newline in its message, and how that # keeps the script name and line number from # appearing if the die is executed; # # modified by Sharon Tuttle from "Learning Perl", # by Schartz and Phoenix # # last modified: 5-4-03 ####################################################### # let's say that this is supposed to be called with a single # argument, the number of lines to be appended to LOG; # if the user gives any more or less, we might just decide # to stop right then and there: # # (since this is a user-error, not something that might need # to be debugged, note that we ended the die message with \n) if (@ARGV != 1) { die "expects exactly 1 argument, the number of lines to append\n"; } # if get here --- WAS called with exactly one argument; print "enter a file where log comments should be appended:\n"; chomp ($logname = ); # PREFERRED way to call open: as open or die open LOG, ">> $logname" or die "Cannot create log file $logname: $!"; # if you get here --- $logname WAS opened for appending, # and you could now perform that appending using # filehandle LOG; close LOG; # end of class8_03_die_better