blib/lib/Array/Extract.pm

filter: Statementstmt 100% 37 / 37 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 96.2% 51 / 53 CC 6 SCAR 13.9
CC
6
Cov
96%
extract
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.0
↑ Summary
1 package Array::Extract;
2 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our @EXPORT_OK;
10 1 our $VERSION = "1.00";
11  
12 =head1 NAME
13  
14 Array::Extract - extract element from an array
15  
16 =head1 SYNOPSIS
17  
18 use Array::Extract qw(extract);
19  
20 # remove those members from @members who are
21 # blackballed and store them in @banned
22 my @banned = extract { $_->blackballed } @members;
23  
24 =head1 DESCRIPTION
25  
26 Function to extract elements from an array that match
27 a block. See L<Array::Extract::Example> for a
28 more comprehensive example of how this can be useful
29  
30 =head2 Function
31  
32 The function is exported on demand
33  
34 =over
35  
36 =item extract BLOCK ARRAY
37  
38 Removes elements from the ARRAY that match the
39 block and returns them.
40  
41 # leave just the even numbers in @numbers
42 my @numbers = (1..100);
43 my @odds = extract { $_ % 2 } @numbers;
44  
45 Care is taken to do the least number of splice
46 operations as possible (which can be important when
47 the array is a tied object with a class such as
48 Tie::File)
49  
50 =cut
51  
52 1 ▶sub extract(&\@) {
Subroutine extract: called CC=6 SCAR=17.9
Pod documented
53 1 my $block = shift;
54 1 my $array = shift;
55  
56 # loop invariants. The element we're currently on
57 # and the length of the array
58 1 my $i = 0;
59 1 my $length = @{ $array };
60  
61 # the index we started removing from
62 1 my $remove_from;
63  
64 # what we've collected to return
65 my @return;
66  
67 # for each element of the array
68 1 ▶ while ($i < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 6 1 7
69 6 local $_ = $array->[ $i ];
70 6 ▶ if (!$block->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not &$block()) { } 2 4 6
71 # this content we keep
72 2 ▶ if (defined $remove_from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $remove_from) 2 0 2
73 # but first we need to remove the stuff we wanted
74 # to extract from the list
75 2 my $number = $i - $remove_from;
76 2 $i -= $number;
77 2 $length -= $number;
78 2 splice @{$array}, $remove_from, $number;
79 2 undef $remove_from;
80 }
81 } else {
82 # remember we're going to remove this content
83 4 ▶ $remove_from = $i
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remove_from 3 1 4
84 unless defined $remove_from;
85  
86 # remember the content we were going to keep
87 4 push @return, $_;
88 }
89 6 $i++;
90 }
91  
92 # remove any thing at the end of the list that we were still removing
93 1 ▶ splice @{$array}, $remove_from, $i - $remove_from if defined $remove_from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $remove_from 1 0 1
94 1 return @return;
95 }
96 1 push @EXPORT_OK, "extract";
97  
98 =back
99  
100 =head1 AUTHOR
101  
102 Written by Mark Fowler <mark@twoshortplanks.com>
103  
104 =head1 COPYRIGHT
105  
106 Copyright Mark Fowler 2011. All Rights Reserved.
107  
108 This program is free software; you can redistribute it
109 and/or modify it under the same terms as Perl itself.
110  
111 =head1 BUGS
112  
113 This module (deliberately) does not alias C<$_> to the
114 actual array element within the block
115  
116 Bugs should be reported via this distribution's
117 CPAN RT queue. This can be found at
118 L<https://rt.cpan.org/Dist/Display.html?Array-Extract>
119  
120 You can also address issues by forking this distribution
121 on github and sending pull requests. It can be found at
122 L<http://github.com/2shortplanks/Array-Extract>
123  
124 =head1 SEE ALSO
125  
126 L<Array::Extract::Example> L<List::Util>, L<List::MoreUtils>, L<Tie::File>
127  
128 =cut
129  
130 1 1;
↑ Summary