Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype mismatch: sub main::any: none vs (&@) at Exporter.pm

use Dancer2;
use List::Util qw(any);

sub use_any_subroutine {

    foreach my $foo (@$foo_array) {
        next unless any { $_ == $foo->id } @hello_world;
    }    
    return;
}

there is a conflict with using List::Util and throwing a warning

Prototype mismatch: sub main::any: none vs (&@) at Exporter.pm(...).

i found a solution that is we can use List::Util::any instead of importing it before use,but i want to import it once,so how to avoid this warning

Thanks very much for comments.

like image 918
Shaista Avatar asked Oct 21 '25 05:10

Shaista


2 Answers

There are a few possible solutions

Note that I'm using the core List::Util module here, because it also contains an any function, and shouldn't need installing on most recent releases of perl

  • As you have said yourself, you could fully-qualify the subroutine instead of importing it, and use List::Util::any

  • You could disable the import of any from Dancer2 by using

    use Dancer2 '!any'
    

    which will work as long as you never need Dancer2's any to write your code

  • You could also use first instead of any from List::Util, which returns the first element of the list for which the block returns a true value, and will work fine as long as everything in your list is true

  • Another alternative is to import any from List::Util with a different name

    BEGIN {
        require List::Util;
        *my_any = \&List::Util::any;
    }
    

    and now you have a subroutine my_any that behaves exactly as the original any from List::Util but doesn't clash with the operator of the same name from Dancer2

like image 68
Borodin Avatar answered Oct 24 '25 05:10

Borodin


Both Dancer2 and List::MoreUtils export an any function into your namespace.

For Dancer2, it's part of its DSL and is used as a route definition that matches any HTTP verb on incoming requests.

Defines a route for multiple HTTP methods at once

The List::MoreUtils one is like a grep instead.

Returns a true value if any item in LIST meets the criterion given through BLOCK.

The warning you are seeing is because you have imported the Dancer2 one first, so Perl learned about the prototype of that one. Then you imported the one from List::MoreUtils, which overwrote &main::any in your namespace main::, but the prototype is still there.

You can avoid importing any from Dancer2.

use Dancer2 qw( !any );
use List::MoreUtils qw( any );

get '/' => sub {
    my @foo = ( 1, 2, 3 );
    return List::MoreUtils::any { $_ == 2 } @foo;
};

dance;

Or you can avoid importing any from List::MoreUtils (using List::MoreUtils::any instead of any).

use Dancer2;
use List::MoreUtils qw( );

get '/' => sub {
    my @foo = ( 1, 2, 3 );
    return List::MoreUtils::any { $_ == 2 } @foo;
};

dance;

like image 33
simbabque Avatar answered Oct 24 '25 05:10

simbabque