Darko Obradovic > List-Conditional-0.02 > List::Conditional

Download:
List-Conditional-0.02.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 0.02   Source  

NAME ^

List::Conditional - Create lists based on a condition for each element

VERSION ^

Version 0.02

SYNOPSIS ^

    use List::Conditional;

        # academic example
        my @list = clist(1 => 'a', 0 => 'b', 1 => 'c');
        # same as @list = ('a', 'c');
        
    # a more practical example
        my @things_to_pack = clist(
                    $destination->is_rainy() => 'umbrella',
                    $destination->is_warm()  => 'shorts',
                    $myself->is_sick()       => 'meds',
                    $flight->duration() > 2  => 'pillow',
                    int $myself->children()  => 'toys',
                    ...
        );

EXPORT ^

clist is automatically exported, as it is the only function in this module.

FUNCTIONS ^

clist

Arguments: an even number of elements, interpreted as pairs of <condition = value>>

Returns: the list of values, for which the associated condition evaluates to true.

This function provides a nice functional and highly readable approach to conditional list building, instead of using imperative control structures like if and push or the ternary operator condition ? value : ().

Beware that all conditions and values are passed to clist in list context, and therefore explicitly enforce scalar context for your conditions and values, as seen in the "SYNOPSIS".

If you want multiple values per condition, you can use List::Flatten as follows:

        use List::Flatten;
        
        my @things_to_pack = flat clist(
                    $destination->is_rainy() => 'umbrella',
                    $destination->is_warm()  => ['shorts', 'sandals'],
                    $myself->is_sick()       => 'meds',
                    $flight->duration() > 2  => 'pillow',
                    int $myself->children()  => ['toys', 'candy'],
                    ...
        );

If you need alternative values in case the conditions do not hold, then the ternary operator is right for you, not this function.

AUTHOR ^

Darko Obradovic, <dobradovic at gmx.de>

BUGS ^

Please report any bugs or feature requests to bug-list-conditional at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Conditional. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc List::Conditional

You can also look for information at:

COPYRIGHT & LICENSE ^

Copyright 2009 Darko Obradovic, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: