Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: A way to untie -T vars?



On Sun, Jan 22, 2023 at 10:34 AM Pier Paolo Grassi <pierpaolog@xxxxxxxxx> wrote:
>
> and what your solution could look like? I would use something like
> ___untie_array and ___untie_scalar that I would never use outside
> the untie function, but I wonder if there is a more general solution

Something like this:

    untie() {
      emulate -L zsh
      case ${(tP)1} in
      (scalar-tied)
        set -- $1 ${(P)1}
        unset $1
        typeset -g $1=$2
        ;;
      (array-tied)
        set -- $1 "${(@P)1}"
        unset $1
        set -A "$@"
        ;;
      (*) ;;
      esac
    }

Positional parameters already shadow same-name parameters from the
caller, so we can safely use them for locals.

I've made one unrelated change here: replaced `eval` with `set -A`. It
looks simpler, and incidentally fixes a bug with the handling of empty
elements.

If you wanted to turn this into a library function, you would need to
handle more types. Things like array-local-tied (simply ignore
"local") and scalar-tied-export (make sure to export the scalar). This
should be straightforward.

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author