13

我有一些模块,想为一些子做别名。这是代码:

#!/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 会警告我?我究竟做错了什么?

4

2 回答 2

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

或者

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

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

于 2013-07-09T11:41:13.467 回答
6

你应该输入更多:

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

这样, 的效果no warnings仅减少到有问题的行。

于 2013-07-09T09:02:53.620 回答