List::Conditional - Create lists based on a condition for each element
Version 0.02
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', ... );
clist is automatically exported, as it is the only function in this module.
Arguments: an even number of elements, interpreted as pairs of <condition = value>>
<condition =
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 : ().
if
push
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.
Darko Obradovic, <dobradovic at gmx.de>
<dobradovic at gmx.de>
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.
bug-list-conditional at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc List::Conditional
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=List-Conditional
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/List-Conditional
CPAN Ratings
http://cpanratings.perl.org/d/List-Conditional
Search CPAN
http://search.cpan.org/dist/List-Conditional
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.
To install List::Conditional, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::Conditional
CPAN shell
perl -MCPAN -e shell install List::Conditional
For more information on module installation, please visit the detailed CPAN module installation guide.