Search code examples
perlanysubroutine-prototypes

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.


Solution

  • 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;