Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH] Support the mksh's ${|func;} substitution
- X-seq: zsh-workers 44740
- From: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- To: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH] Support the mksh's ${|func;} substitution
- Date: Sat, 7 Sep 2019 20:09:57 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=wPt9jSriPtxe6bJv9Fn+Ey3CMT9GTysd3Wbt9ngusJU=; b=VpIHo2xNL0dJNh2c2bmqt7yFYAeaZzynKVdcUDoJTzca5NE9KqfORjdeiLMlyofKs+ EkTcTeby9GT5IqQlodK1ACcp3ulIeUMCkkaOeZ6OuO58xVehCCBKSXHsToA9y9S8SwPD Hk6ZRTddRwqufYg5R4hlEdtvl0wiZqCRlWcBGuC8KLLHC9fvWIsaWeRd53c4Wj/GvDHD XO7+/WGbPeWrQkDFj/XTEvWOWpEJB0/nfGf8VKz3n37jny+/FnOWGWSxPD8L886u+3QC 8A2kM6iT/Y+N8EKPNCAJKiOOfmSHCqGHIC+NyqV9VB7poGYh9/4uQXihDyVqsdjNNeDS xXDQ==
- In-reply-to: <20190907150741.jwztdoslrvk5j7nk@chaz.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>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CAKc7PVBTw2j=awaf0wAkyyO08k=vossU28fvZ=s+fhqMkcKuJQ@mail.gmail.com> <20190907150741.jwztdoslrvk5j7nk@chaz.gmail.com>
On Sat, 7 Sep 2019 at 17:07, Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:
> Note that mksh's operator can do ${|REPLY=value}, it's not
> limited to functions.
Ok, true, it can also run binary commands.
> The ; is also not necessary
I think that this is undocumented feature, as the docs say:
"Another variant of substitution are the valsubs (value substitutions)
${|command;} which are also executed in the current environment, like
funsubs, but share their I/O with the parent; instead, they evaluate
to whatever the, initially empty, expression-local variable REPLY is
set to within the commands."
> 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
That's a valid and nice point, and it does yield some concerns,
however for any problems to appear the user would have to have at
least a function with "targetvariable=..." in name, i.e. a coincidence
of two places in code, the targetvar= in the ${|...;} and in the
function name, and this can be controlled and seems unlikely to occur
by accident.
> 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.
The parsing would have to be changed to prevent the "=" in function names?
I think that I've chosen an initial wrong direction: to implement the
substitution "as-is", with it's form, treating it as a model. Instead,
I should have implemented the feature, not the substitution. Zsh has
its own ways to set-up complex substitutions and this is done via the
parens flags.
Would you consider such method, i.e. to not impose mksh's substitution
ways on Zsh, but instead assign a flag, like e.g.: ${(|)funct}, still
useful?
> 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).
That's interesting, it actually allows to do:
arr=( val1 val2 abc1 abc3 )
funct() { REPLY="${(C)MATCH}"; }
zsh_directory_name() { ... }
print -rl ${arr[@]//(#m)*/${${${(D):-//funct}#\~\[}%\]}}
Output:
Val1
Val2
Abc1
Abc3
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
Messages sorted by:
Reverse Date,
Date,
Thread,
Author