Perl的菱形运算符(空文件句柄)当前正在读取哪个文件? [英] Which file is Perl's diamond operator (null file handle) currently reading from?

查看:122
本文介绍了Perl的菱形运算符(空文件句柄)当前正在读取哪个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Perl的 diamond <>运算符来读取命令行上指定的文件.

I'm using Perl's diamond <> operator to read from files specified on the command line.

我希望能够报告诸如"Trouble on line $. of file $FILENAME"之类的消息,但是如何确定菱形当前正在使用哪个文件?

I'd like to be able to report messages like "Trouble on line $. of file $FILENAME", but how can I tell which file is currently used by the diamond?

推荐答案

请参见 perlvar :

    $ARGV

Contains the name of the current file when reading from <> .

但是在perlvar中也要考虑$..如果使用perl -n进行此操作,则可能无法达到所需的效果,因为在perl -n用例中,计数器未重置.

But also consider $. in perlvar. If you do this with perl -n it might not turn out the way you want it, because the counter is not reset in the perl -n use case.

$.

最后访问的文件句柄的当前行号.

Current line number for the last filehandle accessed.

Perl中的每个文件句柄都会计算已读取的行数 从中. (取决于$/的值,Perl的想法是 构成一行可能与您的不匹配.)当从 文件句柄(通过readline()<>),或在调用tell()seek()时 在其上,$.成为该文件句柄的行计数器的别名.

Each filehandle in Perl counts the number of lines that have been read from it. (Depending on the value of $/ , Perl's idea of what constitutes a line may not match yours.) When a line is read from a filehandle (via readline() or <> ), or when tell() or seek() is called on it, $. becomes an alias to the line counter for that filehandle.

您可以通过分配给$.来调整计数器,但这不会 实际移动寻找指针.本地化$.不会本地化 filehandle的行数.相反,它将本地化​​perl的概念 当前$.是哪个文件句柄的别名.

You can adjust the counter by assigning to $. , but this will not actually move the seek pointer. Localizing $. will not localize the filehandle's line count. Instead, it will localize perl's notion of which filehandle $. is currently aliased to.

$.在关闭文件句柄时重置,但在打开时不重置 重新打开文件句柄,而没有插入close().欲了解更多 详细信息,请参见perlop中的I/O操作员.因为<>从不执行 显式关闭,行号在ARGV文件中增加(但请参见 eof中的示例).

$. is reset when the filehandle is closed, but not when an open filehandle is reopened without an intervening close(). For more details, see I/O Operators in perlop. Because <> never does an explicit close, line numbers increase across ARGV files (but see examples in eof).

您也可以使用HANDLE->input_line_number(EXPR)访问该行 给定文件句柄的计数器,而不必担心哪个 处理您上次访问的时间.

You can also use HANDLE->input_line_number(EXPR) to access the line counter for a given filehandle without having to worry about which handle you last accessed.

助记符:许多程序使用."表示当前行号.

Mnemonic: many programs use "." to mean the current line number.

这是一个例子:

$ perl -nE 'say "$., $ARGV";' foo.pl bar.pl
1, foo.pl
2, foo.pl
3, foo.pl
4, foo.pl
5, foo.pl
6, foo.pl
7, foo.pl
8, foo.pl
9, foo.pl
10, foo.pl
11, foo.pl
12, foo.pl
13, bar.pl
14, bar.pl
15, bar.pl

如果要重置它,则需要在读取循环结束时检查eof(感谢@Borodin ).另请参见针对eof 的perldoc:

If you want it to reset, you need to check for eof at the end of your read loop (thanks @Borodin). Also see the perldoc for eof:

$ perl -nE 'say "$., $ARGV"; close ARGV if eof' foo.pl bar.pl

这篇关于Perl的菱形运算符(空文件句柄)当前正在读取哪个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆