To write to a file, we use the open command again, this time with a >
01: open (LOGFILE,">logfile.txt") || die "Cannot open file";
02: print LOGFILE "$user at $current_time\n";
03: close(LOGFILE);
Please note the \n at the end of the line, otherwise there will be no line breaks in the file. This will create a new file from scratch if it doesn't exist yet. To append to an exisiting file you use:
01: open (LOGFILE,">>logfile.txt") || die "Cannot open file";
instead. That's it! In our next example we will make a simple webcounter, one that I am actually using myself on my website.