zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: saved from prince of eval
Date: Sun, 08 Nov 2015 09:07:52 -0800	[thread overview]
Message-ID: <563F8168.5080006@eastlink.ca> (raw)

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.


             reply	other threads:[~2015-11-08 17:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-08 17:07 Ray Andrews [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=563F8168.5080006@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).