Taco/Util.pm

filter: Statementstmt 96.0% 24 / 25 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 93.1% 41 / 44 CC 9 SCAR 19.6
CC
9
Cov
92%
filter_struct
9.1
BEGIN
1.0
BEGIN
1.0
CRAP 7.1
↑ Summary Taco/Server.pm »
1 # Taco Perl utility module.
2 # Copyright (C) 2013 Graham Bell
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16  
17 =head1 NAME
18  
19 Alien::Taco::Util - Taco Perl utility module
20  
21 =head1 DESCRIPTION
22  
23 This module contains utility subroutines used to implement the
24 Perl Taco client and server.
25  
26 =cut
27  
28 package Alien::Taco::Util;
29  
30 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 our $VERSION = '0.003';
33  
34 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 our @EXPORT_OK = qw/filter_struct/;
37  
38 =head1 SUBROUTINES
39  
40 =over 4
41  
42 =item filter_struct($ref, $predicate, $function)
43  
44 Walk through the given data structure and replace each entry
45 for which the predicate is true with the result of applying the
46 function to it.
47  
48 =cut
49  
50 6 ▶sub filter_struct {
Subroutine filter_struct: called CC=9 SCAR=22.1
Pod documented
51 6 my $x = shift;
52 6 my $pred = shift;
53 6 my $func = shift;
54  
55 6 my $type = ref $x;
56  
57 6 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 5 1 6
elsif ($type eq 'ARRAY') { } 1 0 1
58 5 foreach my $k (keys %$x) {
59 7 ▶ if ($pred->($x->{$k})) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$pred($x->{$k})) { } 4 3 7
elsif (ref $x->{$k}) { } 2 1 3
60 4 $x->{$k} = $func->($x->{$k});
61 }
62 elsif (ref $x->{$k}) {
63 2 filter_struct($x->{$k}, $pred, $func);
64 }
65 }
66 }
67 elsif ($type eq 'ARRAY') {
68 1 ▶ for (my $i = 0; $i < scalar @$x; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$x) 3 1 4
69 3 ▶ if ($pred->($x->[$i])) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$pred($x->[$i])) { } 1 2 3
elsif (ref $x->[$i]) { } 0 2 2
70 1 $x->[$i] = $func->($x->[$i]);
71 }
72 elsif (ref $x->[$i]) {
73 0 filter_struct($x->[$i], $pred, $func);
74 }
75 }
76 }
77 }
78  
79 4 1;
80  
81 __END__
↑ Summary Taco/Server.pm »