Psh/Builtins/Tieopt.pm

filter: Statementstmt 43.4% 30 / 69 Branchbran 20.8% 10 / 48 Conditioncond 21.4% 3 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 32.3% 46 / 142 CC 30 SCAR 56.5
CC
30
Cov
33%
bi_tieopt
302.8
BEGIN
1.0
BEGIN
1.0
CRAP 284.0
« Psh/Parser.pm ↑ Summary Psh/Builtins/Which.pm »
1 package Psh::Builtins::Tieopt;
2  
3 require Psh;
4 require Psh::Util;
5 require Psh::Support::TiedOption;
6 require Psh::Options;
7  
8 =item * C<tieopt NAME $VAR>
9  
10 =item * C<tieopt NAME @VAR>
11  
12 =item * C<tieopt NAME %VAR>
13  
14 Ties the option named NAME to the global variable $VAR, @VAR or %VAR
15  
16 =item * C<tieopt NAME>
17  
18 Ties the option named NAME to the global variable named $NAME, @NAME, or %NAME, depending on the option's type.
19  
20 =item * C<tieopt -u $VAR>
21  
22 Unties the global variable named $VAR
23 Note that you can simply use perl's built-in C<untie $VAR>
24  
25 =cut
26  
27 1 ▶sub bi_tieopt {
Subroutine bi_tieopt: called CC=30 SCAR=57.1
28 1 my $line = shift;
29 1 my @words = @{shift()};
30  
31 1 my ($untie, $name, $var, $actual_type, $requested_type);
32  
33 1 my %var_types = (
34 '$' => 'SCALAR',
35 '@' => 'ARRAY',
36 '%' => 'HASH',
37 );
38  
39 # untie NAME
40 # untie $VAR
41 1 ▶ if ($words[0] =~ /^-u$/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($words[0] =~ /^-u$/i) { } 0 1 1
elsif (@words == 1) { } 1 0 1
elsif (@words == 2) { } 0 0 0
42 0 $untie = 1;
43  
44 0 $var = $words[1];
45 0 ▶ if ($var =~ /^(\$|\@|\%)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^(\$|\@|\%)/) 0 0 0
46 0 $requested_type = $var_types{$1};
47 }
48 0 $var =~ s/\W//g;
49 0 $name = $var;
50  
51 }
52  
53 # tieopt NAME
54 # tieopt $VAR
55 elsif (@words == 1) {
56 1 $name = $words[0];
57  
58 1 ▶ if ($name =~ /^(\$|\@|\%)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(\$|\@|\%)/) 0 1 1
59 0 $requested_type = $var_types{$1};
60 }
61 1 $name =~ s/\W//g;
62  
63 1 $var = $name;
64 }
65 # tieopt NAME $VAR
66 elsif (@words == 2) {
67 0 ($name, $var) = @words;
68  
69 0 ▶ if ($var =~ /^(\$|\@|\%)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^(\$|\@|\%)/) 0 0 0
70 0 $requested_type = $var_types{$1};
71 }
72 0 $name =~ s/\W//g;
73 0 $var =~ s/\W//g;
74 }
75 else {
76 0 return;
77 }
78  
79  
80 1 $curr_val = Psh::Options::get_option($name);
81  
82 1 $actual_type = ref $curr_val;
83  
84 1 ▶ if (defined $curr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $curr_val) 1 0 1
85 1 ▶ if (ref $curr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $curr_val) { } 0 1 1
86 0 ▶ if (ref $curr_val eq 'ARRAY' and @$curr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $curr_val eq "ARRAY" and @$curr_val) 0 0 0
Condition0%0/3
ref $curr_val eq "ARRAY" and @$curr_val
!ll&&!rl&&r
000
MC/DC0%0/2
ref $curr_val eq "ARRAY" and @$curr_val
ref $curr_val eq "ARRAY" @$curr_val
Truth table0%0/3
A and B
A= ref $curr_val eq "ARRAY"B= @$curr_val
ABresult
0X0
100
111
87 0 $actual_type = 'ARRAY';
88 }
89 0 ▶ if (ref $curr_val eq 'HASH' and keys %curr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $curr_val eq "HASH" and keys %curr_val) 0 0 0
Condition0%0/3
ref $curr_val eq "HASH" and keys %curr_val
!ll&&!rl&&r
000
MC/DC0%0/2
ref $curr_val eq "HASH" and keys %curr_val
ref $curr_val eq "HASH" keys %curr_val
Truth table0%0/3
A and B
A= ref $curr_val eq "HASH"B= keys %curr_val
ABresult
0X0
100
111
90 0 $actual_type = 'HASH';
91 }
92 }
93 else {
94 1 ▶ if ($curr_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_val) 1 0 1
95 1 $actual_type = 'SCALAR';
96 }
97 }
98 }
99  
100 1 ▶ $requested_type ||= $actual_type || 'SCALAR';
Condition40%2/5
$requested_type ||= $actual_type || "SCALAR"
l!l&&r!l&&!r
010
$actual_type || 'SCALAR'
l!l
10
MC/DC0%0/2
$requested_type ||= $actual_type || "SCALAR"
$requested_type $actual_type
Truth table33%1/3
A ||= B
A= $requested_typeB= $actual_type
ABresult
1X1
011
000
101  
102 1 ▶ if ($untie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untie) { } 0 1 1
103 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 0 ▶ if ($requested_type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_type eq "SCALAR") 0 0 0
105 0 untie ${"$Psh::PerlEval::current_package\:\:$name"};
106 }
107 0 ▶ if ($requested_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_type eq "ARRAY") 0 0 0
108  
109 0 untie @{"$Psh::PerlEval::current_package\:\:$name"};
110 }
111 0 ▶ if ($requested_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_type eq "HASH") 0 0 0
112 0 untie %{"$Psh::PerlEval::current_package\:\:$name"};
113 }
114 }
115 else {
116 1 ▶ if ($actual_type and $actual_type ne $requested_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual_type and $actual_type ne $requested_type) 0 1 1
Condition33%1/3
$actual_type and $actual_type ne $requested_type
!ll&&!rl&&r
010
MC/DC0%0/2
$actual_type and $actual_type ne $requested_type
$actual_type $actual_type ne $requested_type
Truth table33%1/3
A and B
A= $actual_typeB= $actual_type ne $requested_type
ABresult
0X0
100
111
117 0 ▶ if ($requested_type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($requested_type eq 'ARRAY') { } 0 0 0
elsif ($requested_type eq 'HASH') { } 0 0 0
118 0 Psh::Util::print_error_i18n('bi_tieopt_badtype_array', $name);
119 }
120 elsif ($requested_type eq 'HASH') {
121 0 Psh::Util::print_error_i18n('bi_tieopt_badtype_hash', $name);
122 }
123 else {
124 0 Psh::Util::print_error_i18n('bi_tieopt_badtype_scalar', $name);
125 }
126 0 return;
127 }
128 # print STDERR "tying option: $name to \${$Psh::PerlEval::current_package\:\:$var}\n" if $requested_type eq 'SCALAR';
129 # print STDERR "tying option: $name to \@{$Psh::PerlEval::current_package\:\:$var}\n" if $requested_type eq 'ARRAY';
130 # print STDERR "tying option: $name to \%{$Psh::PerlEval::current_package\:\:$var}\n" if $requested_type eq 'HASH';
131  
132 {
133  
134 # Tie the $name to $var
135 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 1 ▶ if ($requested_type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_type eq "SCALAR") 1 0 1
137 1 ▶ Psh::Options::set_option($name, '') unless $actual_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $actual_type 0 1 1
138 1 tie ${"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Scalar', $name;
139 }
140 0 ▶ if ($requested_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_type eq "ARRAY") 0 0 0
141 0 ▶ Psh::Options::set_option($name, []) unless $actual_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $actual_type 0 0 0
142 0 tie @{"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Array', $name;
143  
144 }
145 0 ▶ if ($requested_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($requested_type eq "HASH") 0 0 0
146 0 ▶ Psh::Options::set_option($name, {}) unless $actual_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $actual_type 0 0 0
147 0 tie %{"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Hash', $name;
148 }
149 }
150 }
151 }
152  
153 1;
« Psh/Parser.pm ↑ Summary Psh/Builtins/Which.pm »