Psh/Options.pm

filter: Statementstmt 21.9% 16 / 73 Branchbran 13.4% 7 / 52 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 33.3% 2 / 6 Podpod n/a totaltotal 14.7% 25 / 169 CC 37 SCAR 42.9
CC
37
Cov
15%
get_printable_option
156.0
set_option
50.8
get_option
50.0
CRAP 72.8
« Psh/Builtins/Firsttime.pm ↑ Summary Psh/Builtins/Symbols.pm »
1 package Psh::Options;
2  
3 require Psh;
4 require Psh::OS;
5  
6 # externally stored options
7 my %env_options= qw( cdpath 1 fignore 1 histsize 1 ignoreeof 1
8 ps1 1 ps2 1 path 1);
9  
10 # internally stored options and their defaults
11 my %options=(
12 'array_exports' => {
13 'path' => $Psh::OS::PATH_SEPARATOR,
14 'classpath' => $Psh::OS::PATH_SEPARATOR,
15 'ld_library_path' => $Psh::OS::PATH_SEPARATOR,
16 'fignore' => $Psh::OS::PATH_SEPARATOR,
17 'cdpath' => $Psh::OS::PATH_SEPARATOR,
18 'ls_colors' => ':',
19 },
20 'expansion' => 1,
21 'globbing' => 1,
22 'window_title' => '\w',
23 'save_history' => 1,
24 );
25  
26 # setup defaults for ENV variables
27 if (!exists $ENV{HISTSIZE}) {
28 $ENV{HISTSIZE}=50;
29 }
30  
31 1 ▶sub set_option {
Subroutine set_option: called CC=11 SCAR=39.3
32 1 my $option= lc(shift());
33 1 my @value= @_;
34 1 ▶ return unless $option;
Branch: 1 branch
BranchTrueFalseTotal
unless $option 0 1 1
35 1 ▶ return unless @value;
Branch: 1 branch
BranchTrueFalseTotal
unless @value 0 1 1
36 1 my $val;
37 1 ▶ if ($env_options{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_options{$option}) { } 0 1 1
38 0 ▶ if (@value>1 or (ref $value[0] and ref $value[0] eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@value > 1 or ref $value[0] and ref $value[0] eq 'ARRAY') { } 0 0 0
Condition0%0/6
@value > 1 or ref $value[0] and ref $value[0] eq "ARRAY"
l!l&&r!l&&!r
000
ref $value[0] and ref $value[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
@value > 1 or ref $value[0] and ref $value[0] eq "ARRAY"
@value > 1 ref $value[0] ref $value[0] eq "ARRAY"
Truth table0%0/4
A or B and C
A= @value > 1B= ref $value[0]C= ref $value[0] eq "ARRAY"
ABCresult
1XX1
0111
00X0
0100
39 0 ▶ if (ref $value[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value[0]) 0 0 0
40 0 @value= @{$value[0]};
41 }
42 0 ▶ if ($options{array_exports} and $options{array_exports}{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'array_exports'} and $options{'array_exports'}{$option}) { } 0 0 0
Condition0%0/3
$options{"array_exports"} and $options{"array_exports"}{$option}
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"array_exports"} and $options{"array_exports"}{$option}
$options{"array_exports"} $options{"array_exports"}{$option}
Truth table0%0/3
A and B
A= $options{"array_exports"}B= $options{"array_exports"}{$option}
ABresult
0X0
100
111
43 0 $val= join($options{array_exports}{$option},@value);
44 } else {
45 0 $val= $value[0];
46 }
47 } else {
48 0 $val= $value[0];
49 }
50 0 $ENV{uc($option)}= $val;
51 } else {
52 1 ▶ if (@value>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@value > 1) { } 0 1 1
53 0 $val= \@value;
54 } else {
55 1 $val= $value[0];
56 }
57 1 $options{$option}= $val;
58 }
59 }
60  
61 1 ▶sub get_option {
Subroutine get_option: called CC=10 SCAR=39.1
62 1 my $option= lc(shift());
63 1 my $val;
64 1 ▶ if ($env_options{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_options{$option}) { } 0 1 1
65 0 $val= $ENV{uc($option)};
66 0 ▶ if ($options{array_exports} and $options{array_exports}{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"array_exports"} and $options{"array_exports"}{$option}) 0 0 0
Condition0%0/3
$options{"array_exports"} and $options{"array_exports"}{$option}
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"array_exports"} and $options{"array_exports"}{$option}
$options{"array_exports"} $options{"array_exports"}{$option}
Truth table0%0/3
A and B
A= $options{"array_exports"}B= $options{"array_exports"}{$option}
ABresult
0X0
100
111
67 0 $val= [split($options{array_exports}{$option}, $val)];
68 }
69 } else {
70 1 $val=$options{$option};
71 }
72 1 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 1 0 1
73 1 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 1 1
74 0 ▶ if (ref $val and ref $val eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val and ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val and ref $val eq 'HASH') { } 0 0 0
Condition0%0/3
ref $val and ref $val eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val and ref $val eq "ARRAY"
ref $val ref $val eq "ARRAY"
Truth table0%0/3
A and B
A= ref $valB= ref $val eq "ARRAY"
ABresult
0X0
100
111
75 0 return @{$val};
76 ▶ } elsif ( ref $val and ref $val eq 'HASH') {
Condition0%0/3
ref $val and ref $val eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val and ref $val eq "HASH"
ref $val ref $val eq "HASH"
Truth table0%0/3
A and B
A= ref $valB= ref $val eq "HASH"
ABresult
0X0
100
111
77 0 return %{$val};
78 }
79 0 return $val;
80 } else {
81 1 return $val;
82 }
83 }
84 0 return undef;
85 }
86  
87 0 ▶sub get_printable_option {
Subroutine get_printable_option: not called CC=12 SCAR=50.5
88 0 my $option= shift;
89 0 my $noquote= shift;
90 0 my $tmpval= get_option($option);
91 0 my $val= '';
92 0 ▶ if (ref $tmpval) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmpval) { } 0 0 0
93 0 ▶ if (ref $tmpval eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $tmpval eq 'HASH') { } 0 0 0
elsif (ref $tmpval eq 'ARRAY') { } 0 0 0
elsif (ref $tmpval eq 'CODE') { } 0 0 0
94 0 $val='{';
95 0 ▶ while (my ($k,$v)= each %$tmpval) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tmpval) 0 0 0
96 0 ▶ next unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
97 0 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 0 0 0
98 0 $val.=" \'".$k."\' => \'".$v."\', ";
99 } else {
100 0 $val.=" \'".$k."\' => undef, ";
101 }
102 }
103 0 $val= substr($val,0,-2).' }';
104 } elsif (ref $tmpval eq 'ARRAY') {
105 0 $val='[';
106 0 foreach (@$tmpval) {
107 0 ▶ if (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 0 0 0
108 0 $val.=" \'".$_."\', ";
109 } else {
110 0 $val.=" undef, ";
111 }
112 }
113 0 $val= substr($val,0,-2).' ]';
114 } elsif (ref $tmpval eq 'CODE') {
115 0 $val='CODE';
116 }
117 } else {
118 0 ▶ if (defined $tmpval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tmpval) { } 0 0 0
119 0 ▶ if ($noquote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noquote) { } 0 0 0
120 0 $val= $tmpval;
121 } else {
122 0 $val= qq['$tmpval'];
123 }
124 } else {
125 0 $val= 'undef';
126 }
127 }
128 0 return $val;
129 }
130  
131 0 ▶sub has_option {
Subroutine has_option: not called CC=4 SCAR=30.0
132 0 my $option= lc(shift());
133 return 1 if exists $options{$option} or ($env_options{$option} and
134 0 ▶ exists $ENV{uc($option)});
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$option} or $env_options{$option} and exists $ENV{uc $option} 0 0 0
Condition0%0/6
exists $options{$option} or $env_options{$option} and exists $ENV{uc $option}
l!l&&r!l&&!r
000
$env_options{$option} and exists $ENV{uc $option}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $options{$option} or $env_options{$option} and exists $ENV{uc $option}
exists $options{$option} $env_options{$option} exists $ENV{uc $option}
Truth table0%0/4
A or B and C
A= exists $options{$option}B= $env_options{$option}C= exists $ENV{uc $option}
ABCresult
1XX1
0111
00X0
0100
135 0 return 0;
136 }
137  
138 0 ▶sub del_option {
Subroutine del_option: not called CC=2 SCAR=17.9
139 0 my $option= lc(shift());
140 0 ▶ if ($env_options{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_options{$option}) { } 0 0 0
141 0 delete $ENV{uc($option)};
142 } else {
143 0 delete $options{$option};
144 }
145 }
146  
147 0 ▶sub list_options {
Subroutine list_options: not called CC=3 SCAR=24.8
148 0 my @opts= keys %options;
149 0 foreach (keys %env_options) {
150 0 ▶ push @opts, lc($_) if exists $ENV{uc($_)};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{uc $_} 0 0 0
151 }
152 0 return @opts;
153 }
154  
155 1;
« Psh/Builtins/Firsttime.pm ↑ Summary Psh/Builtins/Symbols.pm »