blib/lib/Zoidberg/Utils.pm

filter: Statementstmt 79.5% 66 / 83 Branchbran 43.1% 19 / 44 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod 40.0% 2 / 5 totaltotal 65.8% 102 / 155 CC 29 SCAR 21.7
CC
29
Cov
66%
read_file
22.6
pd_read
12.0
read_data_file
6.4
CRAP 8.7
« blib/lib/Zoidberg/Shell.pm ↑ Summary blib/lib/Zoidberg/DispatchTable.pm »
1  
2 package Zoidberg::Utils;
3  
4 67 our $VERSION = '0.981';
5  
6 128 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 128 ▶use vars '$AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 67 ▶use Zoidberg::Utils::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use Exporter::Tidy
10 67 ▶ default => [qw/:output :error/],
Subroutine BEGIN: called CC=1 SCAR=0.0
11 output => [qw/output message debug/],
12 error => [qw/error bug todo complain/],
13 fs => [qw/path list_dir/],
14 other => [qw/
15 setting read_data_file read_file merge_hash
16 complain typed_output output_is_captured
17 list_path unique_file regex_glob
18 getopt help usage version path2hashref
19 67 /] ;
20  
21 67 our $ERROR_CALLER = 1;
22  
23 67 our %loadable = (
24 fs => ['Zoidberg::Utils::FileSystem', qw/path list_dir list_path unique_file regex_glob/ ],
25 output => ['Zoidberg::Utils::Output', qw/output message debug complain typed_output output_is_captured/ ],
26 getopt => ['Zoidberg::Utils::GetOpt', qw/getopt help usage version path2hashref/ ],
27 );
28  
29 188 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=18.0
30 188 $AUTOLOAD =~ s/.*:://;
31 188 ▶ return if $AUTOLOAD eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $Zoidberg::Utils::AUTOLOAD eq "DESTROY" 0 188 188
32  
33 188 my ($class, @subs);
34 188 for my $key (keys %loadable) {
35 314 ▶ next unless grep {$AUTOLOAD eq $_} @{$loadable{$key}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$Zoidberg::Utils::AUTOLOAD eq $_;} @{$Zoidberg::Utils::loadable{$key};} 126 188 314
36 188 ($class, @subs) = @{delete $loadable{$key}};
37 188 ▶ eval "use $class \@subs";
Subroutine BEGIN: called CC=1 SCAR=0.0
38 188 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 188 188
39 188 last;
40 }
41  
42 188 ▶ die "Could not load '$AUTOLOAD'" unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 188 188
43 67 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 188 goto &{$AUTOLOAD};
45 }
46  
47 ## Various methods ##
48  
49 1 ▶sub setting {
Subroutine setting: called CC=4 SCAR=15.6
Pod undocumented
50 # FIXME support for Fish argument and namespace
51 1 my $key = shift;
52 1 ▶ return undef unless exists $Zoidberg::CURRENT->{settings}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Zoidberg::CURRENT->{"settings"}{$key} 0 1 1
53 1 my $ref = $Zoidberg::CURRENT->{settings}{$key};
54 1 ▶ return (wantarray && ref($ref) eq 'ARRAY') ? (@$ref) : $ref;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $ref eq 'ARRAY' ? : 1 0 1
Condition33%1/3
wantarray and ref $ref eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
wantarray and ref $ref eq "ARRAY"
wantarray ref $ref eq "ARRAY"
Truth table33%1/3
A and B
A= wantarrayB= ref $ref eq "ARRAY"
ABresult
0X0
100
111
55 }
56  
57 1 ▶sub read_data_file {
Subroutine read_data_file: called CC=6 SCAR=18.5
Pod documented
58 1 my $file = shift;
59 1 ▶ error 'read_data_file() is not intended for fully specified files, try read_file()'
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^/] 0 1 1
60 if $file =~ m!^/!;
61 1 for my $dir (setting('data_dirs')) {
62 2 for ("$dir/data/$file", map "$dir/data/$file.$_", qw/pl pd yaml/) {
63 6 ▶ next unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 5 1 6
64 1 ▶ error "Can not read file: $_" unless -r $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_ 0 1 1
65 1 return read_file($_);
66 }
67 }
68 0 error "Could not find 'data/$file' in (" .join(', ', setting('data_dirs')).')';
69 }
70  
71 65 ▶sub read_file {
Subroutine read_file: called CC=9 SCAR=31.2
Pod documented
72 65 my $file = shift;
73 65 ▶ error "no such file: $file\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 65 65
74  
75 65 my $ref;
76 0 ▶ if ($file =~ /^\w+$/) { todo 'executable data file' }
Branch: 4 branches
BranchTrueFalseTotal
if ($file =~ /^\w+$/) { } 0 65 65
elsif ($file =~ /\.(pl)$/i) { } 65 0 65
elsif ($file =~ /\.(pd)$/i) { } 0 0 0
elsif ($file =~ /\.(yaml)$/i) { } 0 0 0
77 elsif ($file =~ /\.(pl)$/i) {
78 65 eval q{package Main; $ref = do $file; die $@ if $@ };
79 }
80 0 elsif ($file =~ /\.(pd)$/i) { $ref = pd_read($file) }
81 elsif ($file =~ /\.(yaml)$/i) {
82 0 ▶ eval 'require YAML' or error $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require YAML" or error($@) 0 0 0
83 0 $ref = YAML::LoadFile($file);
84 }
85 0 else { error qq/Unkown file type: "$file"\n/ }
86  
87 65 ▶ error "In file $file\: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 65 65
88 65 ▶ error "File $file did not return a defined value" unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 65 65
89 65 return $ref;
90 }
91  
92 0 ▶sub pd_read {
Subroutine pd_read: not called CC=3 SCAR=24.8
Pod undocumented
93 0 my $FILE = shift;
94  
95 0 print STDERR "Deprecated config file: $FILE - should be a .pl instead of .pd\n";
96  
97 0 ▶ open FILE, '<', $FILE or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $FILE or return undef 0 0 0
98 0 my $CONTENT = join '', (<FILE>);
99 0 close FILE;
100 0 my $VAR1;
101 0 eval $CONTENT;
102 0 ▶ complain("Failed to eval the contents of $FILE ($@)") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
103 0 return $VAR1;
104 }
105  
106 141 ▶sub merge_hash {
Subroutine merge_hash: called CC=2 SCAR=6.9
Pod undocumented
107 141 my $ref = {};
108 141 local $ERROR_CALLER = 2;
109 141 $ref = _merge($ref, $_) for @_;
110 141 return $ref;
111 }
112  
113 282 ▶sub _merge { # Removed use of Storable::dclone - can throw nasty bugs
Subroutine _merge: called CC=5 SCAR=18.1
114 282 my ($ref, $ding) = @_;
115 282 ▶ while (my ($k, $v) = each %{$ding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ding;}) 1332 282 1614
116 1332 ▶ if (defined $$ref{$k} and ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref->{$k} and ref $v eq 'HASH') { } 0 1332 1332
Condition66%2/3
defined $ref->{$k} and ref $v eq "HASH"
!ll&&!rl&&r
12101220
MC/DC0%0/2
defined $ref->{$k} and ref $v eq "HASH"
defined $ref->{$k} ref $v eq "HASH"
Truth table66%2/3
A and B
A= defined $ref->{$k}B= ref $v eq "HASH"
ABresult
0X0
100
111
117 error 'incompatible types for key: '.$k.' in merging hashes'
118 0 ▶ unless ref($$ref{$k}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref->{$k} eq "HASH" 0 0 0
119 0 $$ref{$k} = _merge($$ref{$k}, $v); #recurs
120 }
121 1332 else { $ref->{$k} = $v; }
122 }
123 282 return $ref;
124 }
125  
126 67 1;
127  
128 __END__
« blib/lib/Zoidberg/Shell.pm ↑ Summary blib/lib/Zoidberg/DispatchTable.pm »