Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Higher order functions in zsh (article link)
On 2013-03-07 at 11:59 -0500, Christopher Browne wrote:
> cbbrowne@cbbrowne /tmp/maptest> function map {
> local func_name=$1
> shift
> for elem in $@; print -- $(eval $func_name "${elem}")
> }
>
> But somewhat curiously that doesn't help :-(.
Why does this code have an eval in there? It's:
(1) breaking the f3 example
(2) introducing a security flaw
Hint: touch 'f4 `mkdir snert`'
This works fine:
----------------------------8< cut here >8------------------------------
function map {
local func_name=$1
shift
for elem in "$@"; print -- $($func_name "${elem}")
}
----------------------------8< cut here >8------------------------------
With the eval in there, with { map echo * } f3 loses the quotes and f4
just shows "f4" and creates a new sub-directory, snert.
Lose the eval and things work, no new security hole.
Oh, and you need to quote the $@ to "$@" if you want to preserve empty
elements -- whether you do or not depends on what you're mapping across,
but I tend to think "present but empty" is distinct from "not present".
-Phil
Messages sorted by:
Reverse Date,
Date,
Thread,
Author