The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

List::Any - Provide the stuff missing in List::Util

SYNOPSIS

    use List::Any qw(any all none notall true false firstidx lastidx);

DESCRIPTION

List::Any provides some trivial but commonly needed functionality on lists which is not going to go into List::Util.

All of the below functions are implementable in one line of Perl code. Using the functions from this module however should give slightly better performance as everything is implemented in C. The pure-Perl implementation of these functions only serves as a fallback in case the C portions of this module couldn't be compiled on this machine.

any BLOCK LIST

Returns a true value if any item in LIST meets the criterion given through BLOCK. Sets $_ for each item in LIST in turn:

    print "At least one value undefined"
        if any { !defined($_) } @list;

Returns false otherwise, or undef if LIST is empty.

all BLOCK LIST

Returns a true value if all items in LIST meet the criterion given through BLOCK. Sets $_ for each item in LIST in turn:

    print "All items defined"
        if all { defined($_) } @list;

Returns false otherwise, or undef if LIST is empty.

none BLOCK LIST

Logically the negation of any. Returns a true value if no item in LIST meets the criterion given through BLOCK. Sets $_ for each item in LIST in turn:

    print "No value defined"
        if none { defined($_) } @list;

Returns false otherwise, or undef if LIST is empty.

notall BLOCK LIST

Logically the negation of all. Returns a true value if not all items in LIST meet the criterion given through BLOCK. Sets $_ for each item in LIST in turn:

    print "Not all values defined"
        if notall { defined($_) } @list;

Returns false otherwise, or undef if LIST is empty.

true BLOCK LIST

Counts the number of elements in LIST for which the criterion in BLOCK is true. Sets $_ for each item in list in turn:

    printf "%i item(s) are defined", true { defined($_) } @list;
false BLOCK LIST

Counts the number of elements in LIST for which the criterion in BLOCK is false. Sets $_ for each item in list in turn:

    printf "%i item(s) are not defined", false { defined($_) } @list;
firstidx BLOCK LIST

Returns the index of the first element in LIST for which the criterion in BLOCK is true. Sets $_ for each item in list in turn:

    my @list = (1, 4, 3, 2, 4, 6);
    printf "item with index %i in list is 4", firstidx { $_ == 4 } @list;
    __END__
    item with index 1 in list is 4
    

Returns -1 if no such item could be found.

lastidx BLOCK LIST

Returns the index of the last element in LIST for which the criterion in BLOCK is true. Sets $_ for each item in list in turn:

    my @list = (1, 4, 3, 2, 4, 6);
    printf "item with index %i in list is 4", lastidx { $_ == 4 } @list;
    __END__
    item with index 4 in list is 4

Returns -1 if no such item could be found.

BUGS

Bad namespace, but List::Util is already taken, quite obviously.

VERSION

This is version 0.02.

SEE ALSO

List::Util

AUTHOR

Tassilo von Parseval, <tassilo.von.parseval@rwth-aachen.de>

COPYRIGHT AND LICENSE

Copyright (C) 2004 by Tassilo von Parseval

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 202:

You forgot a '=back' before '=head1'