List::Conditional - Create lists based on a condition for each element
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>>
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
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.
<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.
You can find documentation for this module with the perldoc command.
You can also look for information at:
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.