Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

perl - How do I prevent List::MoreUtils from warning about using $a and $b only once?

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?

Update:

See the answer by Ether for perl v5.19.6 and beyond: problem solved.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

Explicitly specifying the package will suppress the warning too. If you're in main,

my @sums = pairwise { $::a + $::b } @x, @y;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...