How can Perl read each line of a file?
In Perl, you can use the open function to open a file and use the < operator to read each line of the file. Here is an example code:
open(my $file, '<', 'file.txt') or die "Could not open file: $!";
while (my $line = <$file>) {
chomp $line; # 去除行尾的换行符
# 处理每一行的数据
print $line;
}
close($file);
In the above code, the open function is used to open the file file.txt and assign it to the variable $file. Then, <$file> is used to read each line of the file and assign it to the variable $line. The data in each line can be processed within the while loop. The chomp function is used to remove the newline character at the end of each line. Finally, remember to use the close function to close the file.