Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH 1/3]: Add named references
- X-seq: zsh-workers 52530
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: Oliver Kiddle <opk@xxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH 1/3]: Add named references
- Date: Sun, 11 Feb 2024 07:00:42 +0000
- Archived-at: <https://zsh.org/workers/52530>
- In-reply-to: <CAH+w=7aKwKhdXjPo2FQG1GqjHQzX=5to_m6kZeL-UFfQh_XMtw@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- Mail-followup-to: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Oliver Kiddle <opk@xxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- References: <CAH+w=7bd5tHQ8_ZFuyheUrTStm8pR826jH1LB-vMdEnv14nH0w@mail.gmail.com> <67689-1675827940.088548@BxvG.D9_b.7RzI> <CAH+w=7ZFq_MyNtPVetDt84Zp8dnCQXis3p=2sKP018GZ-VTd0g@mail.gmail.com> <12608-1675903622.800470@Xj82.e3y1.svhG> <CAH+w=7ZZUCqYe6w1ZqZZKR6iLsZH0SDDXyzwgTU93nxx6bmxjQ@mail.gmail.com> <66045-1675975796.128039@FBF_.0yMO.Y8fk> <CAH+w=7bcqc8SsRxsht0QFyXy=DYzj6nVaBFhdzQ5MrBB+yBz+A@mail.gmail.com> <CAH+w=7YVJO-HkneMpnfBbqBztPaXdXTD=mo-vHbdUW00TiFVBQ@mail.gmail.com> <40726-1676098925.110777@U2kb.d0Pd.I9ml> <CAH+w=7aKwKhdXjPo2FQG1GqjHQzX=5to_m6kZeL-UFfQh_XMtw@mail.gmail.com>
2023-02-10 23:45:36 -0800, Bart Schaefer:
[...]
> A named parameter declared with the '-n' option to any of the 'typeset'
> commands becomes a reference to a parameter in scope at the time of
> assignment to the named reference, which may be at a different call
> level than the declaring function. For this reason, it is good practice
> to declare a named reference as soon as the referent parameter is in
> scope, and as early as possible in the function if the reference is to a
> parameter in a calling scope.
[...]
One difference with ksh93: if the target variable was not set at
the time of the "typeset -n ref=target", then when ref is
assigned it may end up refering to a local target:
$ ./Src/zsh -c 'function f { typeset -n ref=$1; typeset var=foo; ref=X; echo "$ref ${(!)ref} $var"; }; f var; echo "$var"'
X var X
$ ./Src/zsh --emulate ksh -c 'function f { typeset -n ref=$1; typeset var=foo; ref=X; echo "$ref ${!ref} $var"; }; f var; echo "$var"'
X var X
$
Compare with ksh:
$ ksh -c 'function f { typeset -n ref=$1; typeset var=foo; ref=X; echo "$ref ${!ref} $var"; }; f var; echo "$var"'
X var foo
X
That's a common use case like for the zslurp function discussed
on zsh-users where one would do:
cmd | zslurp var
To get the output of cmd verbatim into $var with $var potentially undeclared
beforehand, which wouldn't work if var was used locally by zslurp.
It can be worked around with a typeset -g $1 before the typeset -n var=$1
$ ./Src/zsh --emulate ksh -c 'function f { typeset -g $1; typeset -n ref=$1; typeset var=foo; ref=X; echo "$ref ${!ref} $var"; }; f var; echo "$var"'
X var foo
X
But it seems to me it would be better to align with ksh's
behaviour in this instance.
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author