该List::MoreUtils模块表示您使用变量,$a并$b在提供该变量时BLOCK使用该pairwise功能.例如:
use strict;
use warnings;
use List::MoreUtils qw'pairwise';
my @x = ( 1 .. 5);
my @y = (11 .. 15);
my @sums = pairwise { $a + $b } @x, @y;
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我得到这样的警告:
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.
有一种优雅的方式来处理这个问题吗?
更新:
在Mastering Perl的其中一章中,brian d foy从List :: Util中显示了这个片段:
sub reduce(&@) {
my $code = shift;
no strict "refs";
return shift unless @_ > 1;
use vars qw($a $b);
my $caller = caller;
local(*{$caller . "::a"}) = \my $a;
local(*{$caller . "::b"}) = \my $b;
$a = shift;
foreach(@_) {
$b = $_;
$a = &{$code}();
}
$a;
}
Run Code Online (Sandbox Code Playgroud)
我不明白这条use vars qw($a $b)线的意义.即使我评论它,我得到相同的输出和警告.
perl ×2