如何防止List :: MoreUtils警告仅使用$ a和$ b一次? [英] How do I prevent List::MoreUtils from warning about using $a and $b only once?
问题描述
List::MoreUtils
模块指示您使用了变量$a
提供与pairwise
功能一起提供的BLOCK
时的$b
和$b
.例如:
The List::MoreUtils
module indicates that you use the variables $a
and $b
when supplying the BLOCK
that goes with the pairwise
function. For example:
use strict;
use warnings;
use List::MoreUtils qw'pairwise';
my @x = ( 1 .. 5);
my @y = (11 .. 15);
my @sums = pairwise { $a + $b } @x, @y;
但是当我这样做时,我会收到如下警告:
But when I do that, I get warnings like this:
Name "main::b" used only once: possible typo at try.pl line 7.
Name "main::a" used only once: possible typo at try.pl line 7.
有没有一种优雅的方法来解决这个问题?
Is there an elegant way to deal with this problem?
更新:
有关Perl v5.19.6及更高版本,请参见以太坊的答案:问题已解决.
See the answer by Ether for perl v5.19.6 and beyond: problem solved.
推荐答案
取决于您认为优雅的东西.
Depends on what you consider elegant.
no warnings qw(once);
our ($a, $b);
这两个就足够了.您甚至可以很容易地限制它们的范围.
One of these two will suffice. You can even limit their scope pretty easily.
my @sums = pairwise { no warnings qw(once); $a + $b } @x, @y;
my @sums = pairwise { our $a + our $b } @x, @y;
明确指定软件包也会抑制该警告.如果您在main
,
Explicitly specifying the package will suppress the warning too. If you're in main
,
my @sums = pairwise { $::a + $::b } @x, @y;
这篇关于如何防止List :: MoreUtils警告仅使用$ a和$ b一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!