Write File
2002-07-15 11:15:15
Category: perl:files
Description: Write over top of a file or append to a file in perl.
Author: detour
Viewed: 7238
Rating: (45 votes)


#!/usr/bin/perl
#
# writefile.pl by detour@metalshell.com
#
# Write output to a file.
#
# http://www.metalshell.com/
 
use strict;
 
# Open file to be overwritten using the single greater then char
open(OUTFILE, ">tmp.out");
print OUTFILE "This line will be the first line in tmp.out\n";
print OUTFILE "Any old data in tmp.out has been overwritten\n";
print OUTFILE "--------------------------------------------\n";
close(OUTFILE);
 
# Open file for append using the double greater then char's
open(OUTFILE, ">>tmp.out");
print OUTFILE "This line will be appended to tmp.out\n";
close(OUTFILE);
 
# Open the file to print out what is inside.
open(INFILE, "tmp.out");
while(<INFILE>) {
  print $_;
}
close(INFILE);