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

时间:2013-07-09 08:53:47

标签: 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;

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

  

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

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

2 个答案:

答案 0 :(得分:6)

您应该输入更多内容:

{   no warnings 'once';
    *other = \&do_some;
}

这样,no warnings的效果只会降低到有问题的一行。

答案 1 :(得分:6)

{
   no warnings 'once';
   *other = \&do_some;
}

*other = \&do_some;
*other if 0;  # Prevent spurious warning

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

相关问题