Perl - Files

Write to a file

# Create the file handle = FILE.  The ">" opens the file for writing, or if the file
# does not exist it will create the file 
open(FILE, ">myfile.txt") or die "Could not open file";
# Write to the file handle
print FILE "Some text for my file\n";

Read file

# Create file handle
open(FILE, "C:\myfiles\somefile.txt") or die "Could not open file\n";
# Put contents of file into an array
@myarray = <FILE>;
# Or print the contents of the file
while(<FILE>) {
    print "$_\n";
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License