package Psh
::Builtins
::Tieopt
;
require Psh
::Support
::TiedOption
;
=item * C<tieopt NAME $VAR>
=item * C<tieopt NAME @VAR>
=item * C<tieopt NAME %VAR>
Ties the option named NAME to the global variable $VAR, @VAR or %VAR
Ties the option named NAME to the global variable named $NAME, @NAME, or %NAME, depending on the option's type.
=item * C<tieopt -u $VAR>
Unties the global variable named $VAR
Note that you can simply use perl's built-in C<untie $VAR>
my ($untie, $name, $var, $actual_type, $requested_type);
if ($words[0] =~ /^-u$/i) {
if ($var =~ /^(\$|\@|\%)/) {
$requested_type = $var_types{$1};
if ($name =~ /^(\$|\@|\%)/) {
$requested_type = $var_types{$1};
if ($var =~ /^(\$|\@|\%)/) {
$requested_type = $var_types{$1};
$curr_val = Psh
::Options
::get_option
($name);
$actual_type = ref $curr_val;
if (ref $curr_val eq 'ARRAY' and @
$curr_val) {
if (ref $curr_val eq 'HASH' and keys %curr_val) {
$requested_type ||= $actual_type || 'SCALAR';
if ($requested_type eq 'SCALAR') {
untie ${"$Psh::PerlEval::current_package\:\:$name"};
if ($requested_type eq 'ARRAY') {
untie @
{"$Psh::PerlEval::current_package\:\:$name"};
if ($requested_type eq 'HASH') {
untie %{"$Psh::PerlEval::current_package\:\:$name"};
if ($actual_type and $actual_type ne $requested_type) {
if ($requested_type eq 'ARRAY') {
Psh
::Util
::print_error_i18n
('bi_tieopt_badtype_array', $name);
elsif ($requested_type eq 'HASH') {
Psh
::Util
::print_error_i18n
('bi_tieopt_badtype_hash', $name);
Psh
::Util
::print_error_i18n
('bi_tieopt_badtype_scalar', $name);
# print STDERR "tying option: $name to \${$Psh::PerlEval::current_package\:\:$var}\n" if $requested_type eq 'SCALAR';
# print STDERR "tying option: $name to \@{$Psh::PerlEval::current_package\:\:$var}\n" if $requested_type eq 'ARRAY';
# print STDERR "tying option: $name to \%{$Psh::PerlEval::current_package\:\:$var}\n" if $requested_type eq 'HASH';
if ($requested_type eq 'SCALAR') {
Psh
::Options
::set_option
($name, '') unless $actual_type;
tie
${"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Scalar', $name;
if ($requested_type eq 'ARRAY') {
Psh
::Options
::set_option
($name, []) unless $actual_type;
tie @
{"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Array', $name;
if ($requested_type eq 'HASH') {
Psh
::Options
::set_option
($name, {}) unless $actual_type;
tie
%{"$Psh::PerlEval::current_package\:\:$var"}, 'Psh::Support::TiedOption::Hash', $name;