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

Re: [PATCH] Support the mksh's ${|func;} substitution



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