Please send questions to st10@humboldt.edu .
#!/usr/bin/perl -w

#######################################################
# lect06_filetest2
#
# now will simply create counter file if doesn't exist already
# (adding to lect06_filehandle_output2)
#
# 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 counter file,
# IF one exists --- ELSE it creates one!

# this is big'n'ugly --- let's stick it in a variable
$ctr_file = "/home/st10/counters/lect06_10_counter";

if (-e $ctr_file)
{
    open COUNTER, "< $ctr_file"
       or die "Cannot open $ctr_file: $!";

    chomp($ct = <COUNTER>);

    print "current counter value is: $ct\n";

    close COUNTER; # close reading filehandle
}
else
{
    # if DIDN'T exist === let's consider initial value
    # of $ct to be 0...

    $ct = 0;
}


# ...and now open writing filehandle (which will CREATE file, if
# didn't exist before...)
open COUNTER, "> $ctr_file"
   or die "Cannot open $ctr_file for writing: $!";

$ct++;
print "reset counter to: $ct\n";
print COUNTER "$ct\n";

close COUNTER; # close output filehandle

# end of lect06_filetest2