Perl:名称“main::IN"只用过一次,但实际使用过 [英] Perl : Name "main::IN" used only once, but it is actually used

查看:138
本文介绍了Perl:名称“main::IN"只用过一次,但实际使用过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I writing a short perl script that reads in a file. See 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 是:

My perl program, convert.pl is :

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... 警告.为什么要抱怨?

I used IN, so I don't understand the Name "main::IN" used... warning. Why is it complaining?

推荐答案

这在 BUGS 下提到 autodie 部分

This is mentioned under BUGS section of autodie

当 autodie 或 Fatal 与包文件句柄(例如,FILE)一起使用时,可能会生成仅使用一次"警告.强烈建议改用标量文件句柄.

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

<小时>

use diagnostics; 说:

名称main::IN"只使用一次:test.pl 第 9 行(#1)可能出现错别字(W 一次) 排版错误通常显示为唯一的变量名称.如果你有一个独特的名字的充分理由,那么只需提及它再次以某种方式压制消息.我们的声明也是为此目的而提供.

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.

注意:此警告检测仅使用过的包符号一次.这意味着词法变量永远不会触发此警告.这也意味着所有的包变量 $c、@c、%c 以及*c、&c、sub c{}、c() 和 c(文件句柄或格式)被认为是相同的;如果一个程序只使用 $c 一次但也使用了其他它不会触发此警告.以 a 开头的符号使用特殊标识符 (q.v. perldata) 的下划线和符号是免除此警告.

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.

因此,如果您使用词法文件句柄,则它不会发出警告.

So if you use lexical filehandle then it will not warn.

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:名称“main::IN"只用过一次,但实际使用过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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