#mode operand Create truncate
#read <
#write > Yes
#append >> Yes
Case 1:throw A exception if you cannot open the file:
Copy Code code as follows:
Use strict;
Use warnings;
My $filename = ' data.txt ';
Open (My $fh, ' <:encoding (UTF-8) ', $filename)
Or die "Could not open file ' $filename ' with the error $!";
while (My $row = < $fh >) {
Chomp $row;
print "$row \ n";
}
Close ($FH);
Case 2:give A warning if you cannot open the file, but keep running:
Copy Code code as follows:
Use strict;
Use warnings;
My $filename = ' data.txt ';
if (open (My $fh, ' <:encoding (UTF-8) ', $filename)) {
while (My $row = < $fh >) {
Chomp $row;
print "$row \ n";
}
Close ($FH);
} else {
Warn "Could not open file ' $filename ' $!";
}
Case 3:read one file into array
Copy Code code as follows:
Use strict;
Use warnings;
My $filename = ' data.txt ';
Open (Filein, "<", $filename)
Or die "Could not open file ' $filename ' with the error $!";
My @FileContents = <FILEIN>;
For my $l (@FileContents) {
print "$l \ n";
}
Close Filein;
End