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

前端 未结 6 1774
温柔的废话
温柔的废话 2020-12-15 06:08

The List::MoreUtils module indicates that you use the variables $a and $b when supplying the BLOCK that goes with the pairwise

相关标签:
6条回答
  • 2020-12-15 06:15

    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;
    
    0 讨论(0)
  • 2020-12-15 06:15

    I have the same problem with a similar module I'm writing. The only solution I've found (other than using functions that use $a and $b twice, of course) is to put this line somewhere in your code:

    $a = $b; # hack to disable warnings about "main::a" used only once
    

    It basically does nothing, but it does disable the warning. Consider keeping the comment so future maintainers don't have to read your mind.

    0 讨论(0)
  • 2020-12-15 06:16

    Add this near top of your program:

    use vars qw( $a $b );
    

    or, if you don't like the "obsolete" part of perldoc vars, simply add:

    our ( $a, $b );
    
    0 讨论(0)
  • 2020-12-15 06:19

    Yep, it's not you. You can no warnings 'once'; or you can predeclare $a and $b so that they will not be used once anymore.

    our ($a, $b);
    

    does the trick. I tend to prefer that because it doesn't turn off warnings for anything else, and it's a bit more descriptive.

    0 讨论(0)
  • 2020-12-15 06:21

    This is probably a bug in List::Util.

    Turning off warnings globally is probably not a good idea, however you could do something like this:

    {
      no warnings 'once';
      return join("_", @monsters) if @monsters && List::Util::reduce { $a && $b // 0 > 0 } 1,@monsters;
    }
    

    This would turn off the relevant warning category for that part of the code only.

    0 讨论(0)
  • 2020-12-15 06:34

    As of perl 5.19.6, this warning is disabled for all uses of $a and $b everywhere.

    0 讨论(0)
提交回复
热议问题