We have 2 ways on how to write to a file in PERL.
The script below will create and write to a file overwritten the data inside the file. The script will automatically creates a file if it doesn't exist. An error will return if there's a file permission denied or directory path doesn't exist.
open FHFILE, "> file1.txt" or die "file can't be created: $!";
print FHFILE "test data\n";
close FHFILE;
The script below will create and append to a file. Same with #1, it automatically creates a file and an error will return if there's a file permission denied or directory path doesn't exist.
open FHFILE, ">> file1.txt" or die "file can't be created: $!";
print FHFILE "test data\n";
close FHFILE;
The script below will read the file then pass the data into an array. An error will return if file is not readable.
open FHFILE, "< file1.txt" or die "error reading file: $!";
@arrData = <FHFILE>;
close FHFILE;
No comments:
Post a Comment