FFRANK has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

Using PDL, there is a conflict with using List::MoreUtils (any) because PDL has already an "any". Prototype mismatch: sub main::any: none vs (&@) at Exporter.pm(...).

Eg: catch $d where any $c (@c) $a is defined at index $c.

#!/usr/bin/perl -w use strict; use PDL; use List::MoreUtils qw(any); my @a; $a[5] = 1; my @b = (0..8); LOOK: for my $d (0..2) { my @c = @b[$d*3..$d*3+2]; if (any { defined $a[$_] } @c) { print "d:$d\n" and last LOOK; } else {();} }
The any sub in List::MoreUtils looks like:

sub any (&@) { my $f = shift; return if ! @_; for (@_) { return 1 if $f->(); } return 0; }
But adding this sub returns an error message. Can't find the way to modify the sub...

Also, is this subroutine optimal (does it looks all elements in the array or does it stops rightaway when the "any" condition is met) ?

Thanks very much for comments and hints, best.

FFrank

Replies are listed 'Best First'.
Re: sub any
by kyle (Abbot) on Oct 31, 2007 at 18:24 UTC

    You could still use the List::MoreUtils 'any' by not importing it and calling it with the package name.

    use List::MoreUtils; # no import list! # ... if (List::MoreUtils::any { defined $a[$_] } @c) {

    And yes, the List::MoreUtils::any does stop processing the list as soon as the condition is met.

Re: sub any
by ikegami (Patriarch) on Nov 01, 2007 at 06:31 UTC

    You could also manually import it to another name

    use List::MoreUtils qw( ); BEGIN { *any_of = \&List::MoreUtils::any; } if (any_of ...) { ... }
      When I got this error I found that List::MoreUtils::any() was clashing with Test::Deep::any()... Which is a shame because Test::Deep::any() is designed to be be used only inside cmp_deeply() as a special comparison tool.