1: #!/usr/local/bin/perl运行结果如下:
2:
3: while ($line = <>) {
4: print ($line);
5: if (eof) {
6: print ("-- end of current file --\n");
7: }
8: }
$ program file1 file2下面把eof改为eof(),第二个程序为:
This is a line from the first file.
Here is the last line of the first file.
-- end of current file --
This is a line from the second and last file.
Here is the last line of the last file.
-- end of current file --
$
1: #!/usr/local/bin/perl运行结果如下:
2:
3: while ($line = <>) {
4: print ($line);
5: if (eof()) {
6: print ("-- end of output --\n");
7: }
8: }
$ program file1 file2这时,只有所有文件都读过了,eof()才返回真,如果只是多个文件中前几个的末尾,返回值为假,因为还有要读取的输入。
This is a line from the first file.
Here is the last line of the first file.
This is a line from the second and last file.
Here is the last line of the last file.
-- end of output --$
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/230160/viewspace-623151/ ,如需转载,请注明出处,否则将追究法律责任。