Perl : Name "main::IN" used only once, 但实际使用

标签 perl autodie

我编写了一个读取文件的简短 perl 脚本。见 tmp.txt:

1   gene_id "XLOC_000001";  gene_name "DDX11L1";    oId
1   gene_id "XLOC_000001";  gene_name "DDX11L1";    oId
1   gene_id "XLOC_000001";  gene_name "DDX11L1";    oId
1   gene_id "XLOC_000001";  gene_name "DDX11L1";    oId

我的 perl 程序,convert.pl 是:

use warnings;
use strict;
use autodie;        # die if io problem with file
my $line;
my ($xloc, $gene, $ens);
open (IN, "tmp.txt")
    or die ("open 'tmp.txt' failed, $!\n");
while ($line = <IN>) {
    ($xloc, $gene) = ($line =~ /gene_id "([^"]+)".*gene_name "([^"]+)"/);
    print("$xloc   $gene\n");
}
close (IN)
    or warn $! ? "ERROR 1" : "ERROR 2";

它输出:

 Name "main::IN" used only once: possible typo at ./convert.pl line 8.
 XLOC_000001   DDX11L1 
 XLOC_000001   DDX11L1 
 XLOC_000001   DDX11L1
 XLOC_000001   DDX11L1 

我使用了 IN,所以我不明白 Name "main::IN"used... 警告。为什么提示?

最佳答案

这在 BUGS 下有所提及自动死亡部分

"Used only once" warnings can be generated when autodie or Fatal is used with package filehandles (eg, FILE). Scalar filehandles are strongly recommended instead.


使用诊断; 说:

Name "main::IN" used only once: possible typo at test.pl line 9 (#1) (W once) Typographical errors often show up as unique variable names. If you had a good reason for having a unique name, then just mention it again somehow to suppress the message. The our declaration is also provided for this purpose.

NOTE: This warning detects package symbols that have been used only once. This means lexical variables will never trigger this warning. It also means that all of the package variables $c, @c, %c, as well as *c, &c, sub c{}, c(), and c (the filehandle or format) are considered the same; if a program uses $c only once but also uses any of the others it will not trigger this warning. Symbols beginning with an underscore and symbols using special identifiers (q.v. perldata) are exempt from this warning.

所以如果你使用词法文件句柄,它就不会发出警告。

use warnings;
use strict;
use autodie;        # die if io problem with file
use diagnostics;
my $line;
my ($xloc, $gene, $ens);
open (my $in, "<", "tmp.txt")
    or die ("open 'tmp.txt' failed, $!\n");
while ($line = <$in>) {
    ($xloc, $gene) = ($line =~ /gene_id "([^"]+)".*gene_name "([^"]+)"/);
    print("$xloc   $gene\n");
}
close ($in)
    or warn $! ? "ERROR 1" : "ERROR 2";

关于Perl : Name "main::IN" used only once, 但实际使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39958624/

相关文章:

string - Perl:在 "display"上打印并打印到文件中

perl - 如何拦截 perl Test::More 的 BAIL_OUT() 并继续下一个测试?

python - 如何从外部有效 XML 标记中删除垃圾?

bash - awk:如何将常量添加到 M 行中每第 N 行的数字?

perl - Perl 中是否有类似 Ruby gsub 方法的方法?

perl - 如何将 autodie 与非内置函数一起使用?