Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: A way to untie -T vars?
- X-seq: zsh-users 28709
- From: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- To: Pier Paolo Grassi <pierpaolog@xxxxxxxxx>
- Cc: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: A way to untie -T vars?
- Date: Sun, 22 Jan 2023 10:58:46 +0100
- Archived-at: <https://zsh.org/users/28709>
- In-reply-to: <CAP+y1xAJq6TrkbTOSFNs=jbf7_=sTYnj3Y1=qHoF5W4S1r=Zkg@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CAKc7PVDEmKnY9TiYwaAgvzXEqNhLwZo46paUPjJOz9HsU3_djg@mail.gmail.com> <CAH+w=7YY5F_ZYJgivw9PSjt8PTQ9R+gHF3jnjbBw-LLkEnc2zg@mail.gmail.com> <CAH+w=7bL4txwLp4zFPeDvb6fbYfJV6-mT+S4oFnrJ7cXPZr_Xw@mail.gmail.com> <CAH+w=7a1RHjYSGnY2=dgZn9n1ZgC2PyJQ12imiDU4gLW4q4saA@mail.gmail.com> <CAN=4vMpo9i5qr-HmH2ymaxaJ6hQLg-gThZkSRJvafUEp1oN0ag@mail.gmail.com> <CAP+y1xAJq6TrkbTOSFNs=jbf7_=sTYnj3Y1=qHoF5W4S1r=Zkg@mail.gmail.com>
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