简体   繁体   English

如何获取Perl中正在读取的文件的名称?

[英]How do I get the name of the file being read in Perl?

In the following Perl pattern: 在以下Perl模式中:

while(<>) {
  # do stuff
}

is there a way to get the name of the file that is presently open? 有没有办法获取目前打开的文件的名称?

Just to be clear, I expect to receive many args, so that loop will process more than one file. 为了清楚起见,我希望收到很多args,这样循环就会处理多个文件。 I want the name of the file presently being processed. 我想要当前正在处理的文件的名称。

It is stored in 它存储在

$ARGV

See perldoc perlvar : perldoc perlvar

  • $ARGV $ ARGV

    contains the name of the current file when reading from <>. 包含从<>读取时当前文件的名称。

However if are piping in from STDIN you will get only '-' 但是,如果从STDIN输入管道,你将只得到' - '

There is also more discussion on the null filehandle in perldoc perlop 关于perldoc perlop中的null文件句柄也有更多的讨论

If you're using linux, you can take a look at the file pointed to by /proc/self/fd/0 . 如果您使用的是linux,则可以查看/proc/self/fd/0指向的文件。

Edit: Answer amended for clarity: The above is useful, but only in cases where input for the perl script is read from stdin . 编辑:为了清楚起见修改了答案:上述内容很有用,但仅限于从stdin读取perl脚本的stdin This can be determined by reading $ARGV , as described in the replies above. 这可以通过阅读$ARGV来确定,如上面的回复中所述。

$ ARGV包含<>中使用的文件的名称。

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

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