blib/lib/List/Conditional.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 38 / 38 CC 4 SCAR 8.5
CC
4
Cov
100%
clist
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.3
↑ Summary
1 package List::Conditional;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 require Exporter;
7 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our @EXPORT = qw(clist);
9  
10 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 =head1 NAME
15  
16 List::Conditional - Create lists based on a condition for each element
17  
18  
19 =head1 VERSION
20  
21 Version 0.02
22  
23 =cut
24  
25 3 our $VERSION = '0.02';
26  
27  
28 =head1 SYNOPSIS
29  
30 use List::Conditional;
31  
32 # academic example
33 my @list = clist(1 => 'a', 0 => 'b', 1 => 'c');
34 # same as @list = ('a', 'c');
35
36 # a more practical example
37 my @things_to_pack = clist(
38 $destination->is_rainy() => 'umbrella',
39 $destination->is_warm() => 'shorts',
40 $myself->is_sick() => 'meds',
41 $flight->duration() > 2 => 'pillow',
42 int $myself->children() => 'toys',
43 ...
44 );
45
46
47 =head1 EXPORT
48  
49 B<clist> is automatically exported, as it is the only function in this module.
50  
51  
52 =head1 FUNCTIONS
53  
54 =head2 clist
55  
56 B<Arguments:> an even number of elements, interpreted as pairs of C<<condition => value>>
57  
58 B<Returns:> the list of values, for which the associated condition evaluates to true.
59  
60 This function provides a nice functional and highly readable approach to conditional list building,
61 instead of using imperative control structures like C<if> and C<push>
62 or the ternary operator C<condition ? value : ()>.
63  
64 Beware that all conditions and values are passed to B<clist> in list context,
65 and therefore explicitly enforce scalar context for your conditions and values,
66 as seen in the L</"SYNOPSIS">.
67  
68 If you want multiple values per condition, you can use L<List::Flatten> as follows:
69  
70 use List::Flatten;
71
72 my @things_to_pack = flat clist(
73 $destination->is_rainy() => 'umbrella',
74 $destination->is_warm() => ['shorts', 'sandals'],
75 $myself->is_sick() => 'meds',
76 $flight->duration() > 2 => 'pillow',
77 int $myself->children() => ['toys', 'candy'],
78 ...
79 );
80  
81 If you need alternative values in case the conditions do not hold,
82 then the ternary operator is right for you, not this function.
83  
84 =cut
85  
86 2 ▶sub clist {
Subroutine clist: called CC=4 SCAR=13.9
Pod documented
87 2 ▶ croak "odd number of elements passed to clist, requires (condition => value)-pairs!" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 1 2
88 1 my @result = ();
89 1 my $it = natatime 2, @_;
90 1 ▶ while (my ($condition, $element) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 3 1 4
91 3 ▶ push @result, $element if $condition;
Branch: 1 branch
BranchTrueFalseTotal
if $condition 2 1 3
92 }
93 1 return @result;
94 }
95  
96  
97 =head1 AUTHOR
98  
99 Darko Obradovic, C<< <dobradovic at gmx.de> >>
100  
101  
102 =head1 BUGS
103  
104 Please report any bugs or feature requests to C<bug-list-conditional at rt.cpan.org>, or through
105 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Conditional>. I will be notified, and then you'll
106 automatically be notified of progress on your bug as I make changes.
107  
108  
109 =head1 SUPPORT
110  
111 You can find documentation for this module with the perldoc command.
112  
113 perldoc List::Conditional
114  
115  
116 You can also look for information at:
117  
118 =over 4
119  
120 =item * RT: CPAN's request tracker
121  
122 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=List-Conditional>
123  
124 =item * AnnoCPAN: Annotated CPAN documentation
125  
126 L<http://annocpan.org/dist/List-Conditional>
127  
128 =item * CPAN Ratings
129  
130 L<http://cpanratings.perl.org/d/List-Conditional>
131  
132 =item * Search CPAN
133  
134 L<http://search.cpan.org/dist/List-Conditional>
135  
136 =back
137  
138  
139 =head1 COPYRIGHT & LICENSE
140  
141 Copyright 2009 Darko Obradovic, all rights reserved.
142  
143 This program is free software; you can redistribute it and/or modify it
144 under the same terms as Perl itself.
145  
146 =cut
147  
148 3 1;
↑ Summary