简体   繁体   English

perl使用$ _读取文件中的每一行不起作用

[英]perl reading each line in a file using $_ not working

I am reading 'Beginning Perl: Online book by Simon Cozen'. 我正在阅读“ Perl入门:Simon Cozen撰写的在线图书”。 In my code below, print comment prints all lines of my data.txt file. 在下面的代码中,打印注释将打印我的data.txt文件的所有行。 But my while loop does not print anything. 但是我的while循环不输出任何内容。 Please help me fix this problem. 请帮助我解决此问题。

open(FILE,'data.txt');
my $line = <FILE>;
my @lines = <FILE>;
print @lines;

while (<FILE>) {
    print "$_";
}

Since you've read the file and now you're "at the end", you need to close the file and open it again to get back to the start. 由于您已经阅读了文件,而现在您已经“结束”了,因此您需要关闭文件并再次打开它以重新开始。

Alternatively, use seek FILE, 0, SEEK_SET; 或者,使用seek FILE, 0, SEEK_SET; on the open file to reset back to the start without a close and reopen. 在打开的文件上重新设置,无需重新关闭即可重新开始。 (Use use Fcntl qw( SEEK_SET ); to import SEEK_SET .) (使用use Fcntl qw( SEEK_SET );导入SEEK_SET 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM