Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH] Support the mksh's ${|func;} substitution
- X-seq: zsh-workers 44739
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- Subject: Re: [PATCH] Support the mksh's ${|func;} substitution
- Date: Sat, 7 Sep 2019 16:07:41 +0100
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=hUv5DPI/eiPWWBHy0vWUvsPkxM6JWg/rG9J3yDbnPb8=; b=LVSaaWn31QtvBKZq7hu7YB/0/qZk90HutklO6CzOls293dK/Fao3AfRQdLPq3Sd429 NzBSOAVp/IUrKP7Y0TpKjyaV6QG0tRW/A3L11pYGecgzf0jqrhotG04M4MMPK8lUbaa1 j+Whjb2AjswvWtMIcb8shnSp9ikoTXXJw8EhDHR1wBd29h9RGkTOzsJWXMbg+BxvCumB LMOk5fXFg0n10zmvbPVYE2tVWBANz3Us2OW4AuXF/FEiLs77aLzIqhWGi3JjPF6w6JrC tqR6g0eITBdut2oA/vF3KUDzylckO5xjd3uYFPGLMyQV7NO1LCBbs73qX0wfjL7FkrCS hEhg==
- In-reply-to: <CAKc7PVBTw2j=awaf0wAkyyO08k=vossU28fvZ=s+fhqMkcKuJQ@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mail-followup-to: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CAKc7PVBTw2j=awaf0wAkyyO08k=vossU28fvZ=s+fhqMkcKuJQ@mail.gmail.com>
2019-09-06 02:52:39 +0200, Sebastian Gniazdowski:
> Hello
> Some notes on the patch:
> - the subst is being handled at top of paramsubst, because it's made
> uncompatible with (...)-flags (${|(U)funct;} looks awful, more on this
> in the patch),
> - then the `s' variable is advanced past the semicolon, so that the
> rest of the function can safely progress doing (almost) nothing
> - one thing that the function still does is a fetchvalue, which I
> prevent from setting vunset to 1 in case of ${|func;}
>
> If commited, the substitution will be super useful in // substitution. E.g.:
>
> arr=( val1 val2 abc1 abc3 )
> func() { REPLY="${(C)match[1]}"; }
> print -rl ${arr[@]//(#b)(*)/${|func;}}
[...]
Note that mksh's operator can do ${|REPLY=value}, it's not
limited to functions. The ; is also not necessary, contrary to
the ${ code; } variant from ksh93 (but which mksh implements
with temporary files instead of changing the whole I/O framework).
Note that in zsh anything is a valid function name, just like
just about anything is a valid command name. So your operator
would be incompatible with mksh's if it accepted arbitrary
function names unless you handled quoting in there:
$ ./Src/zsh -c '"REPLY=value"() { REPLY=x; echo done; }; REPLY\=value; echo ${|REPLY=value;}'
done
zsh:1: no such function: REPLY=value
$ ./Src/zsh -c '"REPLY=value"() { REPLY=x; echo done; }; REPLY\=value; echo ${|"REPLY=value";}'
done
zsh:1: no such function: "REPLY=value"
So at the moment I'd say it has a few problems in that:
- it doesn't accept all function names
- the ; is unnecessary here
- it doesn't allow arbitrary code.
With those fixed, i.e. when it's really like mksh's ${|code},
I'd agree the feature could be useful, but I suspect that would
be harder to implement as it would mean changing the parsing.
Note that beside the math functions, zsh already has something
similar with its "dynamic named directory" framework (a feature
I always found quite obscure/far fetched myself).
echo "${| REPLY=value}"
could be done in zsh with:
zsh_directory_name() {
[[ $1 = d ]] && [[ $2 = //* ]] || return
eval " ${2#//}"
reply=("$REPLY" ${#2})
}
echo ${${${(D):-//REPLY=value}#\~\[}%\]}
(even more convoluted than your math function approach).
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author