zsh-users
 help / color / mirror / code / Atom feed
* saved from prince of eval
@ 2015-11-08 17:07 Ray Andrews
  2015-11-08 19:57 ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Ray Andrews @ 2015-11-08 17:07 UTC (permalink / raw)
  To: Zsh Users

I'm trying to get some mileage out of the '(e)' flag, but it frustrates me:

1:    $ foo="${(e)${array}[${top}, ${bottom}]}"

foo contains the name of the array, nothing more.  The doc says that 
these things can be nested. I tried a few things on one line with no 
luck. If I insert the literal name of the array in place of "${array}" 
everything is fine.

But this works:

2:    $ bar='\$${array}[${top}, ${bottom}]'
3:    $ foo="${(e)$(print -R "${(e)${bar}}")}"

I'm not sure how to interpret it tho. Is 'print' doing the work here, or 
is print a bystander as a nested use of '(e)' works?  The expansion of 
'bar' with it's single quoted string in '3' has a sort of linearity to 
it--you'd think that '1' would work, but it refuses to expand all three 
parameters whereas in '3' the expansion handles all three parameters in 
a 'dumb' but actually far more intuitive and helpful way--it just does 
it. I'd like to understand this better, it could be a case of some tiny 
syntactic error. Why does '1' not work? What's really going on in '3'? 
In any case, it replaces the evil eval which is very nice:

      $ foo='stranger'
      $ bar='echo howdy $foo'
      $ eval $bar
    howdy stranger                     << Sure ...

      $ eval baz=$bar; echo $baz
    zsh: command not found: howdy << Ferkrissakes
    echo howdy $foo

      $ baz=${(e)bar}; echo $baz << Tranquility
    echo howdy stranger

... so '(e)' is our friend. We should be warned from mother's milk not 
to use eval, and I have been warned, but, nuts, I didn't realize that 
there was a safe solution.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-11-11  5:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-08 17:07 saved from prince of eval Ray Andrews
2015-11-08 19:57 ` Bart Schaefer
2015-11-08 22:04   ` Ray Andrews
2015-11-09  8:50     ` Ray Andrews
2015-11-09 18:41       ` Bart Schaefer
2015-11-10  0:48         ` Ray Andrews
2015-11-11  1:07         ` Ray Andrews
2015-11-11  4:18           ` Bart Schaefer
2015-11-11  5:31             ` Ray Andrews

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).