在别名子程序时获取"...仅使用一次:可能的拼写错误"警告

Sui*_*uic 10 perl

我有一些模块,并希望为某些sub制作别名.这是代码:

#!/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;
Run Code Online (Sandbox Code Playgroud)

它有效,但它会产生编译警告

名称"MySub :: other"仅使用一次:/tmp/MySub.pm第23行可能输入错误.

我知道我可以输入no warnings 'once';,但这是唯一的解决方案吗?为什么Perl警告我?我究竟做错了什么?

cho*_*oba 6

你应该输入更多:

{   no warnings 'once';
    *other = \&do_some;
}
Run Code Online (Sandbox Code Playgroud)

这样,效果no warnings仅降低到有问题的线.


ike*_*ami 6

{
   no warnings 'once';
   *other = \&do_some;
}
Run Code Online (Sandbox Code Playgroud)

要么

*other = \&do_some;
*other if 0;  # Prevent spurious warning
Run Code Online (Sandbox Code Playgroud)

我更喜欢后者.对于初学者,它只会禁用您要禁用的警告实例.此外,如果您删除其中一行并忘记删除另一行,则另一行将开始警告.完善!