获得“......只使用一次:可能的错字"别名子程序时的警告 [英] Getting "... used only once: possible typo" warning when aliasing subroutines
问题描述
我有一些模块,想为一些子模块做别名.代码如下:
I have some module, and want to make alias for some sub. Here is the code:
#!/usr/bin/perl
package MySub;
use strict;
use warnings;
sub new {
my $class = shift;
my $params = shift;
my $self = {};
bless( $self, $class );
return $self;
}
sub do_some {
my $self = shift;
print "Do something!";
return 1;
}
*other = \&do_some;
1;
它可以工作,但会产生编译警告
It works, but it produces a compile warning
名称MySub::other"仅使用一次:/tmp/MySub.pm 第 23 行可能有错别字.
Name "MySub::other" used only once: possible typo at /tmp/MySub.pm line 23.
我知道我可以只输入no warnings 'once';
,但这是唯一的解决方案吗?为什么 Perl 会警告我?我做错了什么?
I know that I can just type no warnings 'once';
, but is this the only solution? Why is Perl warning me? What am I doing wrong?
推荐答案
{
no warnings 'once';
*other = \&do_some;
}
或
*other = \&do_some;
*other if 0; # Prevent spurious warning
我更喜欢后者.对于初学者来说,它只会禁用您希望禁用的警告实例.此外,如果您删除其中一行而忘记删除另一行,另一行将开始警告.完美!
I prefer the latter. For starters, it will only disable the instance of the warning you wish to disable. Also, if you remove one of the lines and forget to remove the other, the other will start warning. Perfect!
这篇关于获得“......只使用一次:可能的错字"别名子程序时的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!