| 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
| |||||||||||||||||||||||||||||
| 42 | 0 | $untie = 1; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | $var = $words[1]; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($var =~ /^(\$|\@|\%)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if (ref $curr_val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (ref $curr_val eq 'ARRAY' and @$curr_val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $curr_val eq "ARRAY" and @$curr_val
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
| |||||||||||||||||||||||||||||
| 87 | 0 | $actual_type = 'ARRAY'; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (ref $curr_val eq 'HASH' and keys %curr_val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $curr_val eq "HASH" and keys %curr_val
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
| |||||||||||||||||||||||||||||
| 90 | 0 | $actual_type = 'HASH'; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||
| 94 | 1 | ▶ | if ($curr_val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | $actual_type = 'SCALAR'; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | ▶ | $requested_type ||= $actual_type || 'SCALAR'; | ||||||||||||||||||||||||||
|
Condition40%2/5
$requested_type ||= $actual_type || "SCALAR"
$actual_type || 'SCALAR'
MC/DC0%0/2
$requested_type ||= $actual_type || "SCALAR"
$requested_type
$actual_type
Truth table33%1/3
A ||= B
A= $requested_typeB= $actual_type
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | if ($untie) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($requested_type eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | untie ${"$Psh::PerlEval::current_package\:\:$name"}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ($requested_type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | untie @{"$Psh::PerlEval::current_package\:\:$name"}; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($requested_type eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$actual_type and $actual_type ne $requested_type
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
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($requested_type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | Psh::Options::set_option($name, '') unless $actual_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | tie ${"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Scalar', $name; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($requested_type eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | Psh::Options::set_option($name, []) unless $actual_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | Psh::Options::set_option($name, {}) unless $actual_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | tie %{"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Hash', $name; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1; | ||||||||||||||||||||||||||||